|
| 1 | +name: Create Linked Tag from Upstream Repo |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Runs 4 times a day (every 6 hours) |
| 6 | + - cron: '0 */6 * * *' |
| 7 | + |
| 8 | + # A simple manual trigger for testing. |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +permissions: |
| 12 | + # 'contents: write' is required to push tags to the repository. |
| 13 | + contents: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + sync-tag: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Get latest stable release from public upstream repo |
| 21 | + id: get_release_b |
| 22 | + uses: pozetroninc/github-action-get-latest-release@master |
| 23 | + with: |
| 24 | + # Reads the UPSTREAM_REPO variable from your repository settings. |
| 25 | + # This variable is REQUIRED for the workflow to function. |
| 26 | + repository: ${{ vars.UPSTREAM_REPO }} |
| 27 | + excludes: prerelease, draft |
| 28 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + |
| 30 | + - name: Validate Release Tag Format |
| 31 | + id: validate_tag |
| 32 | + env: |
| 33 | + # Use the repo variable for the regex if it exists, otherwise use the default. |
| 34 | + VERSION_REGEX: ${{ vars.VERSION_REGEX || '^v?[0-9]+\.[0-9]+\.[0-9]+$' }} |
| 35 | + run: | |
| 36 | + TAG_NAME="${{ steps.get_release_b.outputs.release }}" |
| 37 | +
|
| 38 | + echo "Validating release tag format for latest release from upstream repository: $TAG_NAME" |
| 39 | + if [[ -z "$TAG_NAME" ]]; then |
| 40 | + echo "No valid release found from upstream repository. Exiting." |
| 41 | + echo "is_valid=false" >> "$GITHUB_OUTPUT" |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +
|
| 45 | + if [[ "$TAG_NAME" =~ $VERSION_REGEX ]]; then |
| 46 | + echo "Tag '$TAG_NAME' matches the required format." |
| 47 | + echo "is_valid=true" >> "$GITHUB_OUTPUT" |
| 48 | + else |
| 49 | + echo "Tag '$TAG_NAME' does not match the required format. Ignoring." |
| 50 | + echo "is_valid=false" >> "$GITHUB_OUTPUT" |
| 51 | + fi |
| 52 | +
|
| 53 | + - name: Generate GitHub App token |
| 54 | + id: app_token |
| 55 | + uses: actions/create-github-app-token@v2 |
| 56 | + with: |
| 57 | + app-id: ${{ secrets.WORKFLOW_APP_ID }} |
| 58 | + private-key: ${{ secrets.WORKFLOW_APP_PRIVATE_KEY }} |
| 59 | + |
| 60 | + - name: Checkout repository code |
| 61 | + # We need to check out the code to be able to check for existing tags and push new ones. |
| 62 | + if: steps.validate_tag.outputs.is_valid == 'true' |
| 63 | + uses: actions/checkout@v5 |
| 64 | + with: |
| 65 | + fetch-depth: 0 |
| 66 | + fetch-tags: true |
| 67 | + token: ${{ steps.app_token.outputs.token }} |
| 68 | + |
| 69 | + - name: Check if tag already exists locally |
| 70 | + id: check_tag |
| 71 | + if: steps.validate_tag.outputs.is_valid == 'true' |
| 72 | + env: |
| 73 | + RELEASE_TAG: ${{ steps.get_release_b.outputs.release }} |
| 74 | + run: | |
| 75 | + if git tag --list | grep -q "^${RELEASE_TAG}$"; then |
| 76 | + echo "Tag '$RELEASE_TAG' already exists. No action needed." |
| 77 | + echo "create_tag=false" >> "$GITHUB_OUTPUT" |
| 78 | + else |
| 79 | + echo "New valid tag '$RELEASE_TAG' detected!" |
| 80 | + echo "create_tag=true" >> "$GITHUB_OUTPUT" |
| 81 | + fi |
| 82 | +
|
| 83 | + - name: Create and push new tag |
| 84 | + # This step only runs if the tag is valid AND new. |
| 85 | + if: steps.check_tag.outputs.create_tag == 'true' |
| 86 | + env: |
| 87 | + RELEASE_TAG: ${{ steps.get_release_b.outputs.release }} |
| 88 | + run: | |
| 89 | + RELEASE_TAG="v${RELEASE_TAG#v}" # Ensure the tag starts with 'v' |
| 90 | +
|
| 91 | + echo "Creating tag: $RELEASE_TAG" |
| 92 | + git tag "$RELEASE_TAG" |
| 93 | +
|
| 94 | + echo "Pushing tag to remote..." |
| 95 | + git push origin "$RELEASE_TAG" |
| 96 | +
|
| 97 | + echo "Successfully created and pushed tag '$RELEASE_TAG'." |
0 commit comments