summaryrefslogtreecommitdiff
path: root/.github/workflows/release-tag-helper.yml
blob: 4ccfdb72ba9da3bf7d38d695e260e6ce266e4f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
name: Prepare Release Candidate
run-name: Prepare Release Candidate build/v${{ inputs.version }} (${{ inputs.operation }})

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version without v (X.Y.Z, X.Y.Z-betaN, or X.Y.Z-rcN)"
        required: true
        type: string
      expected_sha:
        description: "Full SHA at the tip of release/X.Y"
        required: true
        type: string
      operation:
        description: "Validate only, or create the immutable candidate tag and start its build"
        required: true
        type: choice
        options: [validate, create]
        default: validate

permissions:
  actions: read
  contents: write

jobs:
  prepare:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    environment: candidate-tagging
    outputs:
      candidate_tag: ${{ steps.validate.outputs.candidate_tag }}
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - name: Fetch release branches and tags
        run: git fetch origin '+refs/heads/release/*:refs/remotes/origin/release/*' '+refs/tags/*:refs/tags/*'
      - name: Validate release commit
        id: validate
        env:
          GH_TOKEN: ${{ github.token }}
          VERSION: ${{ inputs.version }}
          EXPECTED_SHA: ${{ inputs.expected_sha }}
        run: |
          set -euo pipefail
          if [[ ! "$EXPECTED_SHA" =~ ^[0-9a-f]{40}$ ]]; then
            echo "::error::expected_sha must be a full 40-character lowercase commit SHA"
            exit 1
          fi
          IDENTITY=$(python3 .github/scripts/release-policy.py classify "$VERSION")
          BRANCH=$(jq -r .release_branch <<<"$IDENTITY")
          if [ "$GITHUB_SHA" != "$EXPECTED_SHA" ]; then
            echo "::error::Run this workflow from $BRANCH at $EXPECTED_SHA"
            exit 1
          fi
          BRANCH_SHA=$(git rev-parse --verify --quiet "refs/remotes/origin/$BRANCH^{commit}" || true)
          if [ "$BRANCH_SHA" != "$EXPECTED_SHA" ]; then
            echo "::error::$BRANCH tip is ${BRANCH_SHA:-missing}, expected $EXPECTED_SHA"
            exit 1
          fi
          git checkout --detach "$EXPECTED_SHA"
          python3 .github/scripts/release-policy.py validate-source "$VERSION"
          python3 .github/scripts/release-linux-images.py validate \
            release-linux-images.json --check-remote

          CI_RUN=$(gh api "repos/$GITHUB_REPOSITORY/actions/workflows/ci.yml/runs?head_sha=$EXPECTED_SHA&status=completed&per_page=20" \
            --jq '.workflow_runs | sort_by(.created_at) | reverse | .[0]')
          if [ -z "$CI_RUN" ] || [ "$CI_RUN" = null ]; then
            echo "::error::No completed ci.yml run found for $EXPECTED_SHA"
            exit 1
          fi
          CI_ID=$(jq -r .id <<<"$CI_RUN")
          CI_CONCLUSION=$(jq -r .conclusion <<<"$CI_RUN")
          if [ "$CI_CONCLUSION" != success ]; then
            echo "::error::Latest ci.yml run $CI_ID concluded $CI_CONCLUSION"
            exit 1
          fi
          {
            echo "branch=$BRANCH"
            echo "ci_run_id=$CI_ID"
            echo "candidate_tag=build/v$VERSION"
          } >> "$GITHUB_OUTPUT"

      - name: Verify candidate tag is create-only
        id: tag
        env:
          VERSION: ${{ inputs.version }}
          EXPECTED_SHA: ${{ inputs.expected_sha }}
        run: |
          .github/scripts/check-candidate-tag.sh "build/v$VERSION" "$EXPECTED_SHA" >> "$GITHUB_OUTPUT"

      - name: Create candidate tag
        if: inputs.operation == 'create' && steps.tag.outputs.exists != 'true'
        env:
          VERSION: ${{ inputs.version }}
          EXPECTED_SHA: ${{ inputs.expected_sha }}
        run: |
          git tag "build/v$VERSION" "$EXPECTED_SHA"
          git push origin "refs/tags/build/v$VERSION:refs/tags/build/v$VERSION"

      - name: Summarize candidate preparation
        env:
          VERSION: ${{ inputs.version }}
          EXPECTED_SHA: ${{ inputs.expected_sha }}
          OPERATION: ${{ inputs.operation }}
          BRANCH: ${{ steps.validate.outputs.branch }}
          CI_RUN_ID: ${{ steps.validate.outputs.ci_run_id }}
        run: |
          {
            echo "## Release candidate preparation"
            echo
            echo "- Version: \`$VERSION\`"
            echo "- Commit: \`$EXPECTED_SHA\`"
            echo "- Release branch: \`$BRANCH\`"
            echo "- CI run: [\`$CI_RUN_ID\`]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$CI_RUN_ID)"
            echo "- Operation: \`$OPERATION\`"
          } >> "$GITHUB_STEP_SUMMARY"

  candidate:
    if: inputs.operation == 'create'
    needs: prepare
    permissions:
      contents: read
      packages: read
    uses: ./.github/workflows/release.yml
    with:
      candidate_tag: ${{ needs.prepare.outputs.candidate_tag }}
      expected_sha: ${{ inputs.expected_sha }}