Skip to content

Commit 3dcd8be

Browse files
authored
Merge pull request #19 from Staffbase/use-official-docker-build-push-action
Use official docker build push action
2 parents 899b677 + 99deb4a commit 3dcd8be

2 files changed

Lines changed: 58 additions & 38 deletions

File tree

README.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ jobs:
4343
dockerpassword: ${{ secrets.DOCKER_PASSWORD }}
4444
# This is the name of the Docker image for your service.
4545
dockerimage: private/diablo-redbook
46+
# List of build-time variables
47+
dockerbuildargs: "ARG1='one',ARG2='two'"
48+
# Sets the target stage to build
49+
dockerbuildtarget: "runtime"
4650
# The additional arguments you need to build the docker image
47-
dockeradditionalbuildparams: "--target runtime --build-arg ARG1='one' --build-arg ARG2='two'"
4851
gitopstoken: ${{ secrets.GITOPS_TOKEN }}
4952
# The gitopsdev, gitopsstage and gitopsprod values are used to specify which files including the YAML path which should be updated with the new image.
5053
# ATTENTION 1: You must use |- to remove the final newline in the string, otherwise the GitHub Action will fail.
@@ -66,22 +69,24 @@ jobs:
6669

6770
## Inputs
6871

69-
| Name | Description | Default |
70-
| ---- | ----------- | ------- |
71-
| `dockerenabled` | Build and push the Docker Image | `true` |
72-
| `dockerregistry` | Docker Registry | `registry.staffbase.com`|
73-
| `dockerimage` | Docker Image | |
74-
| `dockerusername` | Username for the Docker Registry | |
75-
| `dockerpassword` | Password for the Docker Registry | |
76-
| `dockerfile` | Dockerfile | `./Dockerfile` |
77-
| `dockeradditionalbuildparams` | List of Docker Build Parameters like: "--target runtime --build-arg ARG1=one --build-arg ARG2=two" | |
78-
| `gitopsenabled` | Update the manifest files in the GitOps repository | `true` |
79-
| `gitopsorganization` | GitHub Organization for GitOps | `Staffbase` |
80-
| `gitopsrepository` | GitHub Repository for GitOps | `mops` |
81-
| `gitopsuser` | GitHub User for GitOps | `Staffbot` |
82-
| `gitopsemail` | GitHub User for GitOps | `staffbot@staffbase.com` |
83-
| `gitopstoken` | GitHub Token for GitOps | |
84-
| `gitopsdev` | Files which should be updated by the GitHub Action for DEV | |
85-
| `gitopsstage` | Files which should be updated by the GitHub Action for STAGE | |
86-
| `gitopsprod` | Files which should be updated by the GitHub Action for PROD | |
87-
| `workingdirectory` | The directory in which the GitOps action should be executed. The dockerfile variable should be relative to working directory. | `.` |
72+
| Name | Description | Default |
73+
|-------------------------------|-------------------------------------------------------------------------------------------------------------------------------|--------------------------|
74+
| `dockerenabled` | Build and push the Docker Image | `true` |
75+
| `dockerregistry` | Docker Registry | `registry.staffbase.com` |
76+
| `dockerimage` | Docker Image | |
77+
| `dockerusername` | Username for the Docker Registry | |
78+
| `dockerpassword` | Password for the Docker Registry | |
79+
| `dockerfile` | Dockerfile | `./Dockerfile` |
80+
| `dockeradditionalbuildparams` | List of Docker Build Parameters like: "--target runtime --build-arg ARG1=one --build-arg ARG2=two" | |
81+
| `dockerbuildargs` | List of build-time variables like: "ARG1=one,ARG2=two" | |
82+
| `dockerbuildtarget` | Sets the target stage to build like: "runtime" | |
83+
| `gitopsenabled` | Update the manifest files in the GitOps repository | `true` |
84+
| `gitopsorganization` | GitHub Organization for GitOps | `Staffbase` |
85+
| `gitopsrepository` | GitHub Repository for GitOps | `mops` |
86+
| `gitopsuser` | GitHub User for GitOps | `Staffbot` |
87+
| `gitopsemail` | GitHub User for GitOps | `staffbot@staffbase.com` |
88+
| `gitopstoken` | GitHub Token for GitOps | |
89+
| `gitopsdev` | Files which should be updated by the GitHub Action for DEV | |
90+
| `gitopsstage` | Files which should be updated by the GitHub Action for STAGE | |
91+
| `gitopsprod` | Files which should be updated by the GitHub Action for PROD | |
92+
| `workingdirectory` | The directory in which the GitOps action should be executed. The dockerfile variable should be relative to working directory. | `.` |

action.yml

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ inputs:
2222
description: 'Dockerfile'
2323
required: true
2424
default: './Dockerfile'
25-
dockeradditionalbuildparams:
26-
description: 'List of Docker Build Parameters like: "--target runtime --build-arg ARG1=one --build-arg ARG2=two"'
25+
dockerbuildargs:
26+
description: "List of build-time variables"
27+
required: false
28+
dockerbuildtarget:
29+
description: "Sets the target stage to build"
2730
required: false
2831
gitopsenabled:
2932
description: 'Update manifest files in the GitOps repository'
@@ -99,28 +102,40 @@ runs:
99102
PUSH="false"
100103
fi
101104
105+
TAG_LIST="${{ inputs.dockerregistry }}/${{ inputs.dockerimage }}:${TAG}"
106+
if [[ ! -z "${LATEST}" ]]; then
107+
TAG_LIST+=",${{ inputs.dockerregistry }}/${{ inputs.dockerimage }}:${LATEST}"
108+
fi
109+
110+
echo ::set-output name=tag_list::${TAG_LIST}
102111
echo ::set-output name=tag::${TAG}
103112
echo ::set-output name=latest::${LATEST}
104113
echo ::set-output name=push::${PUSH}
105114
106-
- name: Build and Push the Docker Image
107-
shell: bash
108-
working-directory: ${{ inputs.workingdirectory }}
109-
run: |
110-
if [[ ${{ inputs.dockerenabled }} == "true" ]]; then
111-
echo The working directory is $(pwd)
112-
echo '${{ inputs.dockerpassword }}' | docker login ${{ inputs.dockerregistry }} -u '${{ inputs.dockerusername }}' --password-stdin
113-
114-
docker build ${{ inputs.dockeradditionalbuildparams }} -f ${{ inputs.dockerfile }} -t ${{ inputs.dockerregistry }}/${{ inputs.dockerimage }}:${{ steps.preparation.outputs.tag }} .
115+
- name: Set up Docker Buildx
116+
uses: docker/setup-buildx-action@v1
115117

116-
if [[ ! -z "${{ steps.preparation.outputs.latest }}" ]]; then
117-
docker tag ${{ inputs.dockerregistry }}/${{ inputs.dockerimage }}:${{ steps.preparation.outputs.tag }} ${{ inputs.dockerregistry }}/${{ inputs.dockerimage }}:${{ steps.preparation.outputs.latest }}
118-
fi
118+
- name: Login to Registry
119+
uses: docker/login-action@v1
120+
with:
121+
registry: ${{ inputs.dockerregistry }}
122+
username: ${{ inputs.dockerusername }}
123+
password: ${{ inputs.dockerpassword }}
119124

120-
if [[ ${{ steps.preparation.outputs.push }} == "true" ]]; then
121-
docker push --all-tags ${{ inputs.dockerregistry }}/${{ inputs.dockerimage }}
122-
fi
123-
fi
125+
- name: Build
126+
id: docker_build
127+
if: ${{ inputs.dockerenabled }} == "true"
128+
uses: docker/build-push-action@v2
129+
with:
130+
context: .
131+
push: ${{ steps.preparation.outputs.push }}
132+
file: ${{ inputs.dockerfile }}
133+
target: ${{ inputs.dockerbuildtarget }}
134+
build-args: ${{ inputs.dockerbuildargs }}
135+
tags: ${{ steps.preparation.outputs.tag_list }}
136+
platforms: linux/amd64
137+
cache-from: type=gha
138+
cache-to: type=gha,mode=max
124139

125140
- name: Update Docker Image in Repository
126141
shell: bash

0 commit comments

Comments
 (0)