Skip to content

Commit d134275

Browse files
update workflow to run on PR with stale dockerfile check
1 parent 8309582 commit d134275

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed
Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: Rebuild and publish new ubcdsci/intro-to-ds image on DockerHub
22
on:
3-
push:
4-
branches:
5-
- main
3+
pull_request:
4+
types: [opened, synchronize]
65
paths:
76
- Dockerfile
87
jobs:
@@ -11,12 +10,30 @@ jobs:
1110
permissions:
1211
contents: write
1312
steps:
14-
- name: Checkout main
13+
- name: Checkout PR branch
1514
uses: actions/checkout@v3
1615
with:
1716
fetch-depth: '0'
18-
ref: 'main'
17+
ref: ${{ github.head_ref }}
18+
- name: Check if Dockerfile needs to be rebuilt
19+
id: check-stale
20+
run: |
21+
echo "GitHub PR action type: ${{ github.event.action }}"
22+
echo "Checking if the PR modifies the Dockerfile"
23+
if [[ ${{ github.event.action }} == 'opened' ]]; then
24+
echo "PR just opened, and it edits the Dockerfile, so rebuilding the image."
25+
echo "stale_dockerfile=true" >> "$GITHUB_OUTPUT"
26+
else
27+
if git diff --quiet ${{ github.event.before }} ${{ github.event.after }} Dockerfile; then
28+
echo "PR synchronized, but Dockerfile was not edited. Not rebuilding the image."
29+
echo "stale_dockerfile=false" >> "$GITHUB_OUTPUT"
30+
else
31+
echo "PR synchronized, and Dockerfile was edited, so rebuilding the image."
32+
echo "stale_dockerfile=true" >> "$GITHUB_OUTPUT"
33+
fi
34+
fi
1935
- name: Rebuild and publish image
36+
if: ${{steps.check-stale.outputs.stale_dockerfile == true }}
2037
id: rebuild
2138
uses: elgohr/Publish-Docker-Github-Action@v5
2239
with:
@@ -26,33 +43,37 @@ jobs:
2643
dockerfile: Dockerfile
2744
snapshot: true
2845
- name: Update build_html.sh script
46+
if: ${{steps.check-stale.outputs.stale_dockerfile == true }}
2947
run: |
3048
git config --local user.email "[email protected]"
3149
git config --local user.name "GitHub Action"
32-
git pull origin main
50+
git pull origin ${{ github.head_ref }}
3351
sed 's/ubcdsci\/intro-to-ds:[[:alnum:]]\+/ubcdsci\/intro-to-ds:${{ steps.rebuild.outputs.snapshot-tag }}/g' build_html.sh > build_html.tmp && mv build_html.tmp build_html.sh
3452
chmod u+x build_html.sh
3553
git add build_html.sh
3654
git commit -m "update build_html.sh script with new docker image"
3755
- name: Update build_pdf.sh script
56+
if: ${{steps.check-stale.outputs.stale_dockerfile == true }}
3857
run: |
3958
git config --local user.email "[email protected]"
4059
git config --local user.name "GitHub Action"
41-
git pull origin main
60+
git pull origin ${{ github.head_ref }}
4261
sed 's/ubcdsci\/intro-to-ds:[[:alnum:]]\+/ubcdsci\/intro-to-ds:${{ steps.rebuild.outputs.snapshot-tag }}/g' build_pdf.sh > build_pdf.tmp && mv build_pdf.tmp build_pdf.sh
4362
chmod u+x build_pdf.sh
4463
git add build_pdf.sh
4564
git commit -m "update build_pdf.sh script with new docker image"
4665
- name: Update docker-compose.yml script
66+
if: ${{steps.check-stale.outputs.stale_dockerfile == true }}
4767
run: |
4868
git config --local user.email "[email protected]"
4969
git config --local user.name "GitHub Action"
50-
git pull origin main
70+
git pull origin ${{ github.head_ref }}
5171
sed 's/ubcdsci\/intro-to-ds:[[:alnum:]]\+/ubcdsci\/intro-to-ds:${{ steps.rebuild.outputs.snapshot-tag }}/g' docker-compose.yml > docker-compose.tmp && mv docker-compose.tmp docker-compose.yml
5272
git add docker-compose.yml
5373
git commit -m "update docker-compose.yml script with new docker image"
5474
- name: Push changes to build scripts
75+
if: ${{steps.check-stale.outputs.stale_dockerfile == true }}
5576
uses: ad-m/github-push-action@master
5677
with:
5778
github_token: ${{ secrets.GITHUB_TOKEN }}
58-
branch: 'main'
79+
branch: ${{ github.head_ref }}

0 commit comments

Comments
 (0)