GitHubのリポジトリに対してCI/CD等のワークフローを自動実行できるサービス。
2019年11月に正式リリースされた。
※2020-05-03 このページの内容を元に、Qiitaに投稿した。
https://help.github.com/en/actions
runs-on
でサポートされている実行環境${{ <expression> }}
形式でYAMLに埋め込める式表現とか、 if: ${{ <expression> }}
によるガード条件について参考:
2020-05-03更新
https://github.com/actions/starter-workflows … 初心者向けワークフローサンプル集
https://help.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow
NOTE:
.github/workflows/
ディレクトリ下にYAMLファイルを作成する任意のプルリクエストでtestを実行し、masterブランチへのマージでのみdeployを実行する設定例(2ファイル):
# test.yml
name: test
on: pull_request
jobs:
test:
runs-on: ubuntu-latest
steps:
# tasks for testing
---
# deploy.yml
name: deploy
on:
pull_request:
branches:
- master
types: [closed]
jobs:
deploy:
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.merged == true }}
steps:
# tasks for deployment
NOTE:
pull_request
eventでは types: [opened, synchronize, reopend]
でしか実行されない。 test
のときはこれになっている。参考:
crontabと同じ書式でワークフローを定期実行するタイミングを指定することができる。
Example:
on:
schedule:
# JSTで月曜から金曜の9:05 - 17:05の間、2時間毎に実行
- cron: '5 0-8/2 * * MON-FRI'
NOTE:
リファレンス:
Example:
on:
push:
branches:
- master
- 'releases/**'
- '!releases/**-alpha' # alpha版は含めない
tags:
- v1
# file paths to consider in the event. Optional; defaults to all.
paths:
- 'test/*'
paths-ignore:
- 'docs/**'
NOTE:
branches
と branches-ignore
は併用不可tags
と tags-ignore
は併用不可paths
と paths-ignore
は(たぶん)併用可リファレンス:
複数のOSプラットフォームや、ランタイムのバージョンに対応する方法。
下のようなbuild matrixを設定すると良い。
Example:
jobs:
node-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-16.04, ubuntu-18.04]
node: [6, 8, 10]
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
with:
node-version: ${{ matrix.node }}
NG例:
runs-on: [ubuntu-16.04, ubuntu-18.04]
リファレンス:
参考:
jobs.<job_id>.container
で指定する。
https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idcontainer
Examples:
jobs:
my_job:
container: node:10.16-jessie
---
jobs:
my_job:
container:
image: node:10.16-jessie
env:
NODE_ENV: development
ports:
- 80
volumes:
- my_docker_volume:/volume_mount
options: --cpus 1
starter-workflowsにも参考になるサンプルがある。以下は例:
https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#env
いくつかの箇所で設定できる。それぞれ適用範囲が異なる。
設定位置 | 適用範囲 |
---|---|
env | for all jobs and steps |
jobs.<job_id>.env | for all steps in the job |
jobs.<job_id>.steps.env | for the step |
jobs.<job_id>.container.env | for the container to run steps in the job |
Examples:
env:
SERVER: production
---
jobs:
my-job:
name: My Job
runs-on: ubuntu-latest
env:
MY_VAR: Hi there! My name is
steps:
- name: Print a greeting
env:
FIRST_NAME: Mona
MIDDLE_NAME: The
LAST_NAME: Octocat
run: |
echo $MY_VAR $FIRST_NAME $MIDDLE_NAME $LAST_NAME.
See also #変数やシークレットの利用
jobs.<job_id>.if
や jobs.<job_id>.steps.if
の値に条件式を記述することで、該当のjobやstepを条件が真のときのみ実行することができる。
Examples:
jobs:
deploy:
# プルリクエストがマージされた時のみ実行
if: ${{ github.event.pull_request.merged == true }}
steps:
- name: Deploy
# some deploy action
# このstepは常に実行する
- name: Notify
if: ${{ always() }}
# some notify action
# ジョブ失敗時に実行するstep
- name: Clean up on Failure
if: ${{ failure() }}
# some clean up action
NOTE:
if:
に渡す条件式では ${{ }}
は省略可能A job or step will not run when a critical failure prevents the task from running. For example, if getting sources failed.
リファレンス:
Documents:
Authenticating with the GITHUB_TOKEN - GitHub Help
GitHubは、ワークフローで利用する
GITHUB_TOKEN
シークレットを自動的に生成します。 このGITHUB_TOKEN
は、ワークフローの実行内での認証に利用できます。
ユースケースの例:
checkoutアクションでチェックアウトしたものと同じリポジトリであれば、そのままpushできるようだ。
Example:
jobs:
update:
steps:
- uses: actions/[email protected]
- run: |
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
./do-something.sh
git add .
git commit -m "Update by GitHub Action"
git push
checkoutアクションではデフォルトで ${{ github.token }}
が使われており、これにリポジトリへの書込み権限もついているからだろう。
git commit & pushを行うActionもある:
参考:
権限は ${{ github.token }}
で十分なので、上と同様にcheckoutアクションを使って、オプションで別ブランチをチェックアウトして更新を行えばよい。
Example:
jobs:
publish:
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
with:
ref: gh-pages
path: public
- run: |
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
make build
cd public
git add .
git commit -m "Update by GitHub Action"
git push
上の設定自体は試してないが、やり方的には合っているはず。
progrhyme/binq-indexではこのやり方を取っている。
Ubuntuだったら単純に sudo apt install <package>
を run
すればいい。
jobs:
install-zsh:
runs-on: ubuntu-latest
steps:
- run: sudo apt install zsh
- run: zsh --version
参考:
sudo
が使えるよと書いてある