From 5ae65234c23e4bd6478852ddbff6d3754d41d963 Mon Sep 17 00:00:00 2001 From: Tianqi Date: Fri, 12 Jan 2024 21:15:08 -0500 Subject: [PATCH] Add image rebuild workflow for tempest rock. --- .github/workflows/check_rebuild_tempest.yaml | 81 +++++++++++++++++++ .../workflows/update_tempest_releases.yaml | 46 +++++++++++ 2 files changed, 127 insertions(+) create mode 100644 .github/workflows/check_rebuild_tempest.yaml create mode 100644 .github/workflows/update_tempest_releases.yaml diff --git a/.github/workflows/check_rebuild_tempest.yaml b/.github/workflows/check_rebuild_tempest.yaml new file mode 100644 index 0000000..32ec234 --- /dev/null +++ b/.github/workflows/check_rebuild_tempest.yaml @@ -0,0 +1,81 @@ +name: Check and Rebuild Tempest Image + +on: + workflow_call: + inputs: + branch: + description: The branch to run checks on and re-build images from + type: string + required: true + +jobs: + check-update: + runs-on: ubuntu-latest + outputs: + rebuild_image: ${{ steps.check.outputs.rebuild_image }} + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ inputs.branch }} + - name: Install yq + run: | + sudo snap install yq + - name: Check if a recent snap updates is found + id: check + env: + TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # get the snap name and its channel that the ROCK image should track + snap=$(yq '.parts.[].stage-snaps.[]' rocks/tempest/rockcraft.yaml) + IFS='/' read -r name channel <<< "$snap" + echo "snap: $name, channel: $channel" + + image_version=$(yq '.version' rocks/tempest/rockcraft.yaml) + echo "image version: $image_version" + + # parse date of the last release and reformat it to seconds + format='+%s' + snap_update_date=$(snap info $name | grep $channel | awk '{ print $3 }') + echo "last update date for snap $name in $channel: $snap_update_date" + snap_update_date_in_seconds=$(date -d "$snap_update_date" "$format") + + # confirm the validity of the snap release date + if [[ "$?" != 0 ]]; then + echo "$snap_update_date is not a valid date." + exit 1 + fi + + # get the last image update date and reformat it to seconds + image_update_date=$(curl -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $TOKEN" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/orgs/canonical/packages/container/tempest/versions | yq '.[] | select(.metadata.container.tags[0]==$image_version) | .updated_at') + echo "last update date for image tempest tag $image_version: $image_update_date" + + # exit unsuccessfully if the image tag is not found in registry (to be safe we only update image with exiting tags) + if [[ -z "$image_update_date" ]]; then + echo "Tempest image with tag '$image_version' not found in registry." + exit 1 + fi + image_update_date_in_seconds=$(date -d "$image_update_date" "$format") + + # confirm the validity of the image release date + if [[ "$?" != 0 ]]; then + echo "$image_update_date is not a valid date." + exit 1 + fi + + # re-build and publish image if the image update date is earlier than snap update date + if [ "$image_update_date_in_seconds" -lt "$snap_update_date_in_seconds" ]; then + echo "A recent snap release is found. Will re-build and publish a new image." + echo "rebuild_image=1" >> $GITHUB_OUTPUT + else + echo "No new snap releases detected." + echo "rebuild_image=0" >> $GITHUB_OUTPUT + fi + + build-and-publish: + needs: check-update + if: ${{ needs.check-update.outputs.rebuild_image == 1 }} + uses: ./.github/workflows/build_publish.yaml + with: + rocks: '["tempest"]' + branch: ${{ inputs.branch }} + publish: true diff --git a/.github/workflows/update_tempest_releases.yaml b/.github/workflows/update_tempest_releases.yaml new file mode 100644 index 0000000..428763e --- /dev/null +++ b/.github/workflows/update_tempest_releases.yaml @@ -0,0 +1,46 @@ +name: Update Tempest Releases +on: + workflow_dispatch: + schedule: + - cron: '12 1 * * 1,4' # run workflow at 1:12 (an arbitrary time) every Mon and Thu + +jobs: + get_release_branches: + runs-on: ubuntu-latest + outputs: + branches: ${{ steps.get-branches.outputs.branches }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install jq + run: | + sudo apt install jq + - name: Get branches + id: get-branches + run: | + # filter branches prefixed with `stable/*` in remote repo + filtered_branches=$(git branch -a --list "origin/stable/*" --format='%(refname:short)' | sed 's/origin\///') + + # add `main` branch as it tracks the latest release in development + branches_array="main" + + # create a json list of branches + while IFS= read -r line + do + branches_array+=($line) + done < <(printf '%s\n' "$filtered_branches") + branches_json=$(jq -c -n '$ARGS.positional' --args -- "${branches_array[@]}") + echo $branches_json + + echo "branches=$branches_json" >> $GITHUB_OUTPUT + + check-update: + needs: get_release_branches + strategy: + fail-fast: false + matrix: + branch: ${{ fromJson(needs.get_release_branches.outputs.branches) }} + uses: ./.github/workflows/check_rebuild_tempest.yaml + with: + branch: ${{ matrix.branch }}