Actions(タスク)
- Marketplace … 公開されているactionを見つけられる。
Actionとは
アクションは、ワークフロー内で実行される個々のタスク。
Actionの形式
https://help.github.com/en/actions/building-actions/about-actions#types-of-actions
Type | Operating system |
---|---|
Docker container | Linux |
JavaScript | Linux, MacOS, Windows |
ワークフローからの利用
ワークフローからは、次のロケーションのactionが利用できる:
- 公開リポジトリ
- 同リポジトリ内で参照できるもの
- Docker Hubに公開されたDockerイメージ
公式Actions
GitHub公式のactionはたぶん https://github.com/actions にあるもの。
cache
https://github.com/actions/cache
依存関係をキャッシュしてワークフローの実行時間を短くしてくれる。
リポジトリに各種言語における設定例も同梱されているので、そちらを参考に設定するといい。
checkout
https://github.com/actions/checkout
リポジトリをチェックアウトする。
おそらくほとんどのワークフローから利用される。
Example:
- uses: actions/[email protected]
with:
# Number of commits to fetch. 0 indicates all history.
# Default: 1
fetch-depth: 0
# Whether to checkout submodules: `true` to checkout submodules or `recursive` to
# recursively checkout submodules.
#
# Default: false
submodules: true
Tips:
fetch-depth: 0
を付けるとtagやbranchの履歴も全部取得してくれる
setup-go
https://github.com/actions/setup-go
Go言語のコンパイラ(goコマンド)をセットアップ。
Examples:
jobs:
build:
runs-on: ubuntu-16.04
strategy:
matrix:
go: [ '1.13', '1.12' ]
name: Go ${{ matrix.go }} sample
steps:
- uses: actions/[email protected]
- name: Setup go
uses: actions/[email protected]
with:
go-version: ${{ matrix.go }}
- run: go run hello.go
setup-node
https://github.com/actions/setup-node
Node.jsをセットアップ。
Basic:
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
with:
node-version: '10.x'
- run: npm install
- run: npm test
Matrix Testing:
jobs:
build:
runs-on: ubuntu-18.04
strategy:
matrix:
node: [ '10.x', '12.x' ]
name: Node ${{ matrix.node }} sample
steps:
- uses: actions/[email protected]
- name: Setup node
uses: actions/[email protected]
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test
参考:
- GitHub ActionsでのNode.jsの利用 - GitHub ヘルプ
- https://github.com/actions/starter-workflows/blob/master/ci/node.js.yml
setup-python
https://github.com/actions/setup-python
Python実行環境のセットアップ。
Example:
jobs:
build:
:
steps:
- name: Setup Python
uses: actions/[email protected]
with:
python-version: '3.x' # Python 3系を使う
architecture: 'x64'
3rd Party Actions
Slack通知用
たくさんあるが、starが多くて目についたものを載せる:
- 8398a7/action-slack … メッセージ等のカスタマイズの余地はなさそうだが、必要十分な感ある
- rtCamp/action-slack-notify … 通知先チャネルも設定可能で、よさそう
SLACK_WEBHOOK
にはIncoming Webhook URLを設定する- 参考: GitHub Actionsでビルドの成功・失敗をSlackに通知する方法 - Qiita
Deployments作成用
Deploymentsについては See GitHub#Deployments
これもたくさんあるが、一部のみ掲載する。
NOTE:
- 後掲のpeaceiris/actions-gh-pagesのように、内部的にDeploymentを作成するactionもあるようだ。
Actions:
hashicorp/terraform-github-actions
https://github.com/hashicorp/terraform-github-actions
terraformを実行するaction
ドキュメント:
NOTE:
- デフォルト設定でプルリクエストにfmt, validate, plan, applyの結果をコメントしてくれて便利。
例: terraform fmt -> validate -> plan
name: terraform plan
on: pull_request
env:
tf_version: '0.12.24'
tf_work_dir: '.'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
plan:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- name: terraform fmt
uses: hashicorp/[email protected]
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_subcommand: fmt
tf_actions_working_dir: ${{ env.tf_work_dir }}
- name: terraform init
uses: hashicorp/[email protected]
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_subcommand: init
tf_actions_working_dir: ${{ env.tf_work_dir }}
- name: terraform validate
uses: hashicorp/[email protected]
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_subcommand: validate
tf_actions_working_dir: ${{ env.tf_work_dir }}
- name: terraform plan
uses: hashicorp/[email protected]
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_subcommand: plan
tf_actions_working_dir: ${{ env.tf_work_dir }}
例: terraform plan -> apply
name: terraform apply
on:
push:
branches:
- master
env:
tf_version: '0.12.24'
tf_work_dir: '.'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
apply:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- name: terraform init
uses: hashicorp/[email protected]
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_subcommand: init
tf_actions_working_dir: ${{ env.tf_work_dir }}
- name: terraform plan
id: plan
uses: hashicorp/[email protected]
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_subcommand: plan
tf_actions_working_dir: ${{ env.tf_work_dir }}
- name: terraform apply
# planの差分がある時のみ実行
if: ${{ steps.plan.outputs.tf_actions_plan_has_changes == 'true' }}
uses: hashicorp/[email protected]
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_subcommand: apply
tf_actions_working_dir: ${{ env.tf_work_dir }}
参考:
peaceiris/actions-gh-pages
https://github.com/peaceiris/actions-gh-pages
GitHub Pagesに公開するaction.
Example:
- uses: peaceiris/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
#publish_branch: master # default: gh-pages
peaceiris/actions-hugo
https://github.com/peaceiris/actions-hugo
ランナー上にHugoをインストールする。
Example:
- name: Setup Hugo
uses: peaceiris/[email protected]
with:
hugo-version: '0.68.3'
# extended: true
- name: Build
run: hugo --minify
Tips:
extended: true
でHugoの拡張版をインストールhugo-version: latest
で最新版を使う