Skip to content

Commit c96d5f7

Browse files
ci (docker.yml): refactor docker build/publish
1 parent 0c47bfd commit c96d5f7

File tree

1 file changed

+17
-43
lines changed
  • {{cookiecutter.project_slug}}/.github/workflows

1 file changed

+17
-43
lines changed

{{cookiecutter.project_slug}}/.github/workflows/docker.yml

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ on:
1010
paths:
1111
- 'Dockerfile*'
1212
- 'pyproject.toml'
13-
- 'poetry.lock'
13+
- '*.lock'
1414
- 'requirements.txt'
1515
- '**.py'
1616
- '**.sh'
1717
- '.dockerignore'
1818
- '.env.example'
1919
- '.github/workflows/**'
2020
workflow_dispatch:
21-
# workflow_run:
22-
# workflows: ["Run pytest"]
23-
# types:
24-
# - completed
2521

2622
env:
27-
REGISTRY_URL: ${{ vars.REGISTRY_URL }}
28-
REGISTRY_USER: ${{ vars.REGISTRY_USER }}
23+
REGISTRY_URL: ${{ vars.REGISTRY_URL || 'ghcr.io' }}
24+
REGISTRY_USER: ${{ vars.REGISTRY_USER || github.repository_owner }}
25+
REGISTRY_PASS: ${{ (vars.REGISTRY_URL == 'ghcr.io' || !vars.REGISTRY_URL) && secrets.GITHUB_TOKEN || secrets.REGISTRY_PASS }}
26+
IMAGE_NAME: ${{ vars.IMAGE || github.event.repository.name }}
2927

3028
jobs:
3129
push_to_registry:
@@ -36,7 +34,6 @@ jobs:
3634
matrix:
3735
dockerfile: [Dockerfile]
3836
concurrency:
39-
# group: ${{ github.workflow }}-${{ matrix.dockerfile }}-${{ github.event.workflow_run.head_branch || github.ref }}
4037
group: ${{ github.workflow }}-${{ matrix.dockerfile }}-${{ github.head_ref || github.ref }}
4138
cancel-in-progress: true
4239
permissions:
@@ -48,22 +45,6 @@ jobs:
4845
- name: Checkout code
4946
uses: actions/checkout@v4
5047

51-
- name: Set password by container registry
52-
run: |
53-
case "${{ env.REGISTRY_URL }}" in
54-
"ghcr.io")
55-
echo "REGISTRY_PASS=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV
56-
;;
57-
*)
58-
if [ -n "${{ secrets.REGISTRY_PASS }}" ]; then
59-
echo "REGISTRY_PASS=${{ secrets.REGISTRY_PASS }}" >> $GITHUB_ENV
60-
else
61-
echo "REGISTRY_PASS secret is not set and registry is not recognized. Exiting..."
62-
exit 1
63-
fi
64-
;;
65-
esac
66-
6748
- name: Log into container registry
6849
if: github.event_name != 'pull_request'
6950
uses: docker/login-action@v3
@@ -72,23 +53,12 @@ jobs:
7253
username: ${{ env.REGISTRY_USER }}
7354
password: ${{ env.REGISTRY_PASS }}
7455

75-
- name: Set image name
76-
id: image_name
77-
run: |
78-
if [ -n "${{ env.IMAGE }}" ]; then
79-
IMAGE="${{ env.IMAGE }}"
80-
else
81-
IMAGE=$(grep "LABEL org.opencontainers.image.title" Dockerfile | cut -d'"' -f2)
82-
fi
83-
echo "IMAGE=$IMAGE" >> $GITHUB_OUTPUT
84-
echo "IMAGE=$IMAGE" >> $GITHUB_ENV
85-
8656
- name: Docker meta
8757
id: meta
8858
uses: docker/metadata-action@v5
8959
with:
9060
images: |
91-
${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }}
61+
${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ env.IMAGE_NAME }}
9262
tags: |
9363
type=schedule
9464
type=ref,event=branch
@@ -98,6 +68,7 @@ jobs:
9868
type=semver,pattern={{major}}
9969
type=sha
10070
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch || 'main') }}
71+
bake-target: docker-metadata-action
10172

10273
- name: Setup QEMU
10374
uses: docker/setup-qemu-action@v3
@@ -106,12 +77,15 @@ jobs:
10677
uses: docker/setup-buildx-action@v3
10778

10879
- name: Build and push
109-
uses: docker/build-push-action@v6
80+
uses: docker/bake-action@v6
11081
with:
111-
context: .
112-
file: ./${{ matrix.dockerfile }}
82+
source: "{{defaultContext}}"
83+
files: |
84+
./docker-bake.hcl
85+
cwd://${{ steps.meta.outputs.bake-file }}
86+
targets: build
11387
push: ${{ github.event_name != 'pull_request' }}
114-
tags: ${{ steps.meta.outputs.tags }}
115-
labels: ${{ steps.meta.outputs.labels }}
116-
cache-from: type=registry,ref=${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }}:buildcache
117-
cache-to: type=registry,ref=${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }}:buildcache,mode=max
88+
set: |
89+
*.tags=${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ env.IMAGE_NAME }}:latest
90+
*.cache-from=type=gha
91+
*.cache-to=type=gha,mode=max

0 commit comments

Comments
 (0)