🐾
GitHub Actionsでタグとリリースを作成する
概要
GitHub ActionsのWorkflowを使用してリリースタグを生成し、リリースノートを作成します。
実行環境
- ubuntu-latest
ワークフロー
ワークフロー全体
name: release
on:
workflow_dispatch:
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: set git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: fetch tags
run: git fetch --tags --force
- name: create and push next tag
id: create_push_next_tag
run: |
NEXT_TAG="$(date +'%Y%m%d')"
echo "next_tag=${NEXT_TAG}" >> "$GITHUB_OUTPUT"
echo "Next tag: ${NEXT_TAG}"
git fetch origin main
git tag -a "${NEXT_TAG}" -m "${NEXT_TAG}"
git push origin "${NEXT_TAG}"
echo "created=true" >> "$GITHUB_OUTPUT"
- name: create release
if: steps.create_push_next_tag.outputs.created == 'true'
uses: actions/github-script@v8
env:
TAG: ${{ steps.create_push_next_tag.outputs.next_tag }}
with:
script: |
const tag = process.env.TAG
const res = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tag,
name: tag,
target_commitish: 'main',
generate_release_notes: true,
draft: false,
prerelease: false
});
core.setOutput('html_url', res.data.html_url);
result-encoding: string
タグをpushするには contents: write 権限が必要です。
Gitの設定
- name: set git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
公式ドキュメント推奨設定を参考にしています。
タグ設定
- name: create and push next tag
id: create_push_next_tag
run: |
NEXT_TAG="$(date +'%Y%m%d')"
echo "next_tag=${NEXT_TAG}" >> "$GITHUB_OUTPUT"
echo "Next tag: ${NEXT_TAG}"
git fetch origin main
git tag -a "${NEXT_TAG}" -m "${NEXT_TAG}"
git push origin "${NEXT_TAG}"
echo "created=true" >> "$GITHUB_OUTPUT"
yyyymmdd 形式のタグをプッシュします。
リリース作成
- name: create release
if: steps.create_push_next_tag.outputs.created == 'true'
uses: actions/github-script@v8
env:
TAG: ${{ steps.create_push_next_tag.outputs.next_tag }}
with:
script: |
const tag = process.env.TAG
const res = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tag,
name: tag,
target_commitish: 'main',
generate_release_notes: true,
draft: false,
prerelease: false
});
core.setOutput('html_url', res.data.html_url);
result-encoding: string
指定タグを対象にリリース作成を作成します。
-
target_commitish: 'main': mainブランチを対象とします -
generate_release_notes: true: GitHub標準の自動生成機能を使用してリリースノートを生成します -
draft: false: 下書きを無効にします -
prerelease: false: プレリリースを無効にします
タグ作成が成功した場合のみリリースが実行されます。TAG はoutputsから受け取ったタグを使用します。
エラー時
タグ生成に失敗すると created が出力されないためリリース作成はスキップされます。
Discussion