Skip to content

Commit

Permalink
Make Dockerfile rebuild only on PR open and synchronize that edits Do…
Browse files Browse the repository at this point in the history
…ckerfile

Previously Dockerfile would be rebuilt on PR open and any new commits to the PR, resulting in rebuilding/committing changes to the build scripts many times even if the Dockerfile doesn't change.
  • Loading branch information
trevorcampbell authored Sep 13, 2023
1 parent 7cac0e8 commit e3701aa
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/update_build_environment.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Rebuild and publish new ubcdsci/py-intro-to-ds image on DockerHub
on:
pull_request:
types: [opened, synchronize]
paths:
- Dockerfile
jobs:
Expand All @@ -14,7 +15,24 @@ jobs:
with:
fetch-depth: '0'
ref: ${{ github.head_ref }}
- name: Check if Dockerfile needs to be rebuilt
id: check-stale
run: |
if [[ ${{ github.event.action == 'opened' }} ]]; then
echo "stale_dockerfile=1" >> "$GITHUB_OUTPUT"
echo "PR just opened, and it edits the Dockerfile, so rebuilding the image."
else
git diff --quiet ${{ github.event.before }} ${{ github.event.after }} Dockerfile
echo "stale_dockerfile=$?" >> "$GITHUB_OUTPUT"
if [[ ${{ steps.check-stale.outputs.stale_dockerfile == 0 }} ]]; then
echo "PR synchronized, but Dockerfile was not edited. Not rebuilding the image."
else
echo "PR synchronized, and Dockerfile was edited, so rebuilding the image."
fi
fi
- name: Check if Dockerfile needs to be rebuilt
- name: Rebuild and publish image
if: ${{ steps.check-stale.outputs.stale_dockerfile }}
id: rebuild
uses: elgohr/Publish-Docker-Github-Action@v5
with:
Expand All @@ -24,6 +42,7 @@ jobs:
dockerfile: Dockerfile
snapshot: true
- name: Update build_html.sh script
if: ${{ steps.check-stale.outputs.stale_dockerfile }}
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
Expand All @@ -33,6 +52,7 @@ jobs:
git add build_html.sh
git commit -m "update build_html.sh script with new docker image"
- name: Update build_pdf.sh script
if: ${{ steps.check-stale.outputs.stale_dockerfile }}
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
Expand All @@ -42,6 +62,7 @@ jobs:
git add build_pdf.sh
git commit -m "update build_pdf.sh script with new docker image"
- name: Push changes to build scripts
if: ${{ steps.check-stale.outputs.stale_dockerfile }}
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down

0 comments on commit e3701aa

Please sign in to comment.