Skip to content

Commit

Permalink
Optimize docker build time
Browse files Browse the repository at this point in the history
  • Loading branch information
betschki committed Aug 4, 2024
1 parent f6c8104 commit e71b25b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
59 changes: 49 additions & 10 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,78 @@ jobs:
if: github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker/setup-qemu-action@v1
- uses: docker/setup-buildx-action@v1
- uses: docker/login-action@v1
- name: Checkout code
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: magicpages/mysql-onsite-offsite-backup:dev
platforms: linux/amd64,linux/arm64
platforms: linux/amd64 # Only build for amd64 during development

- name: Cleanup Docker cache
run: docker builder prune -f

release-build-and-push:
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker/setup-qemu-action@v1
- uses: docker/setup-buildx-action@v1
- uses: docker/login-action@v1
- name: Checkout code
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- run: echo "VERSION=${GITHUB_REF/refs\/tags\/v/}" >> $GITHUB_ENV

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: magicpages/mysql-onsite-offsite-backup:${{ env.VERSION }},magicpages/mysql-onsite-offsite-backup:latest
platforms: linux/amd64,linux/arm64
platforms: linux/amd64,linux/arm64 # Build for both platforms for releases

- name: Cleanup Docker cache
run: docker builder prune -f
24 changes: 7 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,15 @@ RUN apt-get update && \
apt-get install -y mysql-client s3cmd xz-utils curl cron && \
apt-get clean

# Copy backup, cleanup, and entrypoint scripts
COPY backup.bash /usr/local/bin/backup.bash
COPY cleanup.bash /usr/local/bin/cleanup.bash
COPY entrypoint.bash /usr/local/bin/entrypoint.bash

# Make the scripts executable
RUN chmod +x /usr/local/bin/backup.bash /usr/local/bin/cleanup.bash /usr/local/bin/entrypoint.bash

# Add crontab file in the cron directory
# Copy scripts and crontab
COPY backup.bash cleanup.bash entrypoint.bash /usr/local/bin/
COPY crontab /etc/cron.d/backup-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/backup-cron

# Apply cron job
RUN crontab /etc/cron.d/backup-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Make the scripts executable and set up cron jobs
RUN chmod +x /usr/local/bin/backup.bash /usr/local/bin/cleanup.bash /usr/local/bin/entrypoint.bash && \
chmod 0644 /etc/cron.d/backup-cron && \
crontab /etc/cron.d/backup-cron && \
touch /var/log/cron.log

# Set entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint.bash"]
Expand Down

0 comments on commit e71b25b

Please sign in to comment.