Compile versions #830
This file contains 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: 'Compile versions' | |
on: | |
schedule: | |
- cron: '0 */12 * * *' | |
workflow_dispatch: | |
concurrency: | |
group: '${{ github.workflow }}-${{ github.head_ref || github.ref }}' | |
cancel-in-progress: true | |
env: | |
PR_BRANCH: 'actions/gcloud-versions' | |
jobs: | |
compile-versions: | |
runs-on: 'ubuntu-latest' | |
steps: | |
- uses: 'actions/checkout@v4' | |
- uses: 'actions/setup-go@v4' | |
with: | |
go-version: '1.20' | |
- name: 'Get versions list' | |
run: |- | |
mkdir -p ./data | |
go run ./cmd/compile-versions/main.go > ./data/versions.json | |
echo "Computed versions:" | |
cat ./data/versions.json | |
- id: 'updates' | |
name: 'Update versions list' | |
run: |- | |
if git diff --exit-code ./data/versions.json; then | |
echo "No changes" | |
exit 0 | |
fi | |
echo "had_changes=true" >> ${GITHUB_OUTPUT} | |
git config user.name "google-github-actions-bot" | |
git config user.email "[email protected]" | |
git checkout -b "${{ env.PR_BRANCH }}" | |
git add ./data/versions.json | |
git commit -m "Update gcloud versions ($(date --iso-8601=h))" | |
git push -f origin "${{ env.PR_BRANCH }}" | |
- name: 'Create Pull Request' | |
if: steps.updates.outputs.had_changes == 'true' | |
uses: 'actions/github-script@v6' | |
with: | |
github-token: '${{ secrets.ACTIONS_BOT_TOKEN }}' | |
script: |- | |
const baseBranch = `${{ github.ref_name }}`; | |
const prBranch = `${{ env.PR_BRANCH }}`; | |
const prBody = `Update latest gcloud versions`; | |
try { | |
const listResponse = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: `open`, | |
head: `${context.repo.owner}:${prBranch}`, | |
base: baseBranch, | |
}); | |
core.isDebug() && console.log(listResponse); | |
if(!listResponse.data.length) { | |
const createResponse = await github.rest.pulls.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
title: `Update gcloud versions`, | |
body: prBody, | |
head: prBranch, | |
base: baseBranch, | |
}); | |
core.info(`Created PR #${createResponse.data.number} at ${createResponse.data.html_url}`); | |
} else { | |
const updateResponse = await github.rest.pulls.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: listResponse.data[0].number, | |
title: `Update gcloud versions`, | |
body: prBody, | |
}); | |
core.info(`Updated PR #${updateResponse.data.number} at ${updateResponse.data.html_url}`); | |
} | |
} catch(err) { | |
console.error(err); | |
core.setFailed(`Failed to create pull request: ${err}`); | |
} |