From 912093418e16ca01ae465e5dc0b3e651fedc539f Mon Sep 17 00:00:00 2001 From: Koki Matsumoto Date: Fri, 27 Nov 2026 07:13:00 +0000 Subject: [PATCH] ci: fix tagpr release triggers and container publishing --- .github/workflows/build-container-image.yaml | 71 +++++++++++++++--- .github/workflows/release-golang.yaml | 24 ++++++ .github/workflows/tagpr.yml | 79 +++++++++++++------- 3 files changed, 134 insertions(+), 40 deletions(-) diff --git a/.github/workflows/build-container-image.yaml b/.github/workflows/build-container-image.yaml index 04093c7..baef3d3 100644 --- a/.github/workflows/build-container-image.yaml +++ b/.github/workflows/build-container-image.yaml @@ -6,7 +6,19 @@ on: - main tags: - "v*" + workflow_call: + inputs: + tag: + description: "Release tag to build and publish (e.g. v1.3.0)" + required: true + type: string workflow_dispatch: + inputs: + tag: + description: "Optional release tag to build and publish (e.g. v1.3.0)" + required: false + type: string + default: "" pull_request: types: - opened @@ -22,24 +34,57 @@ on: jobs: build: runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 20 permissions: contents: read - pull-requests: read id-token: write - actions: write packages: write attestations: write steps: + - name: Resolve release context + id: release + env: + INPUT_TAG: ${{ inputs.tag }} + EVENT_NAME: ${{ github.event_name }} + GITHUB_REF: ${{ github.ref }} + GITHUB_REF_NAME: ${{ github.ref_name }} + run: | + if [[ -n "$INPUT_TAG" ]]; then + if [[ ! "$INPUT_TAG" =~ ^v[0-9A-Za-z._+-]+$ ]]; then + echo "::error::Invalid release tag: $INPUT_TAG" + exit 1 + fi + { + echo "is_release=true" + echo "tag=$INPUT_TAG" + echo "ref=refs/tags/$INPUT_TAG" + } >> "$GITHUB_OUTPUT" + elif [[ "$EVENT_NAME" == "push" && "$GITHUB_REF" == refs/tags/v* ]]; then + { + echo "is_release=true" + echo "tag=$GITHUB_REF_NAME" + echo "ref=$GITHUB_REF" + } >> "$GITHUB_OUTPUT" + else + { + echo "is_release=false" + echo "tag=" + echo "ref=" + } >> "$GITHUB_OUTPUT" + fi + - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ steps.release.outputs.ref }} + persist-credentials: false - name: Set up QEMU if: ${{ github.event_name != 'pull_request' }} uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4 - name: Login to GitHub Container Registry - if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') }} + if: ${{ steps.release.outputs.is_release == 'true' }} uses: docker/login-action@v4 with: registry: ghcr.io @@ -51,9 +96,9 @@ jobs: with: images: ghcr.io/${{ github.repository }} tags: | - type=ref,event=tag - type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }} - type=raw,value={{date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}-{{sha}},enable={{is_default_branch}} + type=raw,value=${{ steps.release.outputs.tag || 'untagged' }},enable=${{ steps.release.outputs.is_release == 'true' }} + type=raw,value=latest,enable=${{ steps.release.outputs.is_release == 'true' && !contains(steps.release.outputs.tag, '-') }} + type=raw,value={{date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}-{{sha}},enable=${{ steps.release.outputs.is_release != 'true' && github.event_name == 'push' && github.ref == 'refs/heads/main' }} labels: | org.opencontainers.image.title=GitHub Issue CMS org.opencontainers.image.description=Convert GitHub Issues to Markdown files @@ -67,19 +112,21 @@ jobs: context: . file: ./docker/Dockerfile build-args: | - VERSION=${{ steps.meta.outputs.version }} + VERSION=${{ steps.release.outputs.tag || steps.meta.outputs.version || 'dev' }} platforms: ${{ github.event_name == 'pull_request' && 'linux/amd64' || 'linux/amd64,linux/arm64' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - push: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') }} - provenance: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && 'mode=max' || false }} + push: ${{ steps.release.outputs.is_release == 'true' }} + provenance: ${{ steps.release.outputs.is_release == 'true' && 'mode=max' || false }} cache-from: type=gha,scope=build-container-image cache-to: type=gha,mode=max,scope=build-container-image - name: Display image digest - run: echo ${{ steps.docker_build.outputs.digest }} + env: + IMAGE_DIGEST: ${{ steps.docker_build.outputs.digest }} + run: echo "$IMAGE_DIGEST" - name: Generate artifact attestation - if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') }} + if: ${{ steps.release.outputs.is_release == 'true' }} uses: actions/attest-build-provenance@v4 with: subject-name: ghcr.io/${{ github.repository }} diff --git a/.github/workflows/release-golang.yaml b/.github/workflows/release-golang.yaml index e5fbe20..8a114e9 100644 --- a/.github/workflows/release-golang.yaml +++ b/.github/workflows/release-golang.yaml @@ -4,7 +4,19 @@ on: push: tags: - "v*" + workflow_call: + inputs: + tag: + description: "Release tag to build and publish (e.g. v1.3.0)" + required: true + type: string workflow_dispatch: + inputs: + tag: + description: "Optional release tag to build and publish (e.g. v1.3.0)" + required: false + type: string + default: "" permissions: contents: write @@ -14,10 +26,22 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 20 steps: + - name: Validate release tag + if: ${{ inputs.tag != '' }} + env: + INPUT_TAG: ${{ inputs.tag }} + run: | + if [[ ! "$INPUT_TAG" =~ ^v[0-9A-Za-z._+-]+$ ]]; then + echo "::error::Invalid release tag: $INPUT_TAG" + exit 1 + fi + - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + ref: ${{ inputs.tag != '' && format('refs/tags/{0}', inputs.tag) || '' }} fetch-depth: 0 + persist-credentials: false - name: Set up Go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 diff --git a/.github/workflows/tagpr.yml b/.github/workflows/tagpr.yml index b8d4fe4..51b9127 100644 --- a/.github/workflows/tagpr.yml +++ b/.github/workflows/tagpr.yml @@ -3,20 +3,45 @@ name: Tag and release on: push: branches: [main] + pull_request: + branches: [main] + types: [labeled, unlabeled] + workflow_dispatch: + +concurrency: + group: tagpr + cancel-in-progress: false -permissions: - contents: write - pull-requests: write - issues: read +permissions: {} jobs: tagpr: + if: >- + github.event_name == 'push' || + github.event_name == 'workflow_dispatch' || + ( + github.event_name == 'pull_request' && + github.event.pull_request.state == 'open' && + github.event.pull_request.head.repo.full_name == github.repository && + startsWith(github.head_ref, 'tagpr-from-') && + ( + startsWith(github.event.label.name, 'tagpr:') || + startsWith(github.event.label.name, 'tagpr/') + ) + ) runs-on: ubuntu-latest - timeout-minutes: 30 + timeout-minutes: 15 + permissions: + contents: write + pull-requests: write + issues: read + outputs: + tag: ${{ steps.tagpr.outputs.tag }} steps: - name: Check out release branch uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + ref: main fetch-depth: 0 persist-credentials: false @@ -26,27 +51,25 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # GITHUB_TOKEN-created tags do not trigger a separate tag-push workflow. - - name: Check out created release tag - if: steps.tagpr.outputs.tag != '' - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ steps.tagpr.outputs.tag }} - fetch-depth: 0 - persist-credentials: false + # GITHUB_TOKEN-created tags do not trigger separate tag-push workflows, + # so invoke the GoReleaser and container publishing workflows explicitly. + release-go: + needs: tagpr + if: needs.tagpr.outputs.tag != '' && github.event_name != 'pull_request' + permissions: + contents: write + uses: ./.github/workflows/release-golang.yaml + with: + tag: ${{ needs.tagpr.outputs.tag }} - - name: Set up Go - if: steps.tagpr.outputs.tag != '' - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: go.mod - - - name: Run GoReleaser - if: steps.tagpr.outputs.tag != '' - uses: goreleaser/goreleaser-action@a4f02ab5355a589de60d917a659ce617ed19b3de # v7.3.0 - with: - distribution: goreleaser - version: "~> v2" - args: release --clean - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + release-container: + needs: tagpr + if: needs.tagpr.outputs.tag != '' && github.event_name != 'pull_request' + permissions: + contents: read + id-token: write + packages: write + attestations: write + uses: ./.github/workflows/build-container-image.yaml + with: + tag: ${{ needs.tagpr.outputs.tag }}