Skip to content

Commit e83406e

Browse files
authored
Improve deploy stage in Github Actions CI workflow (#73)
The improvements are: - Upgrade to docker/build-push-action@v2 - For PR events, run docker builds without pushing - Use docker buildx to build multi-arch images (amd64, arm64, ppc64le, s390x)
1 parent 16ddc79 commit e83406e

File tree

3 files changed

+50
-32
lines changed

3 files changed

+50
-32
lines changed

.github/workflows/ci.yml

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: CI
22

33
on:
4-
push:
5-
branches: [ master ]
4+
push: {}
65
pull_request:
76
branches: [ master ]
87

@@ -59,29 +58,55 @@ jobs:
5958

6059
deploy:
6160
name: Deploy
62-
needs: test
63-
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
61+
needs:
62+
- test
63+
- lint
6464
runs-on: ubuntu-latest
6565
steps:
6666

6767
- name: Check out code into the Go module directory
6868
uses: actions/checkout@v2
69-
70-
- name: Push helm-operator image to Quay
71-
uses: docker/build-push-action@v1
69+
with:
70+
fetch-depth: 0
71+
72+
- name: Prepare
73+
id: prep
74+
run: |
75+
TAG=ci
76+
if [[ $GITHUB_REF == refs/tags/* ]]; then
77+
TAG=${GITHUB_REF#refs/tags/}
78+
elif [[ $GITHUB_REF == refs/heads/* ]]; then
79+
TAG=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
80+
elif [[ $GITHUB_REF == refs/pull/* ]]; then
81+
TAG=pr-${{ github.event.number }}
82+
fi
83+
84+
echo ::set-output name=tag::${TAG}
85+
echo ::set-output name=platforms::linux/amd64,linux/arm64,linux/ppc64le,linux/s390x
86+
87+
- name: Set up Docker Buildx
88+
uses: docker/setup-buildx-action@v1
89+
90+
- name: Login to Quay
91+
if: github.event_name != 'pull_request'
92+
uses: docker/login-action@v1
7293
with:
7394
username: ${{ secrets.QUAY_USERNAME }}
7495
password: ${{ secrets.QUAY_PASSWORD }}
7596
registry: quay.io
76-
repository: joelanford/helm-operator
77-
tags: master
7897

79-
- name: Push example nginx-operator image to Quay
80-
uses: docker/build-push-action@v1
98+
- name: Build helm-operator image
99+
uses: docker/build-push-action@v2
81100
with:
82-
path: example/
83-
username: ${{ secrets.QUAY_USERNAME }}
84-
password: ${{ secrets.QUAY_PASSWORD }}
85-
registry: quay.io
86-
repository: joelanford/nginx-operator
87-
tags: latest
101+
context: .
102+
platforms: ${{ steps.prep.outputs.platforms }}
103+
push: ${{ github.event_name != 'pull_request' }}
104+
tags: quay.io/joelanford/helm-operator:${{ steps.prep.outputs.tag }}
105+
106+
- name: Build example nginx-operator image
107+
uses: docker/build-push-action@v2
108+
with:
109+
context: ./example
110+
platforms: ${{ steps.prep.outputs.platforms }}
111+
push: ${{ github.event_name != 'pull_request' }}
112+
tags: quay.io/joelanford/nginx-operator:${{ steps.prep.outputs.tag }}

Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Build the manager binary
2-
FROM golang:1.15 as builder
2+
FROM --platform=$BUILDPLATFORM golang:1.15 as builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
35

46
WORKDIR /workspace
57
# Copy the Go Modules manifests
@@ -10,18 +12,16 @@ COPY go.sum go.sum
1012
RUN go mod download
1113

1214
# Copy the go source
13-
COPY main.go main.go
14-
COPY pkg/ pkg/
15-
COPY internal/ internal/
15+
COPY . .
1616

1717
# Build
18-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o helm-operator main.go
18+
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH make build
1919

2020
# Use distroless as minimal base image to package the manager binary
2121
# Refer to https://github.com/GoogleContainerTools/distroless for more details
2222
FROM gcr.io/distroless/static:nonroot
2323
WORKDIR /
24-
COPY --from=builder /workspace/helm-operator .
24+
COPY --from=builder /workspace/bin/helm-operator .
2525
USER nonroot:nonroot
2626

2727
ENTRYPOINT ["/helm-operator", "run"]

Makefile

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GO_BUILD_ARGS = \
2424
-X '$(VERSION_PKG).GitCommit=$(GIT_COMMIT)' \
2525
" \
2626

27-
#all: manager
27+
export GO111MODULE = on
2828

2929
# Run tests
3030
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
@@ -35,7 +35,7 @@ test: fmt vet
3535

3636
# Build manager binary
3737
build: fmt vet
38-
go build $(GO_BUILD_ARGS) -o bin/helm-operator main.go
38+
CGO_ENABLED=0 go build $(GO_BUILD_ARGS) -o bin/helm-operator main.go
3939

4040
# Run go fmt against code
4141
fmt:
@@ -49,13 +49,6 @@ lint: golangci-lint
4949
$(GOLANGCI_LINT) run
5050
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
5151
$(GOLANGCI_LINT) run --fix
52-
# Build the docker image
53-
docker-build:
54-
docker build -t quay.io/joelanford/helm-operator:$(VERSION) .
55-
56-
# Push the docker image
57-
docker-push:
58-
docker push quay.io/joelanford/helm-operator:$(VERSION)
5952

6053
# find or download controller-gen
6154
# download controller-gen if necessary

0 commit comments

Comments
 (0)