Quantcast
Channel: プログラミング
Viewing all articles
Browse latest Browse all 7878

【Github】Github Custom Action ~ Docker コンテナー ~ - プログラム の超個人的なメモ

$
0
0

■ はじめに

https://dk521123.hatenablog.com/entry/2024/06/27/220219

の続き。

 前回は、Github Custom Action の JavaScript をやったので、
今回は、Docker コンテナーのアクションについて取り上げる

目次

【1】Docker コンテナーのアクション
【2】使用上の注意
 1)セルフホステッドランナ時の注意点
【3】サンプル
 0)前提条件
 1)リポジトリの作成
 2)action.ymlの作成
 3)Dockerfileの作成
 4)ローカルでのテスト
 5)GitHub Actionsでのテスト

【1】Docker コンテナーのアクション

* Docker コンテナー生成し、その上で動くアクション

https://docs.github.com/ja/actions/creating-actions/about-custom-actions#docker-container-actions

【2】使用上の注意

1)セルフホステッドランナ時の注意点

* セルフホステッドランナの場合、Dockerをインストールしてある必要がある
 => docker を内部的に使うので、当たり前っちゃー当たり前だが。

https://docs.github.com/ja/actions/creating-actions/about-custom-actions#docker-container-actions

【3】サンプル

* 「0)前提条件」~「1)リポジトリの作成」の流れは、以下の関連記事と同じ

https://dk521123.hatenablog.com/entry/2024/06/27/220219

0)前提条件

* 以下がインストールされていること
 + git
 + Node.js (npm --version で確認)
 + docker [ローカル上確認する場合。Self-hosted runnerの場合は必須]

Node.js ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2021/11/06/000000

1)リポジトリの作成

mkdir hello-world-docker-action
cd hello-world-docker-action

# プロジェクト初期化
npm init -y# Git初期化
git init
git add .
git commit -m"initial commit for hello world of custom docker action"

2)action.ymlの作成

* 前回との違いは、runs 部分のみ。

https://docs.github.com/ja/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-docker-container-actions

action.yml

name:'hello_world_docker_demo'description:'This is just hello world for docker...'branding:icon: upload-cloud
  color: blue
inputs:input_id1:description:'This is a sample input'required:truedefault:'Hi, world'outputs:output_id1:description:'This is a sample output1'# ★ここに注目runs:using:'docker'image:'Dockerfile'env:INPUT_ID1: ${{ inputs.input_id1}}

3)Dockerfileの作成

[1] Dockerファイルを作成

FROM amazonlinux
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

[2] 周辺のBashファイル(entrypoint.sh)を作成

#!/bin/bashif [-z"${INPUT_ID1}"]thenecho"No input data..."exit1fiecho"output_id1=${INPUT_ID1}">>"${GITHUB_OUTPUT}"
cat "${GITHUB_OUTPUT}"

4)ローカルでのテスト

# Dockerビルド
$ docker build -t hello-world-docker-action:latest ./

# Docker実行
$ docker run --rm--name hello-world-docker-action  -eINPUT_ID1="Hello world!!"-eGITHUB_OUTPUT=/dummy.txt hello-world-docker-action:latest

5)GitHub Actionsでのテスト

[1] コミットおよびハッシュタグ取得

$ git add .
$ git commit -m "[add] add a container action"
$ git show --format='%H' --no-patch

[2] .github/workflows/demo.yml 作成

name: DemoCustomDockerAction

on:
  workflow_dispatch:
jobs:
  demo-custom-action:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v4
      - name: Call Demo action
        id: call-demo
        uses: your-github-user-name/hello-world-docker-action@xxxxxxxxxx...xxx
        with:
          input_id1: 'Hello world!!!?'
      - name: Show result
        run: |
          echo ${{ env.output_id1 }}
        env:
          output_id1: ${{ steps.call-demo.outputs.output_id1 }}

参考文献

公式ドキュメント
https://docs.github.com/ja/actions/creating-actions/creating-a-docker-container-action
一般サイト
https://aadojo.alterbooth.com/entry/2023/06/01/183019
メモ
https://qiita.com/ohnitakahiro/items/d581317f5473a68ca368

関連記事

Github~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2022/06/16/151443
Github Actions ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/12/22/195715
Github Custom Action ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/06/27/220219


Viewing all articles
Browse latest Browse all 7878

Trending Articles