Marketplaceのアクション活用
ストーリー
「全部自分で書く必要はない。Marketplaceには何千ものアクションが公開されている」
木村先輩がGitHub Marketplaceのページを開いた。
「チェックアウト、言語セットアップ、キャッシュ、デプロイ...... よく使う処理はほとんどアクションとして用意されている。 ただし、サードパーティのアクションを使う時は セキュリティの観点を忘れるなよ」
「セキュリティ......何か気をつけることがあるんですか?」
「信頼できるアクションの選び方を教えよう」
GitHub Marketplaceとは
GitHub Marketplaceは、GitHub Actionsで使える再利用可能なアクションを公開・検索できるプラットフォームです。
Marketplace の構成
┌──── GitHub Marketplace ────┐
│ │
│ 公式アクション (GitHub製) │
│ ├── actions/checkout │
│ ├── actions/setup-node │
│ ├── actions/cache │
│ └── actions/upload-artifact │
│ │
│ 認証済みアクション │
│ ├── aws-actions/* │
│ ├── google-github-actions/* │
│ └── docker/* │
│ │
│ コミュニティアクション │
│ ├── 数千のアクション │
│ └── 品質はさまざま │
│ │
└──────────────────────────────┘
必須アクション Top 10
1. actions/checkout
リポジトリのコードをチェックアウトします。ほぼ全てのワークフローで使用します。
yaml
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # 全履歴を取得(デフォルトは1=最新のみ)
ref: ${{ github.head_ref }} # PRのブランチをチェックアウト2. actions/setup-node
Node.js環境をセットアップします。
yaml
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # npm キャッシュを自動管理
registry-url: 'https://npm.pkg.github.com' # プライベートレジストリ3. actions/cache
依存関係やビルド成果物をキャッシュします。
yaml
- name: Cache node_modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-4. actions/upload-artifact / download-artifact
ジョブ間でファイルを受け渡します。
yaml
# アップロード
- name: Upload build
uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/
retention-days: 7
# ダウンロード(別のジョブで)
- name: Download build
uses: actions/download-artifact@v4
with:
name: build-output
path: dist/5. actions/github-script
GitHub APIをJavaScriptで直接呼び出します。
yaml
- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'CI passed! Ready for review.'
})言語/フレームワーク別のセットアップアクション
| 言語 | アクション | 用途 |
|---|---|---|
| Node.js | actions/setup-node@v4 | Node.js環境 |
| Python | actions/setup-python@v5 | Python環境 |
| Go | actions/setup-go@v5 | Go環境 |
| Java | actions/setup-java@v4 | JDK環境 |
| .NET | actions/setup-dotnet@v4 | .NET SDK |
| Rust | dtolnay/rust-toolchain@stable | Rustツールチェイン |
Pythonのセットアップ例
yaml
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- name: Install dependencies
run: pip install -r requirements.txtデプロイ系アクション
AWS関連
yaml
# AWSクレデンシャルの設定
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-1
# ECR へのログイン
- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2
# ECS へのデプロイ
- name: Deploy to ECS
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: task-definition.json
service: my-service
cluster: my-clusterDocker関連
yaml
# Docker Hub へのログイン
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Docker イメージのビルド & プッシュ
- name: Build and push
uses: docker/build-push-action@v6
with:
push: true
tags: myapp:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=maxセキュリティのベストプラクティス
信頼できるアクションの選び方
| チェックポイント | 安全 | 注意 |
|---|---|---|
| 提供元 | actions/* (GitHub公式) | 個人リポジトリ |
| バッジ | Verified Creator | なし |
| スター数 | 1000+ | 10未満 |
| メンテナンス | 最近更新あり | 1年以上放置 |
| ソースコード | 公開されている | 非公開 |
バージョン固定の方法
yaml
# 良い: フルコミットSHAで固定(最も安全)
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# 許容: メジャーバージョンタグで固定
- uses: actions/checkout@v4
# 危険: ブランチ指定(内容が変わる可能性)
- uses: actions/checkout@main # 避けるpermissionsの明示指定
yaml
# ワークフローレベルで最小権限を設定
permissions:
contents: read # リポジトリの読み取りのみ
pull-requests: write # PRへのコメント書き込み
# デフォルトを最小にして、ジョブごとに必要な権限を追加
permissions: {} # 全て無効化
jobs:
build:
permissions:
contents: read # このジョブに必要な権限のみ
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4カスタムアクションの作成
自分でアクションを作ることもできます。
Compositeアクション(最も簡単)
yaml
# .github/actions/setup-project/action.yml
name: 'Setup Project'
description: 'プロジェクトのセットアップを行う'
inputs:
node-version:
description: 'Node.js version'
required: false
default: '20'
runs:
using: 'composite'
steps:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: 'npm'
- name: Install dependencies
shell: bash
run: npm ci
- name: Verify installation
shell: bash
run: npm --version && node --versionyaml
# ワークフローでの使用
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-project
with:
node-version: '20'
- run: npm testまとめ
| ポイント | 内容 |
|---|---|
| Marketplace | 再利用可能なアクションの検索・利用プラットフォーム |
| 必須アクション | checkout, setup-node, cache, upload-artifact |
| セキュリティ | SHA固定、permissions指定、信頼できる提供元を確認 |
| カスタムアクション | Compositeアクションで自前の処理を再利用化 |
チェックリスト
- Marketplace でアクションを検索し、使用方法を理解した
- 主要な公式アクション(checkout, setup-node, cache等)の使い方を把握した
- サードパーティアクションのセキュリティリスクを理解した
- Compositeアクションの作成方法を理解した
次のステップへ
次のセクションでは、学んだ知識を総動員して、初めてのCI/CDワークフローを実際に構築する演習に挑戦します。
推定読了時間: 30分