This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
Check for newer base image every night #181
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: Check for newer base image every night | |
on: | |
schedule: | |
- cron: "0 0 * * *" | |
jobs: | |
compare-base-image-tag: | |
permissions: write-all | |
runs-on: ubuntu-22.04 | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Compare base image tag | |
id: compare | |
run: | | |
# Get current base image tag | |
current_tag=$(grep 'ARG STEAMRT_PLATFORM_VERSION' image/base/Dockerfile | cut -d '=' -f 2) | |
# Get current base image repository | |
current_repository=$(sed -rn '/FROM /p' image/base/Dockerfile | cut -d ' ' -f 2 | cut -d ':' -f 1) | |
# Use crane to list all available remote tags based on the base image tag prefix | |
# https://github.com/google/go-containerregistry/tree/main/cmd/crane | |
remote_tags=$(docker run --rm gcr.io/go-containerregistry/crane:v0.15.2 ls ${current_repository} | grep -v -e '[[a-z]]*') | |
# Compare current tag to remote tags | |
new_tag=$(python3 ./scripts/compare_tags.py "${current_tag}" "${remote_tags}") | |
if [[ ! -z "${new_tag}" ]]; then | |
echo "CI_PR_OLD_BASE_IMAGE_TAG=${current_tag}" >> $GITHUB_ENV | |
echo "CI_PR_NEW_BASE_IMAGE_TAG=${new_tag}" >> $GITHUB_ENV | |
echo "createpr=1" >> $GITHUB_OUTPUT | |
else | |
echo "No newer tag found." | |
echo "createpr=0" >> $GITHUB_OUTPUT | |
fi | |
- name: Check if pull request exists for newer base image | |
if: ${{ steps.compare.outputs.createpr == 1 }} | |
id: checkpr | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const { repo, owner } = context.repo; | |
const result = await github.rest.pulls.list({ | |
owner, | |
repo, | |
head: 'actions/bump-base-image', | |
base: 'main', | |
state: 'open' | |
}); | |
if (result.length > 0) | |
{ | |
return 'skip' | |
} | |
return 'continue' | |
result-encoding: string | |
- name: Push new branch with updated base image | |
if: ${{ steps.checkpr.outputs.result == 'continue' }} | |
run: | | |
# Prepare git user | |
git config user.name github-actions | |
git config user.email [email protected] | |
# Checkout new branch from main | |
git fetch origin main | |
git checkout main | |
git checkout -b actions/bump-base-image-tag | |
# Replace base image tag | |
sed -i "s/ARG STEAMRT_PLATFORM_VERSION=${CI_PR_OLD_BASE_IMAGE_TAG}/ARG STEAMRT_PLATFORM_VERSION=${CI_PR_NEW_BASE_IMAGE_TAG}/" image/base/Dockerfile | |
# Add, commit and push changes to the branch | |
git add image/base/Dockerfile | |
git commit -m "Bump base image tag to ${CI_PR_NEW_BASE_IMAGE_TAG}" | |
git push origin actions/bump-base-image-tag -f | |
- name: Create pull request for newer base image | |
if: ${{ steps.checkpr.outputs.result == 'continue' }} | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const { repo, owner } = context.repo; | |
const result = await github.rest.pulls.create({ | |
title: `[Bump] Base image tag to ${process.env.CI_PR_NEW_BASE_IMAGE_TAG}`, | |
owner, | |
repo, | |
head: 'actions/bump-base-image-tag', | |
base: 'main' | |
}); | |
github.rest.issues.addLabels({ | |
owner, | |
repo, | |
issue_number: result.data.number, | |
labels: ['improvement', 'bump base image tag'] | |
}); |