Create Tag #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Create Tag | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| tag_name: | |
| description: 'Tag name (e.g., v1.2.3, v1.2.3-beta.1)' | |
| required: true | |
| type: string | |
| permissions: | |
| contents: read # 只需要读权限,推送使用 PAT | |
| jobs: | |
| create-tag: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 关键修改:使用 PAT 来 checkout,后续的 push 才能生效 | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.PAT_FOR_TAG }} # <--- 使用我们刚配置的 secret | |
| - name: Validate tag format | |
| run: | | |
| TAG="${{ github.event.inputs.tag_name }}" | |
| if [[ ! "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+.*$ ]]; then | |
| echo "❌ Tag must start with 'v' and contain at least three numbers (e.g., v1.2.3)" | |
| exit 1 | |
| fi | |
| echo "✅ Tag format is valid." | |
| - name: Check if tag already exists | |
| run: | | |
| TAG="${{ github.event.inputs.tag_name }}" | |
| if git rev-parse "$TAG" >/dev/null 2>&1; then | |
| echo "❌ Tag $TAG already exists locally." | |
| exit 1 | |
| fi | |
| if git ls-remote --tags origin "$TAG" | grep -q "refs/tags/$TAG"; then | |
| echo "❌ Tag $TAG already exists on remote." | |
| exit 1 | |
| fi | |
| echo "✅ Tag does not exist." | |
| - name: Create and push tag using PAT | |
| run: | | |
| TAG="${{ github.event.inputs.tag_name }}" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git tag "$TAG" | |
| git push origin "$TAG" | |
| echo "🚀 Tag $TAG created and pushed successfully." | |
| - name: Done | |
| run: echo "The tag has been pushed and should trigger the 'install' workflow automatically." |