我试图找出一个在GitHub上生成构建的单命令进程。
我预计会做的是运行某种命令发布,比如说,make release脚本会构建发布工件,然后以某种方式将其上传到GitHub。
但是,我对如何在GitHub上实际获取发布工件感到很困惑。源代码很棒,但不是每个人都想做自己的构建。 :-)
Update September 2013,你可以自动发布(API in preview mode)
Update January 2014,有一个非官方的命令行应用程序,由github-release称为Nicolas Hillegeer (aktau
),用于创建发布和上传(二进制)工件。
它使用上面提到的新github发布API。看看项目的Makefile,看看如何更自动化它。
例:
# create a formal release
$ github-release release \
--user aktau \
--repo gofinance \
--tag v0.1.0 \
--name "the wolf of source street" \
--description "Not a movie, contrary to popular opinion. Still, my first release!" \
--pre-release
由于二进制资产,此API略有不同。我们在请求发布资产时使用Accept标头进行内容协商。 传递标准API媒体类型以获取API表示:
$ curl -i -H "Authorization: token TOKEN" \
-H "Accept: application/vnd.github.manifold-preview" \
"https://uploads.github.com/repos/hubot/singularity/releases/assets/123"
HTTP/1.1 200 OK
{
"id": 123,
...
}
传递“application / octet-stream”以下载二进制内容。
$ curl -i -H "Authorization: token TOKEN" \
-H "Accept: application/octet-stream" \
"https://uploads.github.com/repos/hubot/singularity/releases/assets/123"
HTTP/1.1 302 Found
上传由对同伴“
uploads.github.com
”服务的单个请求处理。
$ curl -H "Authorization: token TOKEN" \
-H "Accept: application/vnd.github.manifold-preview" \
-H "Content-Type: application/zip" \
--data-binary @build/mac/package.zip \
"https://uploads.github.com/repos/hubot/singularity/releases/123/assets?name=1.0.0-mac.zip"
Update 2d July 2013,你现在可以定义一个版本。
这就是取代old binary upload service的removed in December 2012!
make release脚本构建了发布工件,然后以某种方式将其上传到github。
这意味着将它(“它”是由一个或多个文件传递,通常包括二进制文件)添加到常规本地存储库,然后将该存储库推送到其匹配的GitHub存储库。
话虽如此,GitHub在任何“发布”任务中都没有提到的原因是因为Git是一个源代码管理系统,并且不适合二进制文件。
它当然可以包含那些文件(二进制文件),但不会定期使用它们,因为一段时间后repo的大小膨胀:每次克隆需要更长时间。 请参阅What are the Git limits,以及“git - should source files and repository be on the same machine ?”。
制备:
1)下载github-releases并将其可执行文件放入PATH中。 2)在https://github.com/settings/applications#personal-access-tokens创建一个令牌,让我们说abc123
上传工件:
1)假设您刚刚编译了您决定调用版本3.1的内容,并希望上传它。 2)确保你已经承诺了一切。 3)运行以下五个命令:
git tag v3.1
git push
git push --tags
github-release release --security-token abc123 --user <you> --repo <yourrepo> \
--tag v3.1
github-release upload --security-token abc123 --user <you> --repo <yourrepo> \
--tag v3.1 --name <thefile> --file <thefile>
您可以上载多个文件,例如用于不同的操作系统。
(基于VonC的回答,遗憾的是没有详细说明如何上传工件)
如果您使用Maven,您可以添加GitHub的下载Maven插件(https://github.com/github/maven-plugins/#downloads-plugin)并简单地执行:
$ mvn clean install ghDownloads:upload
Github有一个API来访问他们自己的文件下载系统。回购下载允许您为用户提供二进制文件 - 尽管可能存在大小和数量的限制。 API允许从自动代理访问。请查看:http://developer.github.com/v3/repos/downloads/了解使用信息。
该功能使用不多,但绝对有效。您可以转到任何github仓库,单击“下载”选项卡查看它们。
有关可下载文件的示例:http://github.com/dannystaple/emacs_cheat_sheets/downloads - 提供的HTML文件实际上是一个构建的人工制品,而不是源代码。我试图沙沙作响一个更好的(二进制)示例 - 但没有理由不能提供可执行文件,zips / tarball和其他文件类型。
这些下载与repo或其标记的源代码压缩包不同。任何文件都可以通过这种方式上传。
我遇到了同样的问题,为我做了一个小蟒蛇。我必须说这是一个痛苦,s3是一个完全的怪胎。
https://raw.github.com/reklis/utilityscripts/master/github-upload
#!/opt/local/bin/python2.7
import json
import requests
import sys
import argparse
import os
import mimetypes
import pycurl
import cStringIO
from xml.dom import minidom
github_api_root = "https://api.github.com/"
def parse_args():
parser = argparse.ArgumentParser(description='post a file to github as a download')
parser.add_argument('--user', dest='user', help='github username', required=True)
parser.add_argument('--pass', dest='password', help='github password', required=True)
parser.add_argument('--repo', dest='repo', help='the name of the github repo', required=True)
parser.add_argument('--file', dest='filepath', help='path of the local file to upload', required=True)
parser.add_argument('--desc', dest='description', help='descriptive text about this file', required=True)
parser.add_argument('--owner', dest='owner', help='owner of the github repository', required=True)
args = parser.parse_args()
# print args
return args
def make_dl_post_url(owner, repo):
url = "%srepos/%s/%s/downloads" % (str(github_api_root), str(owner), str(repo))
# print url
return url
def make_dl_delete_url(owner, repo, dlid):
url = "%srepos/%s/%s/downloads/%s" % (str(github_api_root), str(owner), str(repo), str(dlid))
# print url
return url
def add_github_reference(args):
dl_post_url = make_dl_post_url(args.owner, args.repo)
fp = args.filepath
filename = os.path.basename(fp)
filesize = os.path.getsize(fp)
mtype, mdetails = mimetypes.guess_type(fp)
file_description = {
'name': filename,
'size': filesize,
'description': args.description,
'content_type': mtype
}
# print json.dumps(file_description, indent=2)
github = requests.post(dl_post_url, auth=(args.user, args.password), data=json.dumps(file_description))
resp = github.json
# print json.dumps(resp, indent=2)
return resp
def remove_github_reference(args, dlid):
dl_delete_url = make_dl_delete_url(args.owner, args.repo, dlid)
github = requests.delete(dl_delete_url, auth=(args.user, args.password))
delete_ok = (204 == github.status_code)
return delete_ok
def post_file_to_s3(file_path, gh):
# s3 is very particular with field ordering
# curl \
# -F "key=downloads/octocat/Hello-World/new_file.jpg" \
# -F "acl=public-read" \
# -F "success_action_status=201" \
# -F "Filename=new_file.jpg" \
# -F "AWSAccessKeyId=1ABCDEF..." \
# -F "Policy=ewogIC..." \
# -F "Signature=mwnF..." \
# -F "Content-Type=image/jpeg" \
# -F "file=@new_file.jpg" \
# https://github.s3.amazonaws.com/
s3_ok = 201
xml_buffer = cStringIO.StringIO()
try:
post_fields = [
('key', str(gh['path'])),
('acl', str(gh['acl'])),
('success_action_status', str(s3_ok)),
('Filename', str(gh['name'])),
('AWSAccessKeyId', str(gh['accesskeyid'])),
('Policy', str(gh['policy'])),
('Signature', str(gh['signature'])),
('Content-Type', str(gh['mime_type'])),
('file', (pycurl.FORM_FILE, file_path))
]
# print post_fields
s3 = pycurl.Curl()
s3.setopt(pycurl.SSL_VERIFYPEER, 0)
s3.setopt(pycurl.SSL_VERIFYHOST, 0)
s3.setopt(pycurl.POST, 1)
s3.setopt(pycurl.URL, str(gh['s3_url']))
s3.setopt(pycurl.HTTPPOST, post_fields)
# s3.setopt(pycurl.VERBOSE, 1)
# accumulate string response
s3.setopt(pycurl.WRITEFUNCTION, xml_buffer.write)
s3.perform()
file_upload_success = (s3_ok == s3.getinfo(pycurl.HTTP_CODE))
xml_payload = minidom.parseString(xml_buffer.getvalue())
if (file_upload_success):
location_element = xml_payload.getElementsByTagName('Location')
print location_element[0].firstChild.nodeValue
else:
print xml_payload.toprettyxml()
except Exception, e:
print e
file_upload_success = False
finally:
s3.close()
return file_upload_success
def main():
mimetypes.init()
args = parse_args()
# step 1: tell github about the file
gh = add_github_reference(args)
# step 2: upload file to s3
if ('errors' in gh):
print json.dumps(gh, indent=2)
else:
file_upload_success = post_file_to_s3(args.filepath, gh)
# cleanup if upload failed
if (False == file_upload_success):
removed_ok = remove_github_reference(args, gh['id'])
if (removed_ok):
print "removed github reference"
else:
print "failed to remove github reference"
if __name__ == '__main__':
main()
hub
官方Go-based GitHub CLI工具
首先安装Go。在Ubuntu:https://askubuntu.com/questions/959932/installation-instructions-for-golang-1-9-into-ubuntu-16-04/1075726#1075726
然后安装hub
:
go get github.com/github/hub
没有Ubuntu包:https://github.com/github/hub/issues/718
然后从您的回购内部:
hub release create -a prebuilt.zip -m 'release title' tag-name
这个:
tag-name
的遥控器上创建一个未注释的标签prebuilt.zip
作为附件您还可以使用GITHUB_TOKEN
环境变量提供现有API令牌。
对于其他release
操作,请参阅:
hub release --help
在hub
de684cb613c47572cc9ec90d4fd73eef80aef09c上测试过。
Python APIv3上传示例,没有任何外部依赖项
用法:
GITHUB_TOKEN=<token> ./create-release username/reponame <tag-name> <path-to-upload>
脚本:
#!/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())
如果已经存在,则发布和资产创建将失败,并且422。首先删除版本或资产,以解决此问题。这是an example。
对于那些使用gradle的人来说,插件gradle-github-plugin还允许创建版本并将文件附加到它们。
gradle.build
:plugins {
id "co.riiid.gradle" version "X.Y.Z"
}
github {
owner = 'riiid'
repo = 'gradle-github-plugin'
token = 'XXXXXXXXXXXXXXXXXXXXX'
tagName = '0.1.0'
targetCommitish = 'master'
name = 'v0.1.0'
body = """# Project Name
Write `release note` here.
"""
assets = [
'app/build/outputs/apk/app-release.apk',
'app/build/outputs/mapping/release/mapping.txt',
'app/build/outputs',
...
]
}