Skip to content

Commit baefb47

Browse files
ci: automate Docker image rebuild, test, and publish in GitHub Actions and GitLab CI (solves #20)
1 parent 511a2c2 commit baefb47

3 files changed

Lines changed: 133 additions & 9 deletions

File tree

.github/workflows/docker-image.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Docker image CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- Dockerfile
9+
- requirements.txt
10+
- Pipfile
11+
- Pipfile.lock
12+
- .github/workflows/docker-image.yml
13+
pull_request:
14+
paths:
15+
- Dockerfile
16+
- requirements.txt
17+
- Pipfile
18+
- Pipfile.lock
19+
- .github/workflows/docker-image.yml
20+
21+
concurrency:
22+
group: docker-image-${{ github.ref }}
23+
cancel-in-progress: true
24+
25+
env:
26+
IMAGE_NAME: ghcr.io/${{ github.repository }}
27+
28+
jobs:
29+
build-test-push:
30+
name: Build, test and publish docker image
31+
runs-on: ubuntu-latest
32+
permissions:
33+
contents: read
34+
packages: write
35+
steps:
36+
- name: Checkout repository
37+
uses: actions/checkout@v4
38+
39+
- name: Set up Docker Buildx
40+
uses: docker/setup-buildx-action@v3
41+
42+
- name: Build image for tests
43+
run: |
44+
docker buildx build \
45+
--load \
46+
--cache-from type=gha \
47+
--cache-to type=gha,mode=max \
48+
--tag local-ci-image:${{ github.sha }} \
49+
.
50+
51+
- name: Run tests inside built image
52+
run: |
53+
docker run --rm \
54+
-v "${{ github.workspace }}:/app" \
55+
-w /app \
56+
local-ci-image:${{ github.sha }} \
57+
pytest src/test/test_api.py
58+
59+
- name: Log in to GitHub Container Registry
60+
if: github.event_name == 'push'
61+
uses: docker/login-action@v3
62+
with:
63+
registry: ghcr.io
64+
username: ${{ github.actor }}
65+
password: ${{ secrets.GITHUB_TOKEN }}
66+
67+
- name: Extract image metadata
68+
if: github.event_name == 'push'
69+
id: meta
70+
uses: docker/metadata-action@v5
71+
with:
72+
images: ${{ env.IMAGE_NAME }}
73+
tags: |
74+
type=raw,value=latest
75+
type=sha,prefix=sha-
76+
type=raw,value=run-${{ github.run_number }}
77+
78+
- name: Build and push image
79+
if: github.event_name == 'push'
80+
uses: docker/build-push-action@v6
81+
with:
82+
context: .
83+
push: true
84+
tags: ${{ steps.meta.outputs.tags }}
85+
labels: ${{ steps.meta.outputs.labels }}
86+
cache-from: type=gha
87+
cache-to: type=gha,mode=max

.github/workflows/test_api.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
on:
2-
[push]
2+
push:
3+
paths:
4+
- src/**
5+
- requirements.txt
6+
- pytest.ini
7+
- .github/workflows/test_api.yml
8+
pull_request:
9+
paths:
10+
- src/**
11+
- requirements.txt
12+
- pytest.ini
13+
- .github/workflows/test_api.yml
314

415
jobs:
516
pytest:
617
name: Pytest
718
runs-on: ubuntu-latest
8-
container:
9-
image: docker.pkg.github.com/aleksandr-kotlyar/python_and_gitlab/python-3.7.6-alpine-req:ver-14-mimesis-4.1.2
10-
credentials:
11-
username: ${{ github.actor }}
12-
password: ${{ secrets.TRAFFIC_TOKEN }}
1319
steps:
14-
- uses: actions/checkout@v2
15-
- run: pytest src/test/test_api.py
16-
continue-on-error: true
20+
- uses: actions/checkout@v4
21+
- uses: actions/setup-python@v5
22+
with:
23+
python-version: "3.7"
24+
- run: |
25+
python -m pip install --upgrade pip
26+
pip install -r requirements.txt
27+
- run: pytest src/test/test_api.py

.gitlab/build.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,29 @@ Build:
2929
DOCKERFILE_PATH: ".docker/$IMAGE_CATEGORY/Dockerfile"
3030
BUILD_CONTEXT: "."
3131
BUILD_ARG: "--build-arg ALLURE_VERSION=$ALLURE_VERSION"
32+
33+
Auto:docker:rebuild:test:publish:
34+
extends: .build
35+
stage: build
36+
variables:
37+
DOCKERFILE_PATH: "Dockerfile"
38+
BUILD_CONTEXT: "."
39+
BUILD_ARG: ""
40+
IMAGE_NAME_SHA: "$CI_REGISTRY_IMAGE:sha-$CI_COMMIT_SHORT_SHA"
41+
IMAGE_NAME_LATEST: "$CI_REGISTRY_IMAGE:latest"
42+
TEST_PATH: "src/test/test_api.py"
43+
script:
44+
- docker build $BUILD_ARG -f $DOCKERFILE_PATH -t $IMAGE_NAME_SHA $BUILD_CONTEXT
45+
- docker run --rm -v "$CI_PROJECT_DIR:/app" -w /app $IMAGE_NAME_SHA pytest $TEST_PATH
46+
- docker tag $IMAGE_NAME_SHA $IMAGE_NAME_LATEST
47+
- docker push $IMAGE_NAME_SHA
48+
- docker push $IMAGE_NAME_LATEST
49+
rules:
50+
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
51+
changes:
52+
- Dockerfile
53+
- requirements.txt
54+
- Pipfile
55+
- Pipfile.lock
56+
- .gitlab/.gitlab-ci.yml
57+
- .gitlab/build.yml

0 commit comments

Comments
 (0)