How to release a build artifact asset on GitHub with a script?

后端 未结 7 2091
半阙折子戏
半阙折子戏 2020-12-01 06:38

I am trying to figure out a one-command process for generating a build on GitHub.

What I anticipate doing is running some sort of command- make release, say, and the

7条回答
  •  悲哀的现实
    2020-12-01 06:55

    hub official Go-based GitHub CLI tool

    https://github.com/github/hub

    First install Go. On Ubuntu: https://askubuntu.com/questions/959932/installation-instructions-for-golang-1-9-into-ubuntu-16-04/1075726#1075726

    Then install hub:

    go get github.com/github/hub
    

    There is no Ubuntu package: https://github.com/github/hub/issues/718

    Then from inside your repo:

    hub release create -a prebuilt.zip -m 'release title' tag-name
    

    This:

    • prompts for your password the first time, and then automatically creates and stores an API token locally
    • creates a non annotated tag on the remote called tag-name
    • creates a release associated to that tag
    • uploads prebuilt.zip as an attachment

    You can also provide your existing API token with the GITHUB_TOKEN environment variable.

    For other release operations, see:

    hub release --help
    

    Tested on hub de684cb613c47572cc9ec90d4fd73eef80aef09c.

    Python APIv3 upload example without any external dependencies

    Usage:

    GITHUB_TOKEN= ./create-release username/reponame  
    

    Script:

    #!/usr/bin/env python3
    
    import json
    import os
    import sys
    
    from urllib.parse import urlencode
    from urllib.request import Request, urlopen
    
    repo = sys.argv[1]
    tag = sys.argv[2]
    upload_file = sys.argv[3]
    
    token = os.environ['GITHUB_TOKEN']
    url_template = 'https://{}.github.com/repos/' + repo + '/releases'
    
    # Create.
    _json = json.loads(urlopen(Request(
        url_template.format('api'),
        json.dumps({
            'tag_name': tag,
            'name': tag,
            'prerelease': True,
        }).encode(),
        headers={
            'Accept': 'application/vnd.github.v3+json',
            'Authorization': 'token ' + token,
        },
    )).read().decode())
    # This is not the tag, but rather some database integer identifier.
    release_id = _json['id']
    
    # Upload.
    with open(upload_file, 'br') as myfile:
        content = myfile.read()
    _json = json.loads(urlopen(Request(
        url_template.format('uploads') + '/' + str(release_id) + '/assets?' \
          + urlencode({'name': os.path.split(upload_file)[1]}),
        content,
        headers={
            'Accept': 'application/vnd.github.v3+json',
            'Authorization': 'token ' + token,
            'Content-Type': 'application/zip',
        },
    )).read().decode())
    

    Both release and asset creation will fail with 422 if they already exist. Work around that by first deleting the release or asset. Here is an example.

提交回复
热议问题