使用 Travis 调用 GitHub API 构建 Tag [英] Call GitHub API with Travis to build Tag

查看:12
本文介绍了使用 Travis 调用 GitHub API 构建 Tag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GitHub 存储库中创建了一个 TravisCI Hook,它会在推送到存储库后自动运行构建.我想补充的是,如果构建成功,则会自动创建一个标签.

I have created a TravisCI Hook in a GitHub repository that automatically run a build after pushing to the repo. What I would like to add is that if the build succeeds a tag is automatically created.

我发现有一种方法可以使用 GitHub API 创建标签 http://developer.github.com/v3/git/tags/#create-a-tag-object

I have found out that there is a way to create tags with the GitHub API http://developer.github.com/v3/git/tags/#create-a-tag-object

但是如何控制对我的存储库的访问?我无法在 travis.yml 中公开我的登录 github 凭据,因为每个人都可以阅读它,因为它包含在存储库中.

But how do I control access to my repository? I can't expose my login github credentials in the travis.yml because everyone can read it as it is cointained in the repository.

我对自动部署很陌生,所以如果有任何其他解决方案可以在没有 travis 的情况下执行此操作,请告诉我.我想要实现的是在成功构建后为用户创建一个可下载的版本.

I am pretty new to automated deployment so if there is any other solution to do this without travis please let me also know. What I would like to achieve is that a downloadable version is created for the users after a successful build.

好的,我终于找到了正确的 travis.yaml 配置.

Ok I have finally found the correct travis.yaml Configuration.

它是如何工作的:推送到存储库后,travis 将运行我的应用程序的测试.如果测试成功,travis 将构建当前构建的预编译版本并将其上传到我在 GitHub 存储库上创建的特殊版本.

How does it work: After pushing to the repository, travis will run the tests of my application. If the tests are successful travis will build a precompiled version of the current build and upload it to a special release which I have created on the GitHub Repo.

    language: scala
    env:
      global:
        - PLAY_VERSION=2.2.1
        - secure: "HD1x0S9ad/3+G9YUkyT/uTw9lEr+tUQEV4QO+M2Ro1JFSVOzLNZiNoh6FrNb06a0TbencTkftyHYmYjp1/CCyTpF9CMCQ4ddB7TVF9hibH1y9ONVrPJIm5BCEpjGDa4fND8bkcChrpcZDQKIO0ZwArEsl2+IRocnbBT+oYqIFNo="
    before_script:
      - wget http://downloads.typesafe.com/play/${PLAY_VERSION}/play-${PLAY_VERSION}.zip
      - unzip -q play-${PLAY_VERSION}.zip
      - sudo apt-get install jq
    script: play-${PLAY_VERSION}/play test
    notifications:
      email: false
    after_success: 
      - play-${PLAY_VERSION}/play dist
      - cd target/universal/
      - 'ASSETID=$(curl -s -H "Authorization: token ${BUILD_KEY}" "https://api.github.com/repos/meisign/fillable/releases/204198/assets" | jq ".[0].id")'
      - 'curl -XDELETE -s -H "Authorization: token ${BUILD_KEY}" "https://api.github.com/repos/meisign/fillable/releases/assets/$ASSETID"'
      - 'curl -XPOST -s -H "Authorization: token ${BUILD_KEY}" -H "Content-Type: application/zip" --data-binary @./Fillable-1.0-SNAPSHOT.zip "https://uploads.github.com/repos/meisign/fillable/releases/204198/assets?name=Fillable.zip"'

推荐答案

你可以创建一个GitHub Personal API Token 将授予对您的存储库的访问权限.public_repo 范围应该是公共存储库所需的全部.

You can create a GitHub Personal API Token that will grant access to your repositories. The public_repo scope should be all you need for a public repository.

使用此令牌对 GitHub API 进行身份验证.要将令牌与 API 一起使用,请将其包含在 Authorization 标头中.

Use this token for authenticating to the GitHub API. To use the token with the API include it in the Authorization header.

curl -H "Authorization: token <YOUR_TOKEN>" https://api.github.com/user

您也可以使用此令牌推送到您的存储库.

You can also use this token to push to your repository.

git push -q https://<token>@github.com/<user>/<repo>

现在有趣的部分是,您需要将该令牌保密.公开就等于公开您的用户名和密码.

Now for the fun part, you need to keep that token a secret. Having it public is equivalent to having your username and password public.

您需要确保通读所引用的文档,并密切关注您的 Travis-CI 日志.这些命令在 bash 中运行,取决于您的编写方式或是否有任何错误,您可能意外泄露您的令牌.

You need to be sure to read through the documentation referenced and keep an eye on your Travis-CI logs. The commands run in bash, and depending how you write it or if there are any errors you could accidentally reveal your token.

为了将该令牌保密,Travis-CI 有一个生成公钥和私钥的系统.加密密钥特定于您的存储库.

To keep that token a secret Travis-CI has a system for generating public and private keys. The encryption keys are specific to your repository.

链接包含所有相关文档;您需要安装 Travis 命令行界面工具,它可以作为 Ruby Gem 使用.

The link has all of the relevant documentation; you need to install the Travis command line interface tool, it is available as a Ruby Gem.

gem install travis

加密变量(例如您的个人令牌)-

To encrypt a variable (such as your personal token) -

travis encrypt SOMEVAR=secretvalue --add

Travis 假定该命令正在项目目录中运行,并将根据您的存储库提供唯一的公钥来加密您的数据.--add 标志将自动将受保护的数据放入您的 .travis.yml 文件中.

Travis assumes that the command is being run in the project directory and will provide the unique Public key to encrypt your data, based on your repository. The --add flag will automatically place the secured data in your .travis.yml file.

这就是你保守秘密的方式.使用 Git 或 GitHub API 创建标签的实现取决于您.想清楚后请分享.

That's how you keep it a secret. The implementation of creating tags with Git or the GitHub API is up to you. Please share once you figure it out.

这篇关于使用 Travis 调用 GitHub API 构建 Tag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆