Skip to content

Commit 56277c9

Browse files
authored
docker workflows (#6)
1 parent 418e2d1 commit 56277c9

4 files changed

Lines changed: 120 additions & 2 deletions

File tree

.github/workflows/publish-dev.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,51 @@ jobs:
2020
secrets:
2121
twine_username: ${{ secrets.TESTPYPI_USERNAME }}
2222
twine_password: ${{ secrets.TESTPYPI_TOKEN }}
23+
24+
build_image:
25+
# TODO: remove this entire job if an image does not make sense (ie: just a library, no CLI)
26+
permissions: write-all
27+
needs: test_and_publish
28+
name: test image
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: Checkout
32+
uses: actions/checkout@v4
33+
34+
- name: Log in to GHCR
35+
uses: docker/login-action@v3
36+
with:
37+
registry: ghcr.io
38+
username: ${{ github.actor }}
39+
password: ${{ secrets.GITHUB_TOKEN }}
40+
41+
- name: Set up QEMU
42+
uses: docker/setup-qemu-action@v3
43+
with:
44+
platforms: linux/amd64,linux/arm64
45+
46+
- name: Set up Docker Buildx
47+
id: buildx
48+
uses: docker/setup-buildx-action@v3
49+
with:
50+
platforms: linux/amd64,linux/arm64
51+
52+
- name: Sanitize branch name
53+
id: sanitize_branch
54+
run: |
55+
SANITIZED_BRANCH=$(echo -n "${{ github.ref_name }}" | tr '/' '-' | tr -c '[:alnum:]-' '-')
56+
echo sanitized_branch=$SANITIZED_BRANCH >> $GITHUB_OUTPUT
57+
58+
- name: Build and push image - release
59+
uses: docker/build-push-action@v6
60+
with:
61+
push: true
62+
platforms: linux/amd64,linux/arm64
63+
cache-from: ghcr.io/${{ github.repository }}:latest-build-cache
64+
tags: |
65+
ghcr.io/${{ github.repository }}:${{ steps.sanitize_branch.outputs.sanitized_branch }}
66+
67+
- name: Show summary
68+
run: |
69+
echo 'Published: ' >> ${GITHUB_STEP_SUMMARY}
70+
echo '* `ghcr.io/${{ github.repository }}:${{ steps.sanitize_branch.outputs.sanitized_branch }}`' >> ${GITHUB_STEP_SUMMARY}

.github/workflows/publish-main.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,48 @@ jobs:
1414
secrets:
1515
twine_username: ${{ secrets.PYPI_USERNAME }}
1616
twine_password: ${{ secrets.PYPI_TOKEN }}
17+
18+
build_image:
19+
# TODO: remove this entire job if an image does not make sense (ie: just a library, no CLI)
20+
permissions: write-all
21+
needs: test_and_publish
22+
name: release image
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v4
27+
28+
- name: Log in to GHCR
29+
uses: docker/login-action@v3
30+
with:
31+
registry: ghcr.io
32+
username: ${{ github.actor }}
33+
password: ${{ secrets.GITHUB_TOKEN }}
34+
35+
- name: Set up QEMU
36+
uses: docker/setup-qemu-action@v3
37+
with:
38+
platforms: linux/amd64,linux/arm64
39+
40+
- name: Set up Docker Buildx
41+
id: buildx
42+
uses: docker/setup-buildx-action@v3
43+
with:
44+
platforms: linux/amd64,linux/arm64
45+
46+
- name: Build and push image - release
47+
uses: docker/build-push-action@v6
48+
with:
49+
push: true
50+
platforms: linux/amd64,linux/arm64
51+
cache-from: ghcr.io/${{ github.repository }}:latest-build-cache
52+
cache-to: ghcr.io/${{ github.repository }}:latest-build-cache
53+
tags: |
54+
ghcr.io/${{ github.repository }}:${{ github.ref_name }}
55+
ghcr.io/${{ github.repository }}:latest
56+
57+
- name: Show summary
58+
run: |
59+
echo 'Published: ' >> ${GITHUB_STEP_SUMMARY}
60+
echo '* `ghcr.io/${{ github.repository }}:${{ github.ref_name }}`' >> ${GITHUB_STEP_SUMMARY}
61+
echo '* `ghcr.io/${{ github.repository }}:latest`' >> ${GITHUB_STEP_SUMMARY}

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# TODO: remove all file if no docker image is required (ie: just a python library)
2+
FROM python:3.10-alpine AS base
3+
4+
# --- builder
5+
FROM base AS builder
6+
WORKDIR /app
7+
WORKDIR /
8+
9+
COPY Pipfile.lock .
10+
RUN pip install pipenv
11+
RUN pipenv requirements > requirements.txt
12+
RUN pip install --target=/app -r requirements.txt
13+
14+
# --- main
15+
FROM base
16+
COPY --from=builder /app /app
17+
ENV PYTHONPATH=/app
18+
# TODO: replace with your package name
19+
COPY example /app/example
20+
21+
ENTRYPOINT ["python3", "-m", "example"]

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
* [pyproject-pipenv](https://github.com/fopina/pyproject-pipenv) to make sure dependencies in pyproject.toml and Pipfile are in sync
88
* `.github` with actions ready to be used
99
* [test](.github/workflows/test.yml) runs lint checks, unit tests and pyproject-pipenv
10-
* [publish-dev](.github/workflows/publish-dev.yml) publishes feature branches (`dev`/`dev-*`) to [testpypi](https://test.pypi.org) - more about this on [Notes](#feature-branch-publishing)
11-
* [publish-main](.github/workflows/publish-main.yml) publishes semver tags to [pypi](https://pypi.org)
10+
* [publish-dev](.github/workflows/publish-dev.yml) publishes feature branches (`dev`/`dev-*`) to:
11+
* [testpypi](https://test.pypi.org) - more about this on [Notes](#feature-branch-publishing)
12+
* docker image to ghcr.io - remove job if image makes no sense
13+
* [publish-main](.github/workflows/publish-main.yml) publishes semver tags to:
14+
* [pypi](https://pypi.org)
15+
* docker image to ghcr.io
1216

1317
## New project checklist
1418

0 commit comments

Comments
 (0)