How to store releases/binaries in GitLab?

前端 未结 5 1018
鱼传尺愫
鱼传尺愫 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 13:39

    I am using a command line tool I've written in python as a shortcut. Does some steps of the API methods described in previous answers (Upload, Release, Delete Release).

    You can check the source code or just use it yourself.

    For even simpler version, if anyone is interested in doing these with python and requests.py; it is pretty trivial.

    For Upload

    import requests
    secret_token = ""
    project_id = ""
    file_path = "your_file.txt"
    api_root = "https://gitlab.com/api/v4"
    
    headers = {
        'PRIVATE-TOKEN': secret_token,
    }
    
    uri = '{}/projects/{}/uploads'.format(api_root, project_id)
    
    files = {
        'file': open(file_path, 'rb')
    }
    
    
    r_upload = requests.post(uri, headers=headers, files=files)
    if(r_upload.status_code != 201 and r_upload.status_code != 200):
        raise ValueError(f"Upload API responded with unvalid status {r_upload.status_code}")  # noqa: E501
    
    upload = r_upload.json()
    

    Upload returns a json with the link and the markdown formatted link. I found markdown formatted link pretty useful for the release description. Since you have to add the link to the description yourself.

    For Creating a Release

    import requests
    secret_token = ""
    project_id = ""
    api_root = "https://gitlab.com/api/v4"
    
    desc = """
    Your release description. **You can use markdown !**
    Don't forget to add download link here.
    """
    
    headers = {
        'PRIVATE-TOKEN': secret_token,
    }
    
    data = {
        "description": desc
    }
    
    r_new_release = requests.post(uri, data=data, headers=headers)
    if(r_new_release.status_code != 201 and r_new_release.status_code != 200):
        raise ValueError(f"Releases API responded with invalid status {r_new_release.status_code}")
    
    new_release = r_new_release.json()
    

    JSON Response is the release.

    For the other steps basically follow API guidelines and make the adjustments. All of them are pretty similar.

提交回复
热议问题