Skip to content

Commit 03666a0

Browse files
authored
Merge pull request opendatahub-io#249 from HumairAK/secure_pr_runs
Add secure pr build triggers.
2 parents 6fc2303 + a15f084 commit 03666a0

File tree

2 files changed

+98
-16
lines changed

2 files changed

+98
-16
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Trigger build images for PRs
2+
on:
3+
pull_request:
4+
types:
5+
- opened
6+
- reopened
7+
- closed
8+
- synchronize
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
11+
cancel-in-progress: true
12+
jobs:
13+
upload-data:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Save PR payload
18+
shell: bash
19+
run: |
20+
mkdir -p ./pr
21+
echo ${{ github.event.pull_request.number }} >> ./pr/pr_number
22+
echo ${{ github.event.pull_request.state }} >> ./pr/pr_state
23+
echo ${{ github.event.pull_request.head.sha }} >> ./pr/head_sha
24+
- uses: actions/upload-artifact@v2
25+
with:
26+
name: pr
27+
path: pr/

.github/workflows/build-prs.yml

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,69 @@
11
name: Build images for PRs
22
on:
3-
pull_request:
3+
workflow_run:
4+
workflows: ["Trigger build images for PRs"]
45
types:
5-
- opened
6-
- reopened
7-
- closed
8-
- synchronize
9-
permissions:
10-
pull-requests: read
11-
concurrency:
12-
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
13-
cancel-in-progress: true
6+
- completed
147
env:
158
IMAGE_REPO_DSPO: data-science-pipelines-operator
16-
SOURCE_BRANCH: ${{ github.event.pull_request.head.sha }}
179
QUAY_ORG: opendatahub
1810
QUAY_ID: ${{ secrets.QUAY_ID }}
1911
QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }}
20-
TARGET_IMAGE_TAG: pr-${{ github.event.pull_request.number }}
2112
GH_USER_EMAIL: [email protected]
2213
GH_USER_NAME: dsp-developers
2314
jobs:
15+
fetch-data:
16+
name: Fetch workflow payload
17+
runs-on: ubuntu-latest
18+
if: >
19+
github.event.workflow_run.event == 'pull_request' &&
20+
github.event.workflow_run.conclusion == 'success'
21+
outputs:
22+
pr_state: ${{ steps.vars.outputs.pr_state }}
23+
pr_number: ${{ steps.vars.outputs.pr_number }}
24+
head_sha: ${{ steps.vars.outputs.head_sha }}
25+
steps:
26+
- name: 'Download artifact'
27+
uses: actions/[email protected]
28+
with:
29+
script: |
30+
var artifacts = await github.actions.listWorkflowRunArtifacts({
31+
owner: context.repo.owner,
32+
repo: context.repo.repo,
33+
run_id: ${{github.event.workflow_run.id }},
34+
});
35+
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
36+
return artifact.name == "pr"
37+
})[0];
38+
var download = await github.actions.downloadArtifact({
39+
owner: context.repo.owner,
40+
repo: context.repo.repo,
41+
artifact_id: matchArtifact.id,
42+
archive_format: 'zip',
43+
});
44+
var fs = require('fs');
45+
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
46+
- run: unzip pr.zip
47+
- shell: bash
48+
id: vars
49+
run: |
50+
pr_number=$(cat ./pr_number)
51+
pr_state=$(cat ./pr_state)
52+
head_sha=$(cat ./head_sha)
53+
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
54+
echo "pr_state=${pr_state}" >> $GITHUB_OUTPUT
55+
echo "head_sha=${head_sha}" >> $GITHUB_OUTPUT
56+
2457
build-pr-image:
25-
if: github.event.pull_request.state == 'open'
58+
if: needs.fetch-data.outputs.pr_state == 'open'
2659
runs-on: ubuntu-latest
60+
needs: fetch-data
61+
concurrency:
62+
group: ${{ github.workflow }}-build-pr-image-${{ needs.fetch-data.outputs.pr_number }}
63+
cancel-in-progress: true
64+
env:
65+
SOURCE_BRANCH: ${{ needs.fetch-data.outputs.head_sha }}
66+
TARGET_IMAGE_TAG: pr-${{ needs.fetch-data.outputs.pr_number }}
2767
steps:
2868
- uses: actions/checkout@v3
2969
- name: Build Image
@@ -33,6 +73,15 @@ jobs:
3373
IMAGE_REPO: ${{ env.IMAGE_REPO_DSPO }}
3474
DOCKERFILE: Dockerfile
3575
GH_REPO: ${{ github.repository }}
76+
- name: Echo PR metadata
77+
shell: bash
78+
env:
79+
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }}
80+
IMG: quay.io/${{ env.QUAY_ORG }}/${{ env.IMAGE_REPO_DSPO }}:${{ env.TARGET_IMAGE_TAG }}
81+
run: |
82+
echo ${{ needs.fetch-data.outputs.head_sha }}
83+
echo ${{ needs.fetch-data.outputs.pr_number }}
84+
echo ${{ needs.fetch-data.outputs.pr_state }}
3685
- name: Send comment
3786
shell: bash
3887
env:
@@ -66,7 +115,7 @@ jobs:
66115
cd $(mktemp -d)
67116
git clone [email protected]:opendatahub-io/data-science-pipelines-operator.git
68117
cd data-science-pipelines-operator/
69-
git fetch origin pull/${{ github.event.pull_request.number }}/head
118+
git fetch origin pull/${{ needs.fetch-data.outputs.pr_number }}/head
70119
git checkout -b pullrequest ${{ env.SOURCE_BRANCH }}
71120
make deploy IMG="${{ env.IMG }}"
72121
```
@@ -76,11 +125,17 @@ jobs:
76125
EOF
77126
fi
78127
79-
gh pr comment ${{ github.event.pull_request.number }} --body-file /tmp/body-file.txt
128+
gh pr comment ${{ needs.fetch-data.outputs.pr_number }} --body-file /tmp/body-file.txt
80129
81130
clean-pr-images:
82-
if: github.event.pull_request.state == 'closed'
131+
if: needs.fetch-data.outputs.pr_state == 'closed'
83132
runs-on: ubuntu-latest
133+
needs: fetch-data
134+
concurrency:
135+
group: ${{ github.workflow }}-clean-pr-images-${{ needs.fetch-data.outputs.pr_number }}
136+
cancel-in-progress: true
137+
env:
138+
TARGET_IMAGE_TAG: pr-${{ needs.fetch-data.outputs.pr_number }}
84139
steps:
85140
- name: Delete PR image
86141
shell: bash

0 commit comments

Comments
 (0)