LoginSignup
3
3

More than 5 years have passed since last update.

リポジトリに変更を加えずにtravisビルドを実行(travis API経由)してみた。

Last updated at Posted at 2016-03-31

背景・目的

githubにpushするタイミングでtravisビルド(テスト)を実行する、それも、特定のブランチ限定で、というのはよくある使い方ではないでしょうか。
基本はこの運用なのですが、自由にtravisビルドを実行したい時があります。そこで、任意のブランチを任意のタイミングで、travisビルド実行するというのをやってみました。

スクリプト作成に当たってはここを参照しました。

事前準備

travisからアクセストークンを取得する必要があります。(環境変数 MY_TRAVIS_TOKEN)
myorganizationとmyapplicationを実行を行わせたいgithubリポジトリに設定します。(環境変数 MY_TRAVIS_REPO)
ここで設定した環境変数の値を後述のスクリプトで使用します。

実行例です。

$ gem install travis
$ travis login --pro
$ export MY_TRAVIS_TOKEN=`travis token --pro`
$ export MY_TRAVIS_REPO='myorganization%2Fmyapplication'

スクリプトの準備

trig-build.sh
#!/bin/bash                                                                                                                                                                                                                              

MY_TRAVIS_REPO=${MY_TRAVIS_REPO:?}
MY_TRAVIS_TOKEN=${MY_TRAVIS_TOKEN:?}

if [ "$#" = "1" ]; then
    branch=$1
else
    echo "usage : $0 <branch>"
    exit 1
fi

body="{                                                                                                                                       
\"request\": {                                                                                                                                
  \"message\": \"$branch `date +%y%m%d%H%M%S`\",                                                                                              
  \"branch\":\"$branch\",                                                                                                                     
  \"notifications\": {                                                                                                                        
    \"email\": \"false\"                                                                                                                      
  },                                                                                                                                          
  \"config\": {                                                                                                                               
    \"branches\": \"*\"                                                                                                                       
  }                                                                                                                                           
}}"

curl -s -X POST \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "Travis-API-Version: 3" \
  -H "Authorization: token $MY_TRAVIS_TOKEN" \
  -d "$body" \
  https://api.travis-ci.com/repo/$MY_TRAVIS_REPO/requests

travisビルドの実行

このスクリプトを使い、ブランチを指定してtravisビルドを実行します。

$ (path-to)/trig-build.sh develop
・・・
$ (path-to)/trig-build.sh feature/foobar
・・・

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3