|
| 1 | +name: Update SQLDef Version |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: 'New SQLDef version (e.g., v3.0.2)' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + repository_dispatch: |
| 11 | + types: [sqldef-release] |
| 12 | + # Expected payload: { "version": "v3.0.2" } |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: write |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + update-version: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v5 |
| 23 | + - name: Generate GitHub App token |
| 24 | + id: app-token |
| 25 | + uses: actions/create-github-app-token@v2 |
| 26 | + with: |
| 27 | + app-id: ${{ secrets.GH_APP_ID }} |
| 28 | + private-key: ${{ secrets.GH_APP_PRIVATE_KEY }} |
| 29 | + permission-contents: write |
| 30 | + permission-pull-requests: write |
| 31 | + |
| 32 | + - name: Determine and validate version |
| 33 | + id: version |
| 34 | + env: |
| 35 | + INPUT_VERSION: ${{ inputs.version }} |
| 36 | + PAYLOAD_VERSION: ${{ github.event.client_payload.version }} |
| 37 | + EVENT_NAME: ${{ github.event_name }} |
| 38 | + run: | |
| 39 | + # Use input version if triggered manually, otherwise use the payload version |
| 40 | + if [ "$EVENT_NAME" = "workflow_dispatch" ]; then |
| 41 | + VERSION="$INPUT_VERSION" |
| 42 | + else |
| 43 | + VERSION="$PAYLOAD_VERSION" |
| 44 | + fi |
| 45 | +
|
| 46 | + # Check if version is provided |
| 47 | + if [ -z "$VERSION" ]; then |
| 48 | + echo "Error: No version provided" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +
|
| 52 | + # Ensure version starts with 'v' |
| 53 | + if [[ ! "$VERSION" =~ ^v ]]; then |
| 54 | + VERSION="v$VERSION" |
| 55 | + fi |
| 56 | +
|
| 57 | + # Validate version format (v1.2.3 with optional pre-release/build metadata) |
| 58 | + if [[ ! "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9._-]+)?$ ]]; then |
| 59 | + echo "Error: Invalid version format: $VERSION" |
| 60 | + echo "Expected format: vX.Y.Z or vX.Y.Z-suffix (e.g., v3.0.1, v3.0.1-beta, v3.0.1-rc1)" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | +
|
| 64 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 65 | + echo "Validated version: $VERSION" |
| 66 | +
|
| 67 | + - name: Create or switch to branch |
| 68 | + env: |
| 69 | + VERSION: ${{ steps.version.outputs.version }} |
| 70 | + run: | |
| 71 | + BRANCH_NAME="update-sqldef-version-$VERSION" |
| 72 | +
|
| 73 | + # Check if branch exists locally or remotely |
| 74 | + if git show-ref --verify --quiet "refs/heads/$BRANCH_NAME"; then |
| 75 | + echo "Branch exists locally, switching to it" |
| 76 | + git checkout "$BRANCH_NAME" |
| 77 | + elif git ls-remote --exit-code --heads origin "$BRANCH_NAME"; then |
| 78 | + echo "Branch exists remotely, checking it out" |
| 79 | + git checkout -b "$BRANCH_NAME" "origin/$BRANCH_NAME" |
| 80 | + else |
| 81 | + echo "Creating new branch" |
| 82 | + git checkout -b "$BRANCH_NAME" |
| 83 | + fi |
| 84 | +
|
| 85 | + echo "branch=$BRANCH_NAME" >> $GITHUB_ENV |
| 86 | +
|
| 87 | + - name: Update version in action.yml |
| 88 | + env: |
| 89 | + VERSION: ${{ steps.version.outputs.version }} |
| 90 | + run: | |
| 91 | + # Store current version for comparison |
| 92 | + CURRENT_VERSION=$(grep -A 1 "description: 'Version of sqldef to use'" action.yml | grep "default:" | sed "s/.*default: '\(v[0-9.]*[^']*\)'.*/\1/") |
| 93 | +
|
| 94 | + if [ "$CURRENT_VERSION" = "$VERSION" ]; then |
| 95 | + echo "Version is already set to $VERSION, no changes needed" |
| 96 | + echo "changed=false" >> $GITHUB_ENV |
| 97 | + else |
| 98 | + echo "Updating from $CURRENT_VERSION to $VERSION" |
| 99 | +
|
| 100 | + # Update the default version in action.yml (more precise sed pattern) |
| 101 | + sed -i "s/\(default: '\)v[0-9]\+\.[0-9]\+\.[0-9]\+\(-[a-zA-Z0-9._-]\+\)\?\('/\1$VERSION\3/" action.yml |
| 102 | +
|
| 103 | + # Verify the change |
| 104 | + echo "Updated action.yml:" |
| 105 | + grep -A 1 "description: 'Version of sqldef to use'" action.yml |
| 106 | + echo "changed=true" >> $GITHUB_ENV |
| 107 | + fi |
| 108 | +
|
| 109 | + - name: Configure Git |
| 110 | + if: env.changed == 'true' |
| 111 | + run: | |
| 112 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 113 | + git config --local user.name "github-actions[bot]" |
| 114 | +
|
| 115 | + - name: Commit changes |
| 116 | + if: env.changed == 'true' |
| 117 | + env: |
| 118 | + VERSION: ${{ steps.version.outputs.version }} |
| 119 | + run: | |
| 120 | + git add action.yml |
| 121 | +
|
| 122 | + # Check if there are changes to commit |
| 123 | + if git diff --cached --quiet; then |
| 124 | + echo "No changes to commit" |
| 125 | + echo "has_changes=false" >> $GITHUB_ENV |
| 126 | + else |
| 127 | + git commit -m "chore: update SQLDef version to $VERSION" |
| 128 | + echo "has_changes=true" >> $GITHUB_ENV |
| 129 | + fi |
| 130 | +
|
| 131 | + - name: Push branch |
| 132 | + if: env.changed == 'true' && env.has_changes == 'true' |
| 133 | + env: |
| 134 | + BRANCH: ${{ env.branch }} |
| 135 | + run: | |
| 136 | + git push origin "$BRANCH" |
| 137 | +
|
| 138 | + - name: Create or update Pull Request |
| 139 | + if: env.changed == 'true' && env.has_changes == 'true' |
| 140 | + uses: actions/github-script@v8 |
| 141 | + env: |
| 142 | + VERSION: ${{ steps.version.outputs.version }} |
| 143 | + BRANCH: ${{ env.branch }} |
| 144 | + with: |
| 145 | + github-token: ${{ steps.app-token.outputs.token }} |
| 146 | + script: | |
| 147 | + const version = process.env.VERSION; |
| 148 | + const branch = process.env.BRANCH; |
| 149 | + const owner = context.repo.owner; |
| 150 | + const repo = context.repo.repo; |
| 151 | +
|
| 152 | + // Check if PR already exists |
| 153 | + const { data: existingPRs } = await github.rest.pulls.list({ |
| 154 | + owner, |
| 155 | + repo, |
| 156 | + head: `${owner}:${branch}`, |
| 157 | + state: 'open' |
| 158 | + }); |
| 159 | +
|
| 160 | + if (existingPRs.length > 0) { |
| 161 | + const pr = existingPRs[0]; |
| 162 | + console.log(`Pull request already exists: #${pr.number}`); |
| 163 | + console.log(`URL: ${pr.html_url}`); |
| 164 | +
|
| 165 | + // Update PR body if needed |
| 166 | + await github.rest.pulls.update({ |
| 167 | + owner: owner, |
| 168 | + repo: repo, |
| 169 | + pull_number: pr.number, |
| 170 | + body: `## Description\n\nThis PR automatically updates the default SQLDef version to \`${version}\`.\n\n### Changes\n- Updated default version in \`action.yml\` to \`${version}\`\n\n### Triggered by\n${context.eventName === 'workflow_dispatch' ? '- Manual workflow dispatch' : '- SQLDef release workflow'}\n\n---\n*This PR was automatically created by the update-version workflow.*` |
| 171 | + }); |
| 172 | +
|
| 173 | + console.log('Pull request description updated'); |
| 174 | + } else { |
| 175 | + // Create new PR |
| 176 | + const { data: pr } = await github.rest.pulls.create({ |
| 177 | + owner: owner, |
| 178 | + repo: repo, |
| 179 | + title: `chore: update SQLDef version to ${version}`, |
| 180 | + body: `## Description\n\nThis PR automatically updates the default SQLDef version to \`${version}\`.\n\n### Changes\n- Updated default version in \`action.yml\` to \`${version}\`\n\n### Triggered by\n${context.eventName === 'workflow_dispatch' ? '- Manual workflow dispatch' : '- SQLDef release workflow'}\n\n---\n*This PR was automatically created by the update-version workflow.*`, |
| 181 | + head: branch, |
| 182 | + base: 'main', |
| 183 | + draft: false |
| 184 | + }); |
| 185 | +
|
| 186 | + console.log(`Pull request created: #${pr.html_url}`); |
| 187 | + } |
0 commit comments