|
| 1 | +name: Check Emulator Version |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run daily at midnight UTC |
| 6 | + - cron: '0 0 * * *' |
| 7 | + # Allow manual triggering |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-emulator-version: |
| 12 | + name: Check for new emulator version |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + contents: write |
| 16 | + pull-requests: write |
| 17 | + steps: |
| 18 | + - name: Check out the repo |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Get current version and check for newer versions |
| 22 | + id: version-check |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + script: | |
| 26 | + const fs = require('fs'); |
| 27 | + const { execSync } = require('child_process'); |
| 28 | +
|
| 29 | + // Read the Dockerfile |
| 30 | + const dockerfile = fs.readFileSync('Dockerfile', 'utf8'); |
| 31 | +
|
| 32 | + // Extract the current version |
| 33 | + const versionMatch = dockerfile.match(/FROM gcr\.io\/cloud-spanner-emulator\/emulator:([0-9.]+)/); |
| 34 | + if (!versionMatch) { |
| 35 | + throw new Error('Could not find emulator version in Dockerfile'); |
| 36 | + } |
| 37 | +
|
| 38 | + const currentVersion = versionMatch[1]; |
| 39 | + console.log(`Current version: ${currentVersion}`); |
| 40 | +
|
| 41 | + // Set output for current version |
| 42 | + core.setOutput('current_version', currentVersion); |
| 43 | +
|
| 44 | + // Fetch versions from Google Container Registry |
| 45 | + const response = await fetch('https://gcr.io/v2/cloud-spanner-emulator/emulator/tags/list'); |
| 46 | + const data = await response.json(); |
| 47 | +
|
| 48 | + // Filter for semantic version tags (x.y.z format) |
| 49 | + const allVersions = data.tags.filter(tag => /^[0-9]+\.[0-9]+\.[0-9]+$/.test(tag)); |
| 50 | +
|
| 51 | + // Sort versions |
| 52 | + allVersions.sort((a, b) => { |
| 53 | + const aParts = a.split('.').map(Number); |
| 54 | + const bParts = b.split('.').map(Number); |
| 55 | +
|
| 56 | + for (let i = 0; i < 3; i++) { |
| 57 | + if (aParts[i] !== bParts[i]) { |
| 58 | + return aParts[i] - bParts[i]; |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + return 0; |
| 63 | + }); |
| 64 | +
|
| 65 | + // Find versions newer than current version |
| 66 | + const newerVersions = allVersions.filter(version => { |
| 67 | + const vParts = version.split('.').map(Number); |
| 68 | + const cParts = currentVersion.split('.').map(Number); |
| 69 | +
|
| 70 | + for (let i = 0; i < 3; i++) { |
| 71 | + if (vParts[i] !== cParts[i]) { |
| 72 | + return vParts[i] > cParts[i]; |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + return false; |
| 77 | + }); |
| 78 | +
|
| 79 | + if (newerVersions.length === 0) { |
| 80 | + console.log('No newer versions found.'); |
| 81 | + core.setOutput('has_newer_versions', 'false'); |
| 82 | + } else { |
| 83 | + // Get only the next version (first one after sorting) |
| 84 | + const nextVersion = newerVersions[0]; |
| 85 | + console.log(`Next version found: ${nextVersion}`); |
| 86 | +
|
| 87 | + core.setOutput('has_newer_versions', 'true'); |
| 88 | + core.setOutput('next_version', nextVersion); |
| 89 | +
|
| 90 | + // For backward compatibility |
| 91 | + core.setOutput('newer_versions', nextVersion); |
| 92 | + core.setOutput('latest_version', nextVersion); |
| 93 | + } |
| 94 | +
|
| 95 | + - name: Process next version |
| 96 | + id: process-versions |
| 97 | + if: steps.version-check.outputs.has_newer_versions == 'true' |
| 98 | + env: |
| 99 | + current_version: ${{ steps.version-check.outputs.current_version }} |
| 100 | + next_version: ${{ steps.version-check.outputs.next_version }} |
| 101 | + uses: actions/github-script@v7 |
| 102 | + with: |
| 103 | + script: | |
| 104 | + const fs = require('fs'); |
| 105 | + const { execSync } = require('child_process'); |
| 106 | +
|
| 107 | + const currentVersion = process.env.current_version; |
| 108 | + const nextVersion = process.env.next_version; |
| 109 | +
|
| 110 | + console.log(`Processing next version: ${nextVersion}`); |
| 111 | +
|
| 112 | + let updated = false; |
| 113 | + const failedVersions = {}; |
| 114 | +
|
| 115 | + // Update Dockerfile with the next version |
| 116 | + console.log(`Updating Dockerfile to version ${nextVersion}...`); |
| 117 | + const dockerfile = fs.readFileSync('Dockerfile', 'utf8'); |
| 118 | + const updatedDockerfile = dockerfile.replace( |
| 119 | + new RegExp(`FROM gcr.io/cloud-spanner-emulator/emulator:${currentVersion}`), |
| 120 | + `FROM gcr.io/cloud-spanner-emulator/emulator:${nextVersion}` |
| 121 | + ); |
| 122 | + fs.writeFileSync('Dockerfile', updatedDockerfile); |
| 123 | +
|
| 124 | + // Verify the updated Dockerfile |
| 125 | + console.log(`Verifying updated Dockerfile with version ${nextVersion}...`); |
| 126 | + try { |
| 127 | + execSync('bash verify.bash', { stdio: 'inherit' }); |
| 128 | + console.log(`Verification successful for version ${nextVersion}`); |
| 129 | + updated = true; |
| 130 | + } catch (error) { |
| 131 | + console.log(`Verification failed for version ${nextVersion}`); |
| 132 | + // Mark this version as failed |
| 133 | + failedVersions[`version_${nextVersion}`] = 'failed'; |
| 134 | +
|
| 135 | + // Revert the Dockerfile change |
| 136 | + fs.writeFileSync('Dockerfile', dockerfile); |
| 137 | + } |
| 138 | +
|
| 139 | + if (updated) { |
| 140 | + console.log(`Successfully updated to version ${nextVersion}`); |
| 141 | + core.setOutput('updated', 'true'); |
| 142 | + core.setOutput('updated_version', nextVersion); |
| 143 | + } else { |
| 144 | + console.log('No successful update'); |
| 145 | + core.setOutput('updated', 'false'); |
| 146 | + } |
| 147 | +
|
| 148 | + // Output failed version |
| 149 | + for (const [key, value] of Object.entries(failedVersions)) { |
| 150 | + core.setOutput(key, value); |
| 151 | + } |
| 152 | +
|
| 153 | + - name: Create issue for failed version verification |
| 154 | + if: steps.version-check.outputs.has_newer_versions == 'true' && steps.process-versions.outputs.updated != 'true' |
| 155 | + uses: actions/github-script@v7 |
| 156 | + with: |
| 157 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 158 | + script: | |
| 159 | + const nextVersion = process.env.NEXT_VERSION; |
| 160 | +
|
| 161 | + console.log(`Creating issue for failed version: ${nextVersion}`); |
| 162 | +
|
| 163 | + await github.rest.issues.create({ |
| 164 | + owner: context.repo.owner, |
| 165 | + repo: context.repo.repo, |
| 166 | + title: `Emulator version update verification failed: ${nextVersion}`, |
| 167 | + body: `The verification of the next emulator version ${nextVersion} failed. Please check the [workflow run](${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}) for details.` |
| 168 | + }); |
| 169 | + env: |
| 170 | + NEXT_VERSION: ${{ steps.version-check.outputs.next_version }} |
| 171 | + |
| 172 | + - name: Create Pull Request |
| 173 | + if: steps.process-versions.outputs.updated == 'true' |
| 174 | + uses: peter-evans/create-pull-request@v6 |
| 175 | + with: |
| 176 | + commit-message: "chore: update emulator version to ${{ steps.process-versions.outputs.updated_version }}" |
| 177 | + title: "chore: update emulator version to ${{ steps.process-versions.outputs.updated_version }}" |
| 178 | + body: | |
| 179 | + This PR updates the Cloud Spanner emulator version from ${{ steps.version-check.outputs.current_version }} to ${{ steps.process-versions.outputs.updated_version }}. |
| 180 | +
|
| 181 | + This PR was automatically generated by the check-emulator-version workflow. |
| 182 | + branch: update-emulator-version |
| 183 | + base: master |
| 184 | + delete-branch: true |
0 commit comments