Skip to content

Commit e8d9dd8

Browse files
authored
Merge pull request #128 from felipecrs/felipecassiors/docker
Run Docker action on pull requests
2 parents 928aa1c + b1574a3 commit e8d9dd8

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

.github/workflows/docker-build-and-push.yaml

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
name: Docker Build and Push
1+
name: Docker build and push
22

33
on:
44
push:
5+
pull_request:
56

67
jobs:
7-
docker:
8+
docker-build-and-push:
9+
name: Docker build and push
10+
811
runs-on: ubuntu-latest
912

1013
steps:
1114
- uses: actions/checkout@v2
1215

16+
# This is a workaround due to
17+
# https://github.community/t/how-to-use-env-context/16975
18+
# and https://github.com/docker/build-push-action/issues/43
1319
- name: Set environment variables
1420
run: |
15-
# These env variables are set here because of
16-
# https://github.community/t/how-to-use-env-context/16975
17-
1821
DOCKER_USERNAME=matejak
1922
echo "::set-env name=DOCKER_USERNAME::$DOCKER_USERNAME"
2023
@@ -25,16 +28,36 @@ jobs:
2528
version="${GITHUB_REF#refs/tags/}"
2629
DOCKER_TAGS="latest,$version"
2730
unset version
31+
elif [[ "$GITHUB_REF" = refs/pull/* ]]; then
32+
pr_number="${GITHUB_REF##*refs/pull/}"
33+
pr_number="${pr_number%%/merge*}"
34+
DOCKER_TAGS="pr-$pr_number"
35+
unset pr_number
2836
else
2937
branch="${GITHUB_REF#refs/heads/}"
30-
branch="branch-${branch/\//-}"
31-
DOCKER_TAGS="$branch"
38+
branch="${branch//\//-}"
39+
DOCKER_TAGS="branch-$branch"
3240
unset branch
3341
fi
3442
echo "::set-env name=DOCKER_TAGS::$DOCKER_TAGS"
3543
echo "The tags were set to '$DOCKER_TAGS'"
3644
37-
- uses: docker/build-push-action@v1
45+
# This is a workaround due to
46+
# https://github.com/docker/build-push-action/issues/85
47+
- if: startsWith(github.ref, 'refs/pull/')
48+
name: Docker build
49+
uses: docker/build-push-action@v1
50+
with:
51+
push: false
52+
repository: ${{ env.DOCKER_IMAGE }}
53+
dockerfile: docker/Dockerfile
54+
add_git_labels: true
55+
tags: ${{ env.DOCKER_TAGS }}
56+
tag_with_sha: true
57+
58+
- if: "! startsWith(github.ref, 'refs/pull/')"
59+
name: Docker build and push
60+
uses: docker/build-push-action@v1
3861
with:
3962
username: ${{ env.DOCKER_USERNAME }}
4063
password: ${{ secrets.DOCKER_PASSWORD }}
@@ -44,7 +67,7 @@ jobs:
4467
tags: ${{ env.DOCKER_TAGS }}
4568
tag_with_sha: true
4669

47-
- if: github.ref == 'refs/heads/master'
70+
- if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')
4871
name: Update Docker Hub Description
4972
uses: peter-evans/dockerhub-description@v2
5073
env:

0 commit comments

Comments
 (0)