← すべての記事

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

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

概要#

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

チートシートそのものは AI にほぼほぼまとめてもらってます。普段から GitHub Actions 使ってる人であればチートシートの内容が理解できると思いますし、特に苦せず受かるかな?という気がします


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

イベントトリガー#

参考: Workflow syntax - on

# 単一イベント
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

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..."

シークレットと環境変数#

参考: Using secrets | Variables

jobs:
build:
runs-on: ubuntu-latest
env:
# カスタム環境変数
NODE_ENV: production
steps:
- name: Use secret
run: echo "Secret is ${{ secrets.MY_SECRET }}"
env:
# ステップレベルの環境変数
API_KEY: ${{ secrets.API_KEY }}
- name: Use default env vars
run: |
echo "SHA: $GITHUB_SHA"
echo "Ref: $GITHUB_REF"
echo "Actor: $GITHUB_ACTOR"

特定目的のワークフロー#

参考: Service containers | Using a matrix

# サービスコンテナ
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: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [16, 18, 20]

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

デバッグ#

参考: Enabling debug logging

# ステップデバッグを有効化
# リポジトリシークレットに 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

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

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

参考: Defining outputs for jobs

jobs:
job1:
runs-on: ubuntu-latest
outputs:
output1: ${{ steps.step1.outputs.test }}
steps:
- id: step1
run: echo "test=hello" >> $GITHUB_OUTPUT
job2:
runs-on: ubuntu-latest
needs: job1
steps:
- run: echo ${{ needs.job1.outputs.output1 }}

成果物管理#

参考: Storing workflow data as artifacts

# アップロード
- 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

jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: production
url: https://example.com
steps:
- run: ./deploy.sh

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

アクションタイプの比較#

参考: About custom actions

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

action.yml の構造#

参考: Metadata syntax

# 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'
# Docker アクション
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.input-name }}
# Composite アクション
name: 'My Composite Action'
description: 'Composite action example'
inputs:
target:
description: 'Target directory'
required: true
outputs:
result:
description: 'Result value'
value: ${{ steps.final.outputs.result }}
runs:
using: 'composite'
steps:
# シェルは必須指定
- run: echo "Step 1"
shell: bash
# 他のアクションも使用可能
- uses: actions/checkout@v4
# working-directory 指定可能
- run: npm install
shell: bash
working-directory: ${{ inputs.target }}
# 環境変数の設定
- run: echo "VALUE=result" >> $GITHUB_OUTPUT
id: final
shell: bash

ワークフローコマンド#

参考: Workflow commands

// 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');
Terminal window
# シェルスクリプト内
echo "output-name=value" >> $GITHUB_OUTPUT
echo "::debug::Debug message"
echo "::error::Error message"
exit 1 # 失敗

Composite Action の使用#

参考: Creating a composite action

# ディレクトリ構造
.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

# 呼び出し側 (.github/workflows/caller.yml)
jobs:
call-workflow:
uses: org/repo/.github/workflows/reusable.yml@main
with:
config: value
secrets:
token: ${{ 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 ${{ inputs.config }}

.github リポジトリ構造#

参考: Creating starter workflows

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

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

参考: About self-hosted runners

Terminal window
# ランナーの設定
./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
# ランナーグループの使用
jobs:
build:
runs-on: [self-hosted, my-runner-group]

シークレットのスコープ#

参考: Using secrets in GitHub Actions

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

頻出キーワード#

デフォルト環境変数#

参考: 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.*: ランナー情報

条件式#

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. 式の構文: ${{ expression }} で囲む
  4. シークレット参照: ${{ secrets.NAME }} のみ($secrets.NAMEは不可)
  5. 配列表記: [item1, item2] または改行して - item1
  6. 複数行文字列: | (改行保持) または > (改行を空白に変換)

試験Tips#

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