|
| 1 | +name: "docker" |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: [opened, synchronize, reopened] |
| 5 | + release: |
| 6 | + types: |
| 7 | + # "released" excludes pre-releases |
| 8 | + # "published" is either a release or a pre-release |
| 9 | + - published |
| 10 | +jobs: |
| 11 | + build-and-push: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout source code at current commit |
| 15 | + uses: actions/checkout@v2 |
| 16 | + |
| 17 | + # Based off of Geodesic docker workflow: |
| 18 | + # https://github.com/cloudposse/geodesic/blob/master/.github/workflows/docker.yml |
| 19 | + - name: Prepare tags for Docker image |
| 20 | + id: prepare |
| 21 | + run: | |
| 22 | + echo ::set-output name=publish::${{ (github.event_name == 'release' && github.event.action == 'published') || (github.event.pull_request.head.repo.full_name == github.repository) }} |
| 23 | + if [[ $GITHUB_REF == refs/tags/* ]]; then |
| 24 | + VERSION=${GITHUB_REF#refs/tags/} |
| 25 | + fi |
| 26 | +
|
| 27 | + # By default, we tag our image with the short sha on all PR pushes |
| 28 | + TAGS="${{ github.repository }}:sha-${GITHUB_SHA:0:7}" |
| 29 | +
|
| 30 | + # If this is a tagged release, then we tag w/ the semver tag + latest |
| 31 | + if [[ -n $VERSION ]]; then |
| 32 | + TAGS="$TAGS,${{ github.repository }}:${VERSION},${{ github.repository }}:latest" |
| 33 | + fi |
| 34 | +
|
| 35 | + printf "Tagging with %s\n" "${TAGS}" |
| 36 | + echo "::set-output name=tags::${TAGS}" |
| 37 | + - name: Set up Docker Buildx |
| 38 | + uses: docker/setup-buildx-action@v1 |
| 39 | + - name: Login to DockerHub |
| 40 | + if: steps.prepare.outputs.publish == 'true' |
| 41 | + uses: docker/login-action@v1 |
| 42 | + with: |
| 43 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 44 | + password: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 45 | + - name: "Build and push docker image to DockerHub" |
| 46 | + id: docker_build |
| 47 | + uses: docker/build-push-action@v2 |
| 48 | + with: |
| 49 | + push: ${{ steps.prepare.outputs.publish == 'true' }} |
| 50 | + tags: ${{ steps.prepare.outputs.tags }} |
| 51 | + file: ./Dockerfile |
0 commit comments