How to store releases/binaries in GitLab?

前端 未结 5 1038
鱼传尺愫
鱼传尺愫 2020-12-07 13:25

I am building a workflow with Gitlab, Jenkins and - probably - Nexus (I need an artifact storage). I would like to have Gi

5条回答
  •  悲&欢浪女
    2020-12-07 14:00

    This answer is going to be basically the same, as the one from VonC, just described in a step-by-step manner for less experienced CI users.

    So, let's say, that you have a really cool commit 30728cab and you'd like to make this version of your code a new release...

    1) Create a tag for your commit

    git tag -a MY_TAG_NAME 30728cab
    

    After this command you'll be asked to fill in a description, just like when you're committing new changes to your code.

    2) Push the tag to your remote repository

    Tag are NOT going to be pushed there automatically with your commits! You need to push them manually to your remote.

    git push REMOTE_REPO_NAME REMOTE_BRANCH_NAME MY_TAG_NAME
    

    3) Upload a file

    Now you can either a) upload a file to the GitLab repository, b) upload it anywhere else and save the link in both cases.

    WARNING: Files uploaded to the GitLab repository can't be easily deleted then and you can't see their link later!

    While I'm NOT recommending uploading binaries to the repository because of the reason above, you asked for it, so here is the way:

    curl --request POST --header "Private-Token: YOUR_PRIVATE_TOKEN" --form "file=@/PATH/TO/THE/FILE/file.txt" "https://MY_GITLAB_HOSTING.COM/api/v4/projects/MY_PROJECT_ID/uploads"
    

    The private token can be created in User settings -> Access tokens.

    Also, if you really needed to delete the file, you can export the project, remove manually the folder updates from your downloaded archive, delete the former remote repository and create a new one by importing your downloaded and modified archive.

    4) Create a release

    Now we can finally tie it all together using Release.

    curl --request POST --header 'Content-Type: application/json' --header "Private-Token: YOUR_PRIVATE_TOKEN" --data '{"name": "MY_RELEASE_NAME", "tag_name": "MY_TAG_NAME", "description": "Release with the binary LINK_TO_YOUR_BINARY"}' "https://MY_GITLAB_HOSTING.COM/api/v4/projects/MY_PROJECT_ID/releases"
    

    You can see, that Release is inherently tied with a specific tag, which is subsequently tied to a specific commit. Connection with binaries is then performed simply by providing links to those files.

    The funny point is, that your description supports Markdown, but it's really hard to write some larger *.md document in such cumbersome one-liner. So I've written a short Bash script, which allows us to write the Markdown file aside and then it reads it and sends it automatically:

    #!/bin/bash
    
    RELEASE_NAME="$1"
    TAG_NAME="$2"
    PROJECT_ID="$3"
    DESCRIPTION_FILE_PATH="$4"
    PRIVATE_TOKEN="$5"
    
    if [ "$5" == "" ]; then
        echo "Missing parameter! Parameters are RELEASE_NAME, TAG_NAME, PROJECT_ID, DESCRIPTION_FILE_PATH and PRIVATE_TOKEN.";
        exit 1;
    fi
    
    DESCRIPTION=''
    
    # Load data from file
    while read -r line; do
        DESCRIPTION="${DESCRIPTION}${line}\n";
    done < "${DESCRIPTION_FILE_PATH}"
    
    curl --request POST\
         --header 'Content-Type: application/json'\
         --header "Private-Token: ${PRIVATE_TOKEN}"\
         --data-binary "{\"name\": \"${RELEASE_NAME}\", \"tag_name\": \"${TAG_NAME}\", \"description\": \"${DESCRIPTION}\"}"\
         "https://MY_GITLAB_HOSTING.com/api/v4/projects/${PROJECT_ID}/releases" 
    
    echo
    

    so you can use it just like

    ./upload_release.sh MY_RELEASE_NAME MY_TAG_NAME MY_PROJECT_ID MY_MARKDOWN_FILE_PATH MY_PRIVATE_TOKEN

    And now that's it! You have your first complete release!

    Until you realizes, that you've made a terrible typo in the header of your release description...

    5) Delete a release (if needed)

    Here you're lucky! Unlike uploaded binaries you can delete your releases using the REST API, too!

    curl --request DELETE --header "Private-Token: MY_PRIVATE_TOKEN" "https://MY_GITLAB_HOSTING.com/api/v4/projects/MY_PROJECT_ID/releases/MY_TAG_NAME"
    

    And as it is still pretty annoying to type this several times in a row, I've made another Bash script:

    #!/bin/bash
    
    PROJECT_ID=$1
    TAG_NAME=$2
    PRIVATE_TOKEN=$3
    
    if [ "$3" == "" ]; then
        echo "Missing parameter! Parameters are PROJECT_ID, TAG_NAME and PRIVATE_TOKEN.";
        exit 1;
    fi
    
    curl --request DELETE --header "Private-Token: ${PRIVATE_TOKEN}" "https://MY_GITLAB_HOSTING.com/api/v4/projects/${PROJECT_ID}/releases/${TAG_NAME}"
    
    echo
    
    

    which can be used like ./delete_release.sh MY_PROJECT_ID MY_TAG_NAME MY_PRIVATE_TOKEN.

提交回复
热议问题