Build images for PRs #947
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: Build images for PRs | |
on: | |
workflow_run: | |
workflows: ["Trigger build images for PRs"] | |
types: | |
- completed | |
env: | |
IMAGE_REPO_DSPO: data-science-pipelines-operator | |
QUAY_ORG: opendatahub | |
QUAY_ID: ${{ secrets.QUAY_ID }} | |
QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }} | |
GH_USER_EMAIL: [email protected] | |
GH_USER_NAME: dsp-developers | |
jobs: | |
fetch-data: | |
name: Fetch workflow payload | |
runs-on: ubuntu-latest | |
if: > | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
outputs: | |
pr_state: ${{ steps.vars.outputs.pr_state }} | |
pr_number: ${{ steps.vars.outputs.pr_number }} | |
head_sha: ${{ steps.vars.outputs.head_sha }} | |
event_action: ${{ steps.vars.outputs.event_action }} | |
steps: | |
- name: 'Download artifact' | |
uses: actions/[email protected] | |
with: | |
script: | | |
var artifacts = await github.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{github.event.workflow_run.id }}, | |
}); | |
var matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "pr" | |
})[0]; | |
var download = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
var fs = require('fs'); | |
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); | |
- run: unzip pr.zip | |
- shell: bash | |
id: vars | |
run: | | |
pr_number=$(cat ./pr_number) | |
pr_state=$(cat ./pr_state) | |
head_sha=$(cat ./head_sha) | |
event_action=$(cat ./event_action) | |
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT | |
echo "pr_state=${pr_state}" >> $GITHUB_OUTPUT | |
echo "head_sha=${head_sha}" >> $GITHUB_OUTPUT | |
echo "event_action=${event_action}" >> $GITHUB_OUTPUT | |
build-pr-image: | |
if: needs.fetch-data.outputs.pr_state == 'open' | |
runs-on: ubuntu-latest | |
needs: fetch-data | |
concurrency: | |
group: ${{ github.workflow }}-build-pr-image-${{ needs.fetch-data.outputs.pr_number }} | |
cancel-in-progress: true | |
env: | |
SOURCE_BRANCH: ${{ needs.fetch-data.outputs.head_sha }} | |
TARGET_IMAGE_TAG: pr-${{ needs.fetch-data.outputs.pr_number }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Build Image | |
uses: ./.github/actions/build | |
with: | |
OVERWRITE: true | |
IMAGE_REPO: ${{ env.IMAGE_REPO_DSPO }} | |
DOCKERFILE: Dockerfile | |
GH_REPO: ${{ github.repository }} | |
- name: Echo PR metadata | |
shell: bash | |
env: | |
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }} | |
IMG: quay.io/${{ env.QUAY_ORG }}/${{ env.IMAGE_REPO_DSPO }}:${{ env.TARGET_IMAGE_TAG }} | |
run: | | |
echo ${{ needs.fetch-data.outputs.head_sha }} | |
echo ${{ needs.fetch-data.outputs.pr_number }} | |
echo ${{ needs.fetch-data.outputs.pr_state }} | |
echo ${{ needs.fetch-data.outputs.event_action }} | |
- name: Send comment | |
shell: bash | |
env: | |
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }} | |
IMG: quay.io/${{ env.QUAY_ORG }}/${{ env.IMAGE_REPO_DSPO }}:${{ env.TARGET_IMAGE_TAG }} | |
run: | | |
git config user.email "${{ env.GH_USER_EMAIL }}" | |
git config user.name "${{ env.GH_USER_NAME }}" | |
action=${{ needs.fetch-data.outputs.event_action }} | |
if [[ "$action" == "synchronize" ]]; then | |
echo "Change to PR detected. A new PR build was completed." >> /tmp/body-file.txt | |
fi | |
if [[ "$action" == "reopened" ]]; then | |
echo "PR was re-opened." >> /tmp/body-file.txt | |
fi | |
cat <<"EOF" >> /tmp/body-file.txt | |
A new image has been built to help with testing out this PR: `${{ env.IMG }}` | |
EOF | |
if [[ "$action" == "opened" || "$action" == "reopened" ]]; then | |
cat <<"EOF" >> /tmp/body-file.txt | |
An OCP cluster where you are logged in as cluster admin is required. | |
To use this image run the following: | |
```bash | |
cd $(mktemp -d) | |
git clone [email protected]:opendatahub-io/data-science-pipelines-operator.git | |
cd data-science-pipelines-operator/ | |
git fetch origin pull/${{ needs.fetch-data.outputs.pr_number }}/head | |
git checkout -b pullrequest ${{ env.SOURCE_BRANCH }} | |
oc new-project opendatahub | |
make deploy IMG="${{ env.IMG }}" | |
``` | |
More instructions [here](https://github.com/opendatahub-io/data-science-pipelines-operator#deploy-dsp-instance) on how to deploy and test a Data Science Pipelines Application. | |
EOF | |
fi | |
gh pr comment ${{ needs.fetch-data.outputs.pr_number }} --body-file /tmp/body-file.txt | |
clean-pr-images: | |
if: needs.fetch-data.outputs.pr_state == 'closed' | |
runs-on: ubuntu-latest | |
needs: fetch-data | |
concurrency: | |
group: ${{ github.workflow }}-clean-pr-images-${{ needs.fetch-data.outputs.pr_number }} | |
cancel-in-progress: true | |
env: | |
TARGET_IMAGE_TAG: pr-${{ needs.fetch-data.outputs.pr_number }} | |
steps: | |
- name: Delete PR image | |
shell: bash | |
run: | | |
tag=$(curl --request GET 'https://quay.io/api/v1/repository/${{ env.QUAY_ORG }}/${{ env.IMAGE_REPO_DSPO }}/tag/?specificTag=${{ env.TARGET_IMAGE_TAG }}') | |
exists=$(echo ${tag} | yq .tags - | yq any) | |
IMAGE=quay.io/${{ env.QUAY_ORG }}/${{ env.IMAGE_REPO_DSPO }}:${{ env.TARGET_IMAGE_TAG }} | |
if [[ "$exists" == "true" ]]; then | |
echo "PR Closed deleting image...${{ env.IMAGE }}." | |
skopeo delete --creds ${{ env.QUAY_ID }}:${{ env.QUAY_TOKEN }} docker://${IMAGE} | |
else | |
echo "Deletion of image ${IMAGE} skipped because image already does not exist." | |
fi |