.github/workflows/pr_image_id.yml #3327
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
--- | |
# Use the latest published version of the cirrus-ci_retrospective container | |
# to determine the execution context of _this_ workflow run. If it is a | |
# pull request, post the to-be VM/Container image ID string as a comment. | |
on: | |
check_suite: # ALWAYS triggered from the default branch | |
# Ref: https://help.github.com/en/actions/reference/events-that-trigger-workflows#check-suite-event-check_suite | |
types: | |
- completed | |
jobs: | |
comment_image_id: | |
# Do not execute for other github applications, only works with cirrus-ci | |
if: github.event.check_suite.app.name == 'Cirrus CI' | |
runs-on: ubuntu-latest | |
env: | |
# This is the last component of the gist URL | |
# i.e. https://gist.github.com/<user>/<id> | |
built_images_gist_id: f505b6fb78db279855862e035629f8aa | |
steps: | |
- name: Execute latest upstream cirrus-ci_retrospective | |
uses: docker://quay.io/libpod/cirrus-ci_retrospective:latest | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
# Writes $GITHUB_WORKSPACE/cirrus-ci_retrospective.json | |
- name: Debug cirrus-ci_retrospective JSON | |
run: jq --indent 4 --color-output . $GITHUB_WORKSPACE/cirrus-ci_retrospective.json | |
- name: Load JSON into github workflow output variables | |
id: retro | |
run: | | |
ccirjson=$GITHUB_WORKSPACE/cirrus-ci_retrospective.json | |
prn=$(jq --raw-output \ | |
'.[] | select(.name == "'success'") | .build.pullRequest' \ | |
"$ccirjson") | |
bid=$(jq --raw-output \ | |
'.[] | select(.name == "'success'") | .build.id' \ | |
"$ccirjson") | |
status=$(jq --raw-output \ | |
'.[] | select(.name == "'success'") | .status' \ | |
"$ccirjson") | |
if [[ -n "$prn" ]] && \ | |
[[ "$prn" != "null" ]] && \ | |
[[ $prn -gt 0 ]] && \ | |
[[ "$status" == "COMPLETED" ]] | |
then | |
printf "prn=%s\n" "$prn" >> $GITHUB_OUTPUT | |
printf "bid=%s\n" "$bid" >> $GITHUB_OUTPUT | |
printf "is_pr=%s\n" "true" >> $GITHUB_OUTPUT | |
else | |
printf "prn=%s\n" "0" >> $GITHUB_OUTPUT | |
printf "bid=%s\n" "0" >> $GITHUB_OUTPUT | |
printf "is_pr=%s\n" "false" >> $GITHUB_OUTPUT | |
fi | |
- if: steps.retro.outputs.is_pr == 'true' | |
uses: actions/checkout@v4 | |
with: | |
persist-credentials: false | |
- if: steps.retro.outputs.is_pr == 'true' | |
name: Retrieve and process any manifest artifacts | |
# Use the CCIA image produce by the `Build Tooling images` | |
# task of the PR we're looking at. This allows testing | |
# of changes to the CCIA container before merging into `main` | |
# (where this workflow runs from). If that should fail, | |
# fall back to using the latest built CCIA image. | |
run: | | |
PODMAN="podman run --rm -v $GITHUB_WORKSPACE:/data -w /data" | |
$PODMAN quay.io/libpod/ccia:latest --verbose "${{ steps.retro.outputs.bid }}" ".*/manifest.json" | |
- if: steps.retro.outputs.is_pr == 'true' | |
name: Count the number of manifest.json files downloaded | |
id: manifests | |
run: | | |
dled=$(find $GITHUB_WORKSPACE -type f -name 'manifest.json' -not -path '*fake_manifests/*/manifest.json' | wc -l) | |
if [[ "$dled" =~ ^[0-9]+$ ]]; then | |
printf "count=%s\n" "$dled" >> $GITHUB_OUTPUT | |
else | |
printf "count=0\n" >> $GITHUB_OUTPUT | |
fi | |
- if: steps.manifests.outputs.count > 0 | |
name: Extract build details from manifest files | |
env: | |
FLTR: >- | |
{"stage": .builds[].custom_data.STAGE, | |
"name": .builds[].name, | |
"sfx": .builds[].custom_data.IMG_SFX, | |
"task": .builds[].custom_data.TASK} | |
run: | | |
cd $GITHUB_WORKSPACE | |
find ./ -type f -name 'manifest.json' -print0 | \ | |
xargs --null jq -e -c "$FLTR" | \ | |
jq -e -s '.' > ./built_images.json | |
- if: steps.manifests.outputs.count > 0 | |
name: Debug built_images.json contents | |
run: | | |
jq --color-output . $GITHUB_WORKSPACE/built_images.json | |
- if: steps.manifests.outputs.count > 0 | |
id: body | |
name: Format PR-comment body | |
# Consumes $GITHUB_WORKSPACE/built_images.json | |
run: .github/actions/bin/create_image_table.py | |
- if: steps.manifests.outputs.count > 0 | |
name: Debug images.md contents | |
# Produced by create_image_table.py | |
run: cat $GITHUB_WORKSPACE/images.md | |
- if: steps.manifests.outputs.count > 0 | |
name: Debug images.json contents | |
# Produced by create_image_table.py | |
run: jq --color-output . $GITHUB_WORKSPACE/images.json | |
# jungwinter/comment cannot consume a file as comment input | |
- if: steps.manifests.outputs.count > 0 | |
name: Debug PR comment markdown | |
# Use a here-document to display to avoid any | |
# problems with passing special-characters into echo | |
# The quoted-EOD prevents any shell interpretation. | |
run: | | |
cat <<"EOD" | |
${{ env.IMAGE_TABLE }} | |
EOD | |
- if: steps.manifests.outputs.count > 0 | |
name: Post PR comment with image name/id table | |
uses: jungwinter/comment@v1.1.0 | |
with: | |
issue_number: '${{ steps.retro.outputs.prn }}' | |
type: 'create' | |
token: '${{ secrets.GITHUB_TOKEN }}' | |
body: | | |
${{ env.IMAGE_TABLE }} | |
# Ref: https://github.com/marketplace/actions/deploy-to-gist | |
- if: steps.manifests.outputs.count > 0 | |
name: Publish image name/id MD table to gist | |
uses: exuanbo/actions-deploy-gist@v1.1.4 | |
with: | |
token: ${{ secrets.IMG_GIST_TOKEN }} | |
gist_id: ${{ env.built_images_gist_id }} | |
file_path: images.md | |
file_type: text | |
- if: steps.manifests.outputs.count > 0 | |
name: Publish image name/id JSON table to gist | |
uses: exuanbo/actions-deploy-gist@v1.1.4 | |
with: | |
token: ${{ secrets.IMG_GIST_TOKEN }} | |
gist_id: ${{ env.built_images_gist_id }} | |
file_path: images.json | |
file_type: text |