Actions(タスク)

  • Marketplace … 公開されているactionを見つけられる。

Actionとは

アクションは、ワークフロー内で実行される個々のタスク。

About actions - GitHub Help

Actionの形式

https://help.github.com/en/actions/building-actions/about-actions#types-of-actions

TypeOperating system
Docker containerLinux
JavaScriptLinux, MacOS, Windows

ワークフローからの利用

https://help.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#referencing-actions-in-your-workflow

ワークフローからは、次のロケーションの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/checkout@v2
  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/checkout@v2
      - name: Setup go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go }}
      - run: go run hello.go

setup-node

https://github.com/actions/setup-node

Node.jsをセットアップ。

Basic:

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
  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/checkout@v2
      - name: Setup node
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node }}
      - run: npm install
      - run: npm test

参考:

setup-python

https://github.com/actions/setup-python

Python実行環境のセットアップ。

Example:

jobs:
  build:
    :
    steps:
      - name: Setup Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.x' # Python 3系を使う
          architecture: 'x64'

3rd Party Actions

Slack通知用

たくさんあるが、starが多くて目についたものを載せる:

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/checkout@v2

      - name: terraform fmt
        uses: hashicorp/terraform-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tf_actions_subcommand: fmt
          tf_actions_working_dir: ${{ env.tf_work_dir }}

      - name: terraform init
        uses: hashicorp/terraform-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tf_actions_subcommand: init
          tf_actions_working_dir: ${{ env.tf_work_dir }}

      - name: terraform validate
        uses: hashicorp/terraform-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tf_actions_subcommand: validate
          tf_actions_working_dir: ${{ env.tf_work_dir }}

      - name: terraform plan
        uses: hashicorp/terraform-github-actions@master
        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/checkout@v2

      - name: terraform init
        uses: hashicorp/terraform-github-actions@master
        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/terraform-github-actions@master
        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/terraform-github-actions@master
        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/actions-gh-pages@v3
  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/actions-hugo@v2
  with:
    hugo-version: '0.68.3'
    # extended: true

- name: Build
  run: hugo --minify

Tips:

  • extended: true でHugoの拡張版をインストール
  • hugo-version: latest で最新版を使う