-
Notifications
You must be signed in to change notification settings - Fork 50
151 lines (139 loc) · 5.72 KB
/
build-prs.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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: 140449482+dsp-developers@users.noreply.github.com
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 }}
steps:
- name: 'Download artifact'
uses: actions/github-script@v3.1.0
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)
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
echo "pr_state=${pr_state}" >> $GITHUB_OUTPUT
echo "head_sha=${head_sha}" >> $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 }}
- 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=${{ github.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 git@github.com: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 }}
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