Skip to content
name: Rebuild and publish new ubcdsci/intro-to-ds image on DockerHub
on:
pull_request:
types: [opened, synchronize]
jobs:
rebuild-docker:
name: Rebuild docker image
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout PR branch
uses: actions/checkout@v3
with:
fetch-depth: '0'
ref: ${{ github.head_ref }}
- name: Check if Dockerfile needs to be rebuilt
id: check-stale
run: |
echo "Checking if Dockerfile was modified since last commit on this PR"
echo "GitHub PR action type: ${{ github.event.action }}"
if [ "${{ github.event.action }}" == "opened" ]; then
echo "GitHub base ref: ${{ github.event.pull_request.base.sha }}"
echo "GitHub head ref: ${{ github.event.pull_request.head.sha }}"
BEFORE=${{ github.event.pull_request.base.sha }}
AFTER=${{ github.event.pull_request.head.sha }}
else
echo "GitHub event before: ${{ github.event.before }}"
echo "GitHub event after: ${{ github.event.after }}"
BEFORE=${{ github.event.before }}
AFTER=${{ github.event.after }}
fi
if git diff --quiet $BEFORE $AFTER Dockerfile; then
echo "PR synchronized, but Dockerfile was not edited. Not rebuilding the image."
echo "stale_dockerfile=false" >> "$GITHUB_OUTPUT"
else
echo "PR synchronized, and Dockerfile was edited, so rebuilding the image."
echo "stale_dockerfile=true" >> "$GITHUB_OUTPUT"
fi
- name: Rebuild and publish image
if: ${{ steps.check-stale.outputs.stale_dockerfile == 'true' }}
id: rebuild
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: ubcdsci/intro-to-ds
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
dockerfile: Dockerfile
snapshot: true
- name: Update build_html.sh script
if: ${{ steps.check-stale.outputs.stale_dockerfile == 'true' }}
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git pull origin ${{ github.head_ref }}
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
chmod u+x build_html.sh
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 == 'true' }}
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git pull origin ${{ github.head_ref }}
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
chmod u+x build_pdf.sh
git add build_pdf.sh
git commit -m "update build_pdf.sh script with new docker image"
- name: Update docker-compose.yml script
if: ${{ steps.check-stale.outputs.stale_dockerfile == 'true' }}
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git pull origin ${{ github.head_ref }}
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
git add docker-compose.yml
git commit -m "update docker-compose.yml script with new docker image"
- name: Push changes to build scripts
if: ${{ steps.check-stale.outputs.stale_dockerfile == 'true' }}
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.head_ref }}