Skip to content

Commit 7c339a2

Browse files
authored
Rewrite build-push to use mainly Docker actions (#120)
This commits rewrites the `build-publish` workflow to mainly use: - https://github.com/marketplace/actions/docker-metadata-action - https://github.com/marketplace/actions/build-and-push-docker-images As a result of this PR, the following should be true: - the image is built on every PR (as a CI check) - the image is published in this way: | Event | Tags | |-------------------|-----------------------------| | push tag vX.Y.Z | `latest`, `X.Y.Z` | | PR merged to main | `latest`, `sha-<short sha>` |
1 parent 0fca04b commit 7c339a2

File tree

1 file changed

+25
-89
lines changed

1 file changed

+25
-89
lines changed

.github/workflows/build-publish.yaml

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,113 +3,49 @@ name: Build and Publish Docker image
33
on:
44
pull_request:
55
branches:
6-
- main
7-
6+
- main
87
push:
98
branches:
10-
- main
9+
- main
1110
tags:
12-
- v[0-9]+.[0-9]+.[0-9]+
11+
- v[0-9]+.[0-9]+.[0-9]+
1312

1413
jobs:
15-
prepare:
14+
build-and-publish:
1615
runs-on: ubuntu-latest
17-
outputs:
18-
FULL_IMAGE_TAG: ${{ steps.tag.outputs.tag }}
1916
steps:
20-
- name: Set Tag
21-
id: tag
22-
run: |
23-
export CI_COMMIT_SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-8)
24-
echo $CI_COMMIT_SHORT_SHA;
25-
echo ${GITHUB_REF##*/};
26-
echo $version_pattern;
27-
if [[ ${GITHUB_REF} =~ $version_pattern ]]; then
28-
echo "::set-output name=tag::${GITHUB_REF##*/}"
29-
else
30-
echo "::set-output name=tag::${GITHUB_REF##*/}-$CI_COMMIT_SHORT_SHA"
31-
fi
32-
env:
33-
version_pattern: "tags\\/v[0-9]+\\.[0-9]+\\.[0-9]+"
34-
build:
35-
runs-on: ubuntu-latest
36-
needs: prepare
37-
environment:
38-
name: cloudops
17+
- name: Check out the repo
18+
uses: actions/checkout@v3
3919

40-
steps:
41-
- name: Docker image tag
42-
env:
43-
IMAGE_TAG: ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
20+
- name: Build `version.json` file
4421
run: |
45-
echo "Building an image with the following tag:"
46-
echo $IMAGE_TAG
47-
48-
- name: Checkout
49-
uses: actions/checkout@v2
50-
51-
- name: Login to Docker Hub
52-
uses: docker/login-action@v1
53-
with:
54-
username: ${{ secrets.DOCKERHUB_USERNAME }}
55-
password: ${{ secrets.DOCKERHUB_TOKEN }}
56-
57-
- name: Extract metadata (tags, labels) for Docker
58-
uses: docker/metadata-action@v3
59-
with:
60-
images: $GITHUB_REPOSITORY
61-
62-
- name: Build Docker image
63-
env:
64-
IMAGE_TAG: ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
65-
run: |
66-
printf '{\n "commit": "%s",\n "version": "%s",\n "image_tag": "%s",\n "source": "%s",\n "build": "%s"\n}\n' \
22+
printf '{\n "commit": "%s",\n "version": "%s",\n "source": "%s",\n "build": "%s"\n}\n' \
6723
"$GITHUB_SHA" \
6824
"$GITHUB_REF" \
69-
"$IMAGE_TAG" \
7025
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
7126
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > ./version.json
72-
docker build --file Dockerfile -t $GITHUB_REPOSITORY:$IMAGE_TAG .
73-
74-
- name: Save image as artifact
75-
env:
76-
IMAGE_TAG: ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
77-
run: |
78-
docker save -o /tmp/container.tar.gz $GITHUB_REPOSITORY:$IMAGE_TAG
79-
80-
- uses: actions/upload-artifact@v3
81-
with:
82-
name: container-image
83-
path: /tmp/container.tar.gz
84-
retention-days: 1 # Instead of default 90 days
85-
86-
publish:
87-
runs-on: ubuntu-latest
88-
needs:
89-
- prepare
90-
- build
91-
if: ${{ !github.event.pull_request }} # Exclude pull-requests (see `on:` above)
9227
93-
steps:
94-
- uses: actions/download-artifact@v3
28+
- name: Extract metadata (tags, labels) for Docker
29+
id: meta
30+
uses: docker/metadata-action@v4
9531
with:
96-
name: container-image
97-
path: /tmp/container.tar.gz
98-
99-
- name: Load container from artifact
100-
run: |
101-
docker load -i /tmp/container.tar.gz
32+
images: $GITHUB_REPOSITORY
33+
# https://github.com/marketplace/actions/docker-metadata-action#tags-input
34+
tags: |
35+
type=semver,pattern={{version}}
36+
type=raw,value=latest,enable={{is_default_branch}}
37+
type=sha,enable={{is_default_branch}}
10238
10339
- name: Login to Docker Hub
104-
uses: docker/login-action@v1
40+
if: github.event_name != 'pull_request'
41+
uses: docker/login-action@v2
10542
with:
10643
username: ${{ secrets.DOCKERHUB_USERNAME }}
10744
password: ${{ secrets.DOCKERHUB_TOKEN }}
10845

109-
- name: Build Docker image
110-
env:
111-
IMAGE_TAG: ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
112-
run: |
113-
docker image tag $GITHUB_REPOSITORY:$IMAGE_TAG $GITHUB_REPOSITORY:latest
114-
docker push $GITHUB_REPOSITORY:$IMAGE_TAG
115-
docker push $GITHUB_REPOSITORY:latest
46+
- name: Build and push
47+
uses: docker/build-push-action@v3
48+
with:
49+
context: .
50+
push: ${{ github.event_name != 'pull_request' }}
51+
tags: ${{ steps.meta.outputs.tags }}

0 commit comments

Comments
 (0)