Skip to content

Commit

Permalink
Merge pull request #128 from felipecrs/felipecassiors/docker
Browse files Browse the repository at this point in the history
Run Docker action on pull requests
  • Loading branch information
matejak committed Sep 20, 2020
2 parents 928aa1c + b1574a3 commit e8d9dd8
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions .github/workflows/docker-build-and-push.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
name: Docker Build and Push
name: Docker build and push

on:
push:
pull_request:

jobs:
docker:
docker-build-and-push:
name: Docker build and push

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

# This is a workaround due to
# https://github.community/t/how-to-use-env-context/16975
# and https://github.com/docker/build-push-action/issues/43
- name: Set environment variables
run: |
# These env variables are set here because of
# https://github.community/t/how-to-use-env-context/16975
DOCKER_USERNAME=matejak
echo "::set-env name=DOCKER_USERNAME::$DOCKER_USERNAME"
Expand All @@ -25,16 +28,36 @@ jobs:
version="${GITHUB_REF#refs/tags/}"
DOCKER_TAGS="latest,$version"
unset version
elif [[ "$GITHUB_REF" = refs/pull/* ]]; then
pr_number="${GITHUB_REF##*refs/pull/}"
pr_number="${pr_number%%/merge*}"
DOCKER_TAGS="pr-$pr_number"
unset pr_number
else
branch="${GITHUB_REF#refs/heads/}"
branch="branch-${branch/\//-}"
DOCKER_TAGS="$branch"
branch="${branch//\//-}"
DOCKER_TAGS="branch-$branch"
unset branch
fi
echo "::set-env name=DOCKER_TAGS::$DOCKER_TAGS"
echo "The tags were set to '$DOCKER_TAGS'"
- uses: docker/build-push-action@v1
# This is a workaround due to
# https://github.com/docker/build-push-action/issues/85
- if: startsWith(github.ref, 'refs/pull/')
name: Docker build
uses: docker/build-push-action@v1
with:
push: false
repository: ${{ env.DOCKER_IMAGE }}
dockerfile: docker/Dockerfile
add_git_labels: true
tags: ${{ env.DOCKER_TAGS }}
tag_with_sha: true

- if: "! startsWith(github.ref, 'refs/pull/')"
name: Docker build and push
uses: docker/build-push-action@v1
with:
username: ${{ env.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
Expand All @@ -44,7 +67,7 @@ jobs:
tags: ${{ env.DOCKER_TAGS }}
tag_with_sha: true

- if: github.ref == 'refs/heads/master'
- if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')
name: Update Docker Hub Description
uses: peter-evans/dockerhub-description@v2
env:
Expand Down

0 comments on commit e8d9dd8

Please sign in to comment.