Post

GitHub Actions Certificate (GH-200) チートシート

概要

GitHub Actions Certificate 試験のための簡潔なリファレンスです、試験受けた当日につくったのでかなりホヤホヤです。 4つのドメインごとに重要な構文とコマンドを整理しています。


ドメイン1: ワークフローの作成と管理 (40%)

イベントトリガー

参考: Workflow syntax - on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 単一イベント
on: push

# 複数イベント
on: [push, pull_request]

# ブランチフィルタ
on:
  push:
    branches: [main, develop]
    paths: ['src/**']

# スケジュール(UTC)
on:
  schedule:
    - cron: '0 0 * * *'  # 毎日0時

# 手動トリガー
on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'デプロイ環境'
        required: true
        default: 'staging'

ワークフローコンポーネント

参考: Workflow syntax - jobs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
name: CI Workflow

on: push

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      # アクションの使用
      - uses: actions/checkout@v4

      # シェルコマンドの実行
      - name: Run tests
        run: npm test

      # 条件付き実行
      - name: Deploy
        if: github.ref == 'refs/heads/main'
        run: ./deploy.sh
        continue-on-error: true

  # 依存ジョブ
  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying..."

シークレットと環境変数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
jobs:
  build:
    runs-on: ubuntu-latest

    env:
      # カスタム環境変数
      NODE_ENV: production

    steps:
      - name: Use secret
        run: echo "Secret is $"
        env:
          # ステップレベルの環境変数
          API_KEY: $

      - name: Use default env vars
        run: |
          echo "SHA: $GITHUB_SHA"
          echo "Ref: $GITHUB_REF"
          echo "Actor: $GITHUB_ACTOR"

特定目的のワークフロー

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# サービスコンテナ
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:14
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432:5432

# ラベルでランナー指定
jobs:
  build:
    runs-on: [self-hosted, linux, x64]

# マトリックス戦略
jobs:
  test:
    runs-on: $
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        node: [16, 18, 20]

ドメイン2: ワークフローを活用する (20%)

デバッグ

参考: Enabling debug logging

1
2
3
4
5
6
7
8
9
10
# ステップデバッグを有効化
# リポジトリシークレットに ACTIONS_STEP_DEBUG=true を設定

# ワークフローコマンドでログ出力
- name: Debug info
  run: |
    echo "::debug::This is a debug message"
    echo "::notice::This is a notice"
    echo "::warning::This is a warning"
    echo "::error::This is an error"

キャッシュ

参考: Caching dependencies

1
2
3
4
5
6
7
- name: Cache dependencies
  uses: actions/cache@v3
  with:
    path: ~/.npm
    key: $-node-$
    restore-keys: |
      $-node-

ジョブ間のデータ受け渡し

参考: Defining outputs for jobs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      output1: $
    steps:
      - id: step1
        run: echo "test=hello" >> $GITHUB_OUTPUT

  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
      - run: echo $

成果物管理

参考: Storing workflow data as artifacts

1
2
3
4
5
6
7
8
9
10
11
12
13
# アップロード
- name: Upload artifact
  uses: actions/upload-artifact@v3
  with:
    name: my-artifact
    path: dist/
    retention-days: 5

# ダウンロード
- name: Download artifact
  uses: actions/download-artifact@v3
  with:
    name: my-artifact

環境保護

参考: Using environments for deployment

1
2
3
4
5
6
7
8
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://example.com
    steps:
      - run: ./deploy.sh

ドメイン3: アクションの作成と管理 (25%)

アクションタイプの比較

参考: About custom actions

タイプ 実行環境 速度 柔軟性 用途
JavaScript Node.js 高速 GitHub API統合、軽量処理
Docker コンテナ 遅い 任意の言語・環境
Composite ホストランナー 高速 ステップの再利用

action.yml の構造

参考: Metadata syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# JavaScript アクション
name: 'My Action'
description: 'Action description'
author: 'Author Name'

inputs:
  input-name:
    description: 'Input description'
    required: true
    default: 'default value'

outputs:
  output-name:
    description: 'Output description'

runs:
  using: 'node20'
  main: 'dist/index.js'
1
2
3
4
5
6
# Docker アクション
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - $
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Composite アクション
name: 'My Composite Action'
description: 'Composite action example'

inputs:
  target:
    description: 'Target directory'
    required: true

outputs:
  result:
    description: 'Result value'
    value: $

runs:
  using: 'composite'
  steps:
    # シェルは必須指定
    - run: echo "Step 1"
      shell: bash

    # 他のアクションも使用可能
    - uses: actions/checkout@v4

    # working-directory 指定可能
    - run: npm install
      shell: bash
      working-directory: $

    # 環境変数の設定
    - run: echo "VALUE=result" >> $GITHUB_OUTPUT
      id: final
      shell: bash

ワークフローコマンド

参考: Workflow commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// JavaScript アクション内
const core = require('@actions/core');

// 入力の取得
const input = core.getInput('input-name');

// 出力の設定
core.setOutput('output-name', 'value');

// ログ
core.debug('Debug message');
core.info('Info message');
core.warning('Warning message');
core.error('Error message');

// 失敗
core.setFailed('Error message');

// 環境変数の設定
core.exportVariable('VAR_NAME', 'value');
1
2
3
4
5
# シェルスクリプト内
echo "output-name=value" >> $GITHUB_OUTPUT
echo "::debug::Debug message"
echo "::error::Error message"
exit 1  # 失敗

Composite Action の使用

参考: Creating a composite action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# ディレクトリ構造
.github/
└── actions/
    └── my-action/
        └── action.yml

# ワークフローから使用
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # ローカルアクションを使用
      - uses: ./.github/actions/my-action
        with:
          target: ./src

      # 他のリポジトリのサブディレクトリ
      - uses: owner/repo/path/to/action@v1

Composite Action の注意点

  • shellは各ステップで必須指定
  • working-directoryで作業ディレクトリを変更可能
  • outputssteps.<step_id>.outputs.<output_name>で参照
  • 他のアクション(uses)も使用可能
  • ワークフローコマンド($GITHUB_OUTPUT等)も使用可能

ドメイン4: エンタープライズ管理 (15%)

再利用可能なワークフロー

参考: Reusing workflows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 呼び出し側 (.github/workflows/caller.yml)
jobs:
  call-workflow:
    uses: org/repo/.github/workflows/reusable.yml@main
    with:
      config: value
    secrets:
      token: $

# 再利用可能ワークフロー (reusable.yml)
on:
  workflow_call:
    inputs:
      config:
        required: true
        type: string
    secrets:
      token:
        required: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo $

.github リポジトリ構造

参考: Creating starter workflows

1
2
3
4
5
6
.github/
├── workflow-templates/
│   ├── ci.yml              # ワークフローテンプレート
│   └── ci.properties.json  # メタデータ
└── workflows/
    └── organization-workflow.yml

セルフホステッドランナー

参考: About self-hosted runners

1
2
3
4
5
6
7
8
9
10
11
12
13
# ランナーの設定
./config.sh --url https://github.com/org/repo --token TOKEN

# プロキシ設定
export HTTP_PROXY=http://proxy:8080
export HTTPS_PROXY=http://proxy:8080

# ラベルの設定
./config.sh --labels "linux,x64,gpu"

# サービスとして実行
sudo ./svc.sh install
sudo ./svc.sh start
1
2
3
4
# ランナーグループの使用
jobs:
  build:
    runs-on: [self-hosted, my-runner-group]

シークレットのスコープ

参考: Using secrets in GitHub Actions

スコープ 範囲 優先度 用途
Organization 組織内の複数リポジトリ 共通の認証情報
Repository 特定リポジトリ リポジトリ固有の設定
Environment 特定環境 環境別の認証情報
1
2
3
4
5
6
# Environment シークレットの使用
jobs:
  deploy:
    environment: production  # production環境のシークレットを使用
    steps:
      - run: echo $

頻出キーワード

デフォルト環境変数

参考: Default environment variables

  • GITHUB_SHA: コミットSHA
  • GITHUB_REF: ブランチまたはタグのref
  • GITHUB_ACTOR: ワークフローをトリガーしたユーザー
  • GITHUB_REPOSITORY: リポジトリ名(owner/repo)
  • GITHUB_WORKSPACE: ワークスペースのパス
  • RUNNER_OS: ランナーのOS(Linux, Windows, macOS)
  • GITHUB_TOKEN: 自動生成される認証トークン

コンテキスト

参考: Contexts

  • github.*: GitHubイベント情報
  • env.*: 環境変数
  • secrets.*: シークレット
  • inputs.*: ワークフロー入力
  • needs.*: 依存ジョブの出力
  • matrix.*: マトリックス値
  • runner.*: ランナー情報

条件式

1
2
3
4
5
6
if: success()                          # 前のステップが成功
if: failure()                          # 前のステップが失敗
if: always()                           # 常に実行
if: cancelled()                        # キャンセル時
if: github.ref == 'refs/heads/main'    # mainブランチ
if: github.event_name == 'push'        # pushイベント

YAML構文の注意点

  1. インデント: スペース2つが標準(タブは使用不可)
  2. 階層構造: jobs.<job_id>.steps の形式を正確に
  3. 式の構文: $ で囲む
  4. シークレット参照: $ のみ($secrets.NAMEは不可)
  5. 配列表記: [item1, item2] または改行して - item1
  6. 複数行文字列: | (改行保持) または > (改行を空白に変換)

試験Tips

  1. 英語モード推奨: 日本語の翻訳品質に問題あり
  2. YAML構文: インデントとキーワードのスペルに注意
  3. 実践経験: 実際にワークフローを書いた経験が重要
  4. Enterprise機能: .githubリポジトリとランナーグループを重点的に
  5. デバッグ: ACTIONS_STEP_DEBUGとログコマンドを覚える
This post is licensed under CC BY 4.0 by the author.

© taroru. Some rights reserved.

Using the Chirpy theme for Jekyll.