chore: enhance image build process #11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Push Docker Image | |
on: | |
push: | |
branches: | |
- main | |
tags: | |
- '*' | |
workflow_dispatch: | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
with: | |
driver-opts: image=moby/buildkit:latest | |
- name: Install docker-squash | |
run: | | |
pip install docker-squash | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Log in to the Container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Clone repo to build | |
run: git clone https://github.com/${{ github.repository }}.git repo | |
- name: Build and push image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
tags: | | |
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-huge | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Setup jq | |
uses: dcarbone/install-jq-action@v3 | |
with: | |
version: "1.7" | |
- name: Squash the image | |
run: | | |
docker pull ghcr.io/${{ github.repository }}:${{ github.ref_name }}-huge && \ | |
docker pull debian:bookworm && \ | |
docker-squash \ | |
--from $(docker inspect --format='{{json .RootFS.Layers}}' debian:bookworm | jq -r '.[-1]') \ | |
--tag ghcr.io/${{ github.repository }}:${{ github.ref_name }} \ | |
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-huge | |
- name: Push squashed image | |
run: | | |
docker push ghcr.io/${{ github.repository }}:${{ github.ref_name }} && \ | |
docker tag ghcr.io/${{ github.repository }}:${{ github.ref_name }} ${{ github.repository }}:${{ github.ref_name }} && \ | |
docker push ${{ github.repository }}:${{ github.ref_name }} | |
- name: Push latest image | |
run: | | |
docker tag ghcr.io/${{ github.repository }}:${{ github.ref_name }} ghcr.io/${{ github.repository }}:latest && \ | |
docker push ghcr.io/${{ github.repository }}:latest && \ | |
docker tag ghcr.io/${{ github.repository }}:${{ github.ref_name }} ${{ github.repository }}:latest && \ | |
docker push ${{ github.repository }}:latest | |
if: startsWith(github.ref, 'refs/tags/') | |
- name: Docker Hub Description | |
uses: peter-evans/dockerhub-description@v4 | |
with: | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
repository: ${{ github.repository }} | |
short-description: ${{ github.event.repository.description }} | |
enable-url-completion: true | |
create-release: | |
runs-on: ubuntu-latest | |
needs: build-and-push | |
permissions: write-all | |
if: startsWith(github.ref, 'refs/tags/') | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Update CHANGELOG | |
id: changelog | |
uses: requarks/changelog-action@v1 | |
with: | |
token: ${{ github.token }} | |
tag: ${{ github.ref_name }} | |
- name: Create Release | |
id: create_release | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
tag_name: ${{ github.ref_name }} | |
release_name: v${{ github.ref_name }} | |
body: | | |
Container images for this release: | |
- Docker Hub: `${{ github.repository }}:${{ github.ref_name }}` | |
- GitHub Container Registry: `ghcr.io/${{ github.repository }}:${{ github.ref_name }}` | |
${{ steps.changelog.outputs.changes }} | |
draft: false | |
prerelease: false | |
- name: Commit CHANGELOG.md | |
uses: stefanzweifel/git-auto-commit-action@v4 | |
with: | |
branch: main | |
commit_message: 'docs: update CHANGELOG.md for ${{ github.ref_name }} [skip ci]' | |
file_pattern: CHANGELOG.md | |
- name: Docker Hub Description | |
uses: peter-evans/dockerhub-description@v4 | |
with: | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
repository: ${{ github.repository }} | |
short-description: ${{ github.event.repository.description }} | |
enable-url-completion: true |