GitHub Actions Certificate (GH-200) チートシート
GitHub Actions Certificate 試験のための簡潔なチートシートです、試験受けた当日につくったのでかなりホヤホヤです。 4つのドメインごとに重要な構文とコマンドを整理しています。
概要#
GitHub Actions Certificate 試験のための簡潔なチートシートです、試験受けた当日につくったのでかなりホヤホヤです。 4つのドメインごとに重要な構文とコマンドを整理しています。
チートシートそのものは AI にほぼほぼまとめてもらってます。普段から GitHub Actions 使ってる人であればチートシートの内容が理解できると思いますし、特に苦せず受かるかな?という気がします
ドメイン1: ワークフローの作成と管理 (40%)#
イベントトリガー#
# 単一イベント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'ワークフローコンポーネント#
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"特定目的のワークフロー#
# サービスコンテナ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%)#
デバッグ#
# ステップデバッグを有効化# リポジトリシークレットに 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"キャッシュ#
- name: Cache dependencies uses: actions/cache@v3 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node-ジョブ間のデータ受け渡し#
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 }}成果物管理#
# アップロード- 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環境保護#
jobs: deploy: runs-on: ubuntu-latest environment: name: production url: https://example.com steps: - run: ./deploy.shドメイン3: アクションの作成と管理 (25%)#
アクションタイプの比較#
| タイプ | 実行環境 | 速度 | 柔軟性 | 用途 |
|---|---|---|---|---|
| JavaScript | Node.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ワークフローコマンド#
// 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');# シェルスクリプト内echo "output-name=value" >> $GITHUB_OUTPUTecho "::debug::Debug message"echo "::error::Error message"exit 1 # 失敗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@v1Composite Action の注意点#
shellは各ステップで必須指定working-directoryで作業ディレクトリを変更可能outputsはsteps.<step_id>.outputs.<output_name>で参照- 他のアクション(
uses)も使用可能 - ワークフローコマンド(
$GITHUB_OUTPUT等)も使用可能
ドメイン4: エンタープライズ管理 (15%)#
再利用可能なワークフロー#
# 呼び出し側 (.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 リポジトリ構造#
.github/├── workflow-templates/│ ├── ci.yml # ワークフローテンプレート│ └── ci.properties.json # メタデータ└── workflows/ └── organization-workflow.ymlセルフホステッドランナー#
# ランナーの設定./config.sh --url https://github.com/org/repo --token TOKEN
# プロキシ設定export HTTP_PROXY=http://proxy:8080export HTTPS_PROXY=http://proxy:8080
# ラベルの設定./config.sh --labels "linux,x64,gpu"
# サービスとして実行sudo ./svc.sh installsudo ./svc.sh start# ランナーグループの使用jobs: build: runs-on: [self-hosted, my-runner-group]シークレットのスコープ#
| スコープ | 範囲 | 優先度 | 用途 |
|---|---|---|---|
| Organization | 組織内の複数リポジトリ | 低 | 共通の認証情報 |
| Repository | 特定リポジトリ | 中 | リポジトリ固有の設定 |
| Environment | 特定環境 | 高 | 環境別の認証情報 |
# Environment シークレットの使用jobs: deploy: environment: production # production環境のシークレットを使用 steps: - run: echo ${{ secrets.PROD_TOKEN }}頻出キーワード#
デフォルト環境変数#
GITHUB_SHA: コミットSHAGITHUB_REF: ブランチまたはタグのrefGITHUB_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構文の注意点#
- インデント: スペース2つが標準(タブは使用不可)
- 階層構造:
jobs.<job_id>.stepsの形式を正確に - 式の構文:
${{ expression }}で囲む - シークレット参照:
${{ secrets.NAME }}のみ($secrets.NAMEは不可) - 配列表記:
[item1, item2]または改行して- item1 - 複数行文字列:
|(改行保持) または>(改行を空白に変換)
試験Tips#
- 英語モード推奨: 日本語の翻訳品質に問題あり
- YAML構文: インデントとキーワードのスペルに注意
- 実践経験: 実際にワークフローを書いた経験が重要
- Enterprise機能:
.githubリポジトリとランナーグループを重点的に - デバッグ:
ACTIONS_STEP_DEBUGとログコマンドを覚える