|
| 1 | +name: Tag PR on label |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [labeled] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write # to create/push tags |
| 9 | + pull-requests: write # to remove labels / comment |
| 10 | + |
| 11 | +env: |
| 12 | + LABEL_NAME: tag-e2e |
| 13 | + POST_COMMENT: "true" |
| 14 | + |
| 15 | +jobs: |
| 16 | + tag-on-label: |
| 17 | + if: github.event.label.name == 'tag-e2e' |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout (full history for tags) |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + |
| 26 | + - name: Compute next tag |
| 27 | + id: compute |
| 28 | + shell: bash |
| 29 | + run: | |
| 30 | + set -euo pipefail |
| 31 | +
|
| 32 | + PR_NUMBER="${{ github.event.pull_request.number }}" |
| 33 | +
|
| 34 | + # Take everything before the first slash (or whole branch if no slash) |
| 35 | + PREFIX="test-${PR_NUMBER}" |
| 36 | +
|
| 37 | + # Ensure we know about remote tags |
| 38 | + git fetch --tags origin |
| 39 | +
|
| 40 | + # List existing matching tags from remote |
| 41 | + EXISTING_TAGS="$(git ls-remote --tags origin "${PREFIX}-*" | awk -F'refs/tags/' '{print $2}')" |
| 42 | +
|
| 43 | + NEXT=1 |
| 44 | + if [[ -n "${EXISTING_TAGS}" ]]; then |
| 45 | + MAX=0 |
| 46 | + # Extract numeric suffix and find max |
| 47 | + while IFS= read -r TAG; do |
| 48 | + NUM="${TAG##*-}" |
| 49 | + if [[ "$NUM" =~ ^[0-9]+$ ]] && (( NUM > MAX )); then |
| 50 | + MAX="$NUM" |
| 51 | + fi |
| 52 | + done <<< "$EXISTING_TAGS" |
| 53 | + NEXT=$((MAX + 1)) |
| 54 | + fi |
| 55 | +
|
| 56 | + NEW_TAG="${PREFIX}-${NEXT}" |
| 57 | + echo "new_tag=${NEW_TAG}" >> "$GITHUB_OUTPUT" |
| 58 | +
|
| 59 | + - name: Create and push tag |
| 60 | + shell: bash |
| 61 | + run: | |
| 62 | + set -euo pipefail |
| 63 | + git config user.name "github-actions[bot]" |
| 64 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 65 | + git tag "${{ steps.compute.outputs.new_tag }}" |
| 66 | + git push origin "${{ steps.compute.outputs.new_tag }}" |
| 67 | +
|
| 68 | + - name: Remove triggering label |
| 69 | + if: always() |
| 70 | + shell: bash |
| 71 | + env: |
| 72 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + run: | |
| 74 | + gh issue edit ${{ github.event.pull_request.number }} \ |
| 75 | + --remove-label "${LABEL_NAME}" \ |
| 76 | + --repo "${{ github.repository }}" |
| 77 | +
|
| 78 | + - name: Comment with created tag (optional) |
| 79 | + if: env.POST_COMMENT == 'true' |
| 80 | + shell: bash |
| 81 | + env: |
| 82 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 83 | + run: | |
| 84 | + gh pr comment ${{ github.event.pull_request.number }} \ |
| 85 | + --repo "${{ github.repository }}" \ |
| 86 | + --body "Created tag \`${{ steps.compute.outputs.new_tag }}\`." |
0 commit comments