Skip to content

Commit 717ed57

Browse files
authored
Refactor Docker workflow for improved structure
1 parent cfa7c0d commit 717ed57

File tree

1 file changed

+28
-57
lines changed

1 file changed

+28
-57
lines changed

.github/workflows/docker-image.yml

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,42 @@
1-
# This workflow uses actions that are not certified by GitHub.
2-
# They are provided by a third-party and are governed by
3-
# separate terms of service, privacy policy, and support
4-
# documentation.
5-
# Triggers a Docker workflow on completion of the "build" workflow but
6-
# pushes to GitHub Container Registry
7-
#
8-
# Author: Edward Lewis - 2021
9-
10-
name: GHCR
1+
name: Build and Publish Docker Image (Release)
112

123
on:
13-
workflow_dispatch:
144
push:
15-
branches:
16-
- 'master'
175
tags:
18-
- '*'
19-
pull_request:
20-
branches:
21-
- 'master'
6+
- "*.*.*" # Adjust if you use a different tagging scheme
227

23-
env:
24-
REGISTRY: ghcr.io
25-
IMAGE_NAME: pygeodatacrawler
8+
permissions:
9+
contents: read
10+
packages: write
2611

2712
jobs:
28-
# Push image to GitHub Packages.
29-
# See also https://docs.docker.com/docker-hub/builds/
30-
push:
13+
build-and-push:
3114
runs-on: ubuntu-latest
32-
if: ${{ github.event.workflow_run.conclusion == 'success' }}
33-
permissions:
34-
packages: write
35-
contents: read
3615

3716
steps:
38-
- uses: actions/checkout@v2
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
3919

40-
- name: Build image
41-
run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"
20+
- name: Set up Docker Buildx
21+
uses: docker/setup-buildx-action@v3
4222

43-
- name: Log in to registry
44-
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
23+
- name: Log in to GitHub Container Registry
24+
uses: docker/login-action@v3
25+
with:
26+
registry: ghcr.io
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
4529

46-
- name: Rename image for publication
30+
- name: Extract tag name
31+
id: vars
4732
run: |
48-
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
49-
50-
# Change all uppercase to lowercase
51-
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
52-
# Strip git ref prefix from version
53-
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
54-
# Strip "v" prefix from tag name
55-
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
56-
# Use Docker `latest` tag convention
57-
[ "$VERSION" == "master" ] && VERSION=latest
58-
echo IMAGE_ID=$IMAGE_ID
59-
echo VERSION=$VERSION
60-
echo FULLCONTAINERNAME=$IMAGE_ID:$VERSION >> $GITHUB_ENV
61-
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
62-
63-
- name: Push to CR
64-
run: docker push ${{ env.FULLCONTAINERNAME }}
65-
66-
on-failure:
67-
runs-on: ubuntu-latest
68-
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
69-
steps:
70-
- name: Print Test Fail
71-
run: echo Tests Failed
33+
echo "TAG=${GITHUB_REF_NAME}" >> $GITHUB_OUTPUT
34+
35+
- name: Build and push Docker image
36+
uses: docker/build-push-action@v5
37+
with:
38+
context: .
39+
push: true
40+
tags: |
41+
ghcr.io/${{ github.repository }}:${{ steps.vars.outputs.TAG }}
42+
ghcr.io/${{ github.repository }}:latest

0 commit comments

Comments
 (0)