以下の内容はhttps://kdnakt.hatenablog.com/entry/2025/01/05/creating-a-docker-container-github-actionより取得しました。


GitHub Actionsのactionをゼロから作る

Dockerコンテナーのアクションを作成するチュートリアルを試してみた。

docs.github.com

[きっかけ]

最近、Learning GitHub Actions: Automation and Integration of CI/CD with GitHub (English Edition)を読んでいる。

IPUSIRONさんのツイートを見て、Humble Bundleでまとめ売りされていた本を購入した。その中に入っていたのがきっかけ(Linuxか?というと謎だけど...)。

[action実装チュートリアル]

本を読んでいてymlファイルが出てきても全体像がよくわからないのと、全く手を動かさないのもよくないかなと思っていた。GitHubのドキュメントを探したところ、ちょうど良さそうなチュートリアルを見つけたので試してみた。

docs.github.com

GitHub Actions用のactionを実装していく。まずは、actionのためのコードを保管するリポジトリを作成する。

続いて、actionを実行するDockerfileとactionのコードをentrypoint.shとして実装する。

# Container image that runs your code
FROM alpine:3.10

# Copies your code file from your action repository to the filesystem path `/` of the container
COPY entrypoint.sh /entrypoint.sh

# Code file to execute when the docker container starts up (`entrypoint.sh`)
ENTRYPOINT ["/entrypoint.sh"]

#!/bin/sh -l

echo "Hello $1"
time=$(date)
echo "time=$time" >> $GITHUB_OUTPUT

あとは、必要なメタデータを以下のようにaction.ymlにまとめる。

# action.yml
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 }}

続いて、これを利用するワークフローを定義する。サンプルをベースに以下のような形に実装した。

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - name: Hello world action step
        id: hello
        uses: kdnakt/hello-world-action@main
        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 }}"

onの中にworkflow_dispatchを設定しておくと、手動でこのジョブを実行することができ、デバッグなどの際に便利とのこと。

stepの定義の中でusesを使って今回実装したactionを「uses: kdnakt/hello-world-action@main」のように指定している。
最初に実装したときは、usesでactionを指定する際に、@以降を忘れてしまって「the `uses' attribute must be a path, a Docker image, or owner/repo@ref」というエラーが表示された。mainブランチしかないので、refにmainを指定して無事エラーが解消した。

[brandingアイコン]

マーケットプレイスに公開されているアクションを見ると、様々なアイコンが設定されている。

github.com

これは、以下のような設定をaction.ymlファイルに追加することで実現できる。

branding:
  icon: 'award'
  color: 'green'

docs.github.com

brandingに利用できるアイコンはGitHub Actions Branding Cheat Sheetにまとまっていて分かりやすい。

haya14busa.github.io

例えば、GitHub Pages actionのアイコンは次のようになっている。

これは、以下のコードで実装されている。

branding:
  icon: 'upload-cloud'
  color: 'blue'

github.com

今回実装したactionでもbrandingを定義してみたが、ワークフローの実行履歴などには特に反映されなかった。残念。

[まとめ]

github.com

github.com




以上の内容はhttps://kdnakt.hatenablog.com/entry/2025/01/05/creating-a-docker-container-github-actionより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14