LoginSignup
2
3

More than 3 years have passed since last update.

GitHubのactionsを試してみる【actions】

Posted at

GitHub が提供する CI/CD ツール、Actions。このツールを使いこなせればこのブログのテーマのデプロイが簡略化出来そうなので、どんなものか試してみた。

ゴール

自前で用意した Dockerfile を使用してワークフローを実行する。 公式にてアクションの作成サンプルがあるのでそちらを参考にしながらカスタムアクションを作成してみる。

Docker コンテナのアクションを作成する

準備するもの

ワークフローを定義するためにmain.ymlを作成、 Docker コンテナで実行するためにaction.ymlを作成し、 Dockerfileと entrypoint.shでアクションコードを作成する。

ディレクトリ構造

今回作成する設定ファイルを含めたディレクトリ構造。ワークフローを記述したファイルは .github/workflows に配置した。 ファイル名は main.yaml にしたが、特に指定はない。

.
├── .github
│   └── workflows
│       └── main.yaml
├── Dockerfile
├── README.md
├── action.yml
└── entrypoint.sh

ワークフローファイル(main.yml)の中身

今回はこのファイルがあるリポジトリでタスクを実行するので checkout を実行する。

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
    # To use this repository's private action, you must check out the repository
    - name: Checkout
      uses: actions/checkout@v1
    - name: Hello world action step
      id: hello
      uses: ./
      with:
        who-to-greet: 'Mona the Octocat'
    # Use the output from the `hello` step
    - name: Get the output time
      run: echo "The time was ${{ steps.hello.outputs.time }}"

jobs..steps.uses

このプロパティではaction.yml またはDockerfileのディレクトリを指定する。直接ファイル名を指定するとエラーになる

##[error]Can't find 'action.yml' or 'Dockerfile' under '/home/runner/work/actions/actions/.github/action.yml'.

アクションファイル(action.yml)の中身

Docker コンテナで実行するのでruns.usingに dockerを指定する。

name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  time: # id of output
    description: 'The time we greeted you'
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.who-to-greet }}

Dockerfileの中身

entrypoint.shを実行するだけの内容。

FROM alpine:3.10

COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT [ "/entrypoint.sh" ]

entrypoint.shの中身

引数をうけとり出力する。

#!/bin/sh -l

echo "Hello $1"
time=$(date)
echo ::set-output name=time::$time

いざ実行

上記のファイルを用意してpushすればワークフローが実行される。実行が成功すると下記のような感じで各タスクにチェックマークがつく
actions-result.png

Refference

GitHub Actionsのワークフロー構文

GitHub Actionsのメタデータ構文

2
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
2
3