Skip to content

Commit

Permalink
Add GoReleaser
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Apr 21, 2021
1 parent a69d124 commit c0b36a5
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 58 deletions.
47 changes: 26 additions & 21 deletions .github/workflows/edge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ env:
HELM_CHART_VERSION: 0.0.0-edge
GIT_NAME: NGINX Kubernetes Team
GIT_MAIL: [email protected]
VERSION: edge

jobs:

Expand All @@ -46,6 +45,8 @@ jobs:
steps:
- name: Checkout Repository
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Output Variables
id: commit
run: |
Expand All @@ -56,24 +57,26 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Determine GOPATH
run: echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
- name: Check if CRDs changed
run: |
make update-crds && git diff --name-only --exit-code deployments/common/crds*
- name: Check if Codegen changed
run: |
make update-codegen && git diff --name-only --exit-code pkg/**/zz_generated.deepcopy.go
- name: Build Binary
run: >
make build
VERSION=${{ env.VERSION }}
IC_VERSION=${{ env.VERSION }}-${{ steps.commit.outputs.sha }}
GIT_COMMIT=${{ steps.commit.outputs.sha }}
- name: Build binaries
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: --rm-dist --debug --skip-publish --snapshot
env:
GOFLAGS: '-gcflags=-trimpath=${{ github.workspace }} -asmflags=-trimpath=${{ github.workspace }}'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOPATH: ${{ env.GOPATH }}
- name: Store Artifacts in Cache
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/nginx-ingress
path: ${{ github.workspace }}/dist
key: nginx-ingress-${{ github.run_id }}-${{ github.run_number }}

unit-tests:
Expand Down Expand Up @@ -171,7 +174,7 @@ jobs:
- name: Fetch Cached Artifacts
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/nginx-ingress
path: ${{ github.workspace }}/dist
key: nginx-ingress-${{ github.run_id }}-${{ github.run_number }}
- name: Docker Buildx
uses: docker/setup-buildx-action@v1
Expand All @@ -191,7 +194,7 @@ jobs:
context: '.'
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
target: local
target: goreleaser
tags: ${{ matrix.image }}:${{ matrix.tag }}
load: true
build-args: |
Expand All @@ -204,7 +207,7 @@ jobs:
context: '.'
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
target: local
target: goreleaser
tags: ${{ matrix.image }}:${{ matrix.tag }}
load: true
secrets: |
Expand All @@ -219,7 +222,7 @@ jobs:
with:
file: build/Dockerfile
context: '.'
target: local
target: goreleaser
tags: ${{ matrix.image }}:${{ matrix.tag }}
load: true
secrets: |
Expand Down Expand Up @@ -296,7 +299,7 @@ jobs:
- name: Fetch Cached Artifacts
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/nginx-ingress
path: ${{ github.workspace }}/dist
key: nginx-ingress-${{ github.run_id }}-${{ github.run_number }}
- name: Docker Buildx
uses: docker/setup-buildx-action@v1
Expand All @@ -316,7 +319,7 @@ jobs:
context: '.'
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
target: local
target: goreleaser
tags: nginx-plus-ingress-ap-openshift:${{ github.sha }}
secrets: |
"nginx-repo.crt=${{ secrets.KIC_NGINX_AP_CRT }}"
Expand Down Expand Up @@ -353,7 +356,7 @@ jobs:
- name: Fetch Cached Artifacts
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/nginx-ingress
path: ${{ github.workspace }}/dist
key: nginx-ingress-${{ github.run_id }}-${{ github.run_number }}
- name: Docker Buildx
uses: docker/setup-buildx-action@v1
Expand All @@ -373,7 +376,7 @@ jobs:
context: '.'
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
target: local
target: goreleaser
tags: ${{ matrix.image }}:${{ matrix.tag }}
load: true
build-args: |
Expand All @@ -385,7 +388,7 @@ jobs:
context: '.'
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
target: local
target: goreleaser
tags: ${{ matrix.image }}:${{ matrix.tag }}
load: true
secrets: |
Expand Down Expand Up @@ -463,10 +466,11 @@ jobs:
id: commit
run: |
echo "::set-output name=sha::$(echo ${GITHUB_SHA} | cut -c1-7)"
echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"
- name: Fetch Cached Artifacts
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/nginx-ingress
path: ${{ github.workspace }}/dist
key: nginx-ingress-${{ github.run_id }}-${{ github.run_number }}
- name: Docker Buildx
uses: docker/setup-buildx-action@v1
Expand All @@ -491,12 +495,13 @@ jobs:
context: '.'
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
target: local
target: goreleaser
tags: nginx/${{ matrix.image }}:${{ matrix.tag }}
platforms: linux/arm,linux/arm64,linux/amd64,linux/ppc64le,linux/s390x
push: true
build-args: |
BUILD_OS=${{ matrix.type }}
IC_VERSION=${{ env.VERSION }}-${{ steps.commit.outputs.sha }}
IC_VERSION=${{ steps.commit.outputs.tag }}-SNAPSHOT-${{ steps.commit.outputs.sha }}
package-helm:
name: Package Helm Chart
Expand Down
26 changes: 16 additions & 10 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,30 @@ jobs:
echo "::set-output name=sha::$(echo ${GITHUB_SHA} | cut -c1-7)"
- name: Determine Go version from go.mod
run: echo "GO_VERSION=$(grep "go 1." go.mod | cut -d " " -f 2)" >> $GITHUB_ENV
- name: Setup Golang Environment
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Determine GOPATH
run: echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
- name: Check if CRDs changed
run: |
make update-crds && git diff --name-only --exit-code deployments/common/crds*
- name: Check if Codegen changed
run: |
make update-codegen && git diff --name-only --exit-code pkg/**/zz_generated.deepcopy.go
- name: Build Binary
run: >
make build
VERSION=${{ env.VERSION }}
IC_VERSION=${{ env.VERSION }}-${{ steps.commit.outputs.sha }}
GIT_COMMIT=${{ steps.commit.outputs.sha }}
- name: Build binaries
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: --rm-dist --debug --skip-publish --snapshot
env:
GOFLAGS: '-gcflags=-trimpath=${{ github.workspace }} -asmflags=-trimpath=${{ github.workspace }}'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOPATH: ${{ env.GOPATH }}
- name: Store Artifacts in Cache
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/nginx-ingress
path: ${{ github.workspace }}/dist
key: nginx-ingress-${{ github.run_id }}-${{ github.run_number }}

unit-tests:
Expand Down Expand Up @@ -291,7 +297,7 @@ jobs:
- name: Fetch Cached Artifacts
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/nginx-ingress
path: ${{ github.workspace }}/dist
key: nginx-ingress-${{ github.run_id }}-${{ github.run_number }}
- name: Docker Buildx
uses: docker/setup-buildx-action@v1
Expand Down Expand Up @@ -430,7 +436,7 @@ jobs:
- name: Fetch Cached Artifacts
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/nginx-ingress
path: ${{ github.workspace }}/dist
key: nginx-ingress-${{ github.run_id }}-${{ github.run_number }}
- name: Docker Buildx
uses: docker/setup-buildx-action@v1
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ hack/controller-gen-*
docs-web/.netlify/state.json
site/
venv/

dist/
31 changes: 31 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
env:
- GO111MODULE=on
before:
hooks:
- go mod tidy
- go mod verify
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64
- arm
- arm64
- ppc64le
- s390x
goarm:
- 7
flags:
- -trimpath
gcflags:
- all=-trimpath={{.Env.GOPATH}}
asmflags:
- all=-trimpath={{.Env.GOPATH}}
main: ./cmd/nginx-ingress/main.go
binary: nginx-ingress
archives:
- format: binary
changelog:
skip: true
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
VERSION = 1.11.1
TAG = $(VERSION)
PREFIX = nginx/nginx-ingress
GIT_COMMIT = $(shell git rev-parse HEAD)
GIT_COMMIT_SHORT = $(shell echo ${GIT_COMMIT} | cut -c1-7)
GIT_TAG = $(shell git describe --tags --abbrev=0)
DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
VERSION = $(GIT_TAG)-SNAPSHOT-$(GIT_COMMIT_SHORT)
TAG = $(VERSION)
TARGET ?= local

override DOCKER_BUILD_OPTIONS += --build-arg IC_VERSION=$(VERSION)-$(GIT_COMMIT) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg VERSION=$(VERSION)
override DOCKER_BUILD_OPTIONS += --build-arg IC_VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg DATE=$(DATE)
DOCKER_CMD = docker build $(DOCKER_BUILD_OPTIONS) --target $(TARGET) -f build/Dockerfile -t $(PREFIX):$(TAG) .
PLUS_ARGS = --build-arg PLUS=-plus --secret id=nginx-repo.crt,src=nginx-repo.crt --secret id=nginx-repo.key,src=nginx-repo.key

GIT_COMMIT = $(shell git rev-parse --short HEAD)

export DOCKER_BUILDKIT = 1

.DEFAULT_GOAL:=help
Expand Down Expand Up @@ -50,7 +52,7 @@ build: ## Build Ingress Controller binary
@docker -v || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with Docker\n"; exit $$code)
ifeq (${TARGET},local)
@go version || (code=$$?; printf "\033[0;31mError\033[0m: unable to build locally, try using the parameter TARGET=container\n"; exit $$code)
CGO_ENABLED=0 GO111MODULE=on GOFLAGS='$(GOFLAGS)' GOOS=linux go build -ldflags "-s -w -X main.version=${VERSION} -X main.gitCommit=${GIT_COMMIT}" -o nginx-ingress github.com/nginxinc/kubernetes-ingress/cmd/nginx-ingress
CGO_ENABLED=0 GO111MODULE=on GOOS=linux go build -trimpath -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=$(DATE)" -o nginx-ingress github.com/nginxinc/kubernetes-ingress/cmd/nginx-ingress
endif

.PHONY: debian-image
Expand Down
28 changes: 18 additions & 10 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.0-experimental
# syntax=docker/dockerfile:1.2
ARG BUILD_OS=debian

############################################# Base image for Debian #############################################
Expand Down Expand Up @@ -263,7 +263,7 @@ COPY --from=tracer-downloader /usr/local/lib/libjaegertracing_plugin.so /usr/loc


############################################# Create common files, permissions and setcap #############################################
FROM $BUILD_OS as files
FROM $BUILD_OS as common

ARG PLUS

Expand All @@ -272,7 +272,7 @@ RUN mkdir -p /var/lib/nginx /etc/nginx/secrets /etc/nginx/stream-conf.d \
&& chown -R nginx:0 /etc/nginx /var/cache/nginx /var/lib/nginx \
&& rm -f /etc/nginx/conf.d/* /etc/apt/apt.conf.d/90nginx /etc/apt/sources.list.d/nginx-plus.list

COPY internal/configs/version1/nginx$PLUS.ingress.tmpl \
COPY --chown=nginx:0 internal/configs/version1/nginx$PLUS.ingress.tmpl \
internal/configs/version1/nginx$PLUS.tmpl \
internal/configs/version2/nginx$PLUS.virtualserver.tmpl \
internal/configs/version2/nginx$PLUS.transportserver.tmpl /
Expand All @@ -284,28 +284,36 @@ COPY internal/configs/version1/nginx$PLUS.ingress.tmpl \
EXPOSE 80 443

ENTRYPOINT ["/nginx-ingress"]
USER nginx


############################################# Build nginx-ingress in golang container #############################################
FROM golang:1.16-alpine AS builder
ARG VERSION
ARG IC_VERSION
ARG GIT_COMMIT
ARG DATE
ARG TARGETARCH

WORKDIR /go/src/github.com/nginxinc/kubernetes-ingress/nginx-ingress/cmd/nginx-ingress
COPY go.mod go.sum /go/src/github.com/nginxinc/kubernetes-ingress/nginx-ingress/
RUN go mod download
COPY . /go/src/github.com/nginxinc/kubernetes-ingress/nginx-ingress/
RUN CGO_ENABLED=0 GO111MODULE=on go build -ldflags "-s -w -X main.version=${VERSION} -X main.gitCommit=${GIT_COMMIT}" -o /nginx-ingress
RUN CGO_ENABLED=0 GO111MODULE=on GOOS=linux GOARCH=$TARGETARCH go build -trimpath -ldflags "-s -w -X main.version=${IC_VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o /nginx-ingress


############################################# Create image with nginx-ingress built in container #############################################
FROM files AS container
FROM common AS container
COPY --chown=nginx:0 --from=builder /nginx-ingress /

USER nginx


############################################# Create image with nginx-ingress built locally #############################################
FROM files AS local
FROM common AS local
COPY --chown=nginx:0 nginx-ingress /

USER nginx

############################################# Create image with nginx-ingress built by GoReleaser #############################################
FROM common AS goreleaser
ARG TARGETARCH
ARG TARGETVARIANT

COPY --chown=nginx:0 dist/kubernetes-ingress_linux_$TARGETARCH${TARGETVARIANT:+_7}/nginx-ingress /
20 changes: 15 additions & 5 deletions build/DockerfileWithAppProtectForPlusForOpenShift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.0-experimental
# syntax=docker/dockerfile:1.2
FROM registry.access.redhat.com/ubi7/ubi AS base

LABEL name="NGINX Ingress Controller" \
Expand Down Expand Up @@ -96,7 +96,7 @@ COPY --chown=nginx:0 build/log-default.json /etc/nginx

EXPOSE 80 443

COPY internal/configs/version1/nginx-plus.ingress.tmpl \
COPY --chown=nginx:0 internal/configs/version1/nginx-plus.ingress.tmpl \
internal/configs/version1/nginx-plus.tmpl \
internal/configs/version2/nginx-plus.virtualserver.tmpl \
internal/configs/version2/nginx-plus.transportserver.tmpl /
Expand All @@ -123,14 +123,24 @@ COPY nginx-ingress /


FROM golang:1.16-alpine AS builder
ARG VERSION
ARG IC_VERSION
ARG GIT_COMMIT
ARG DATE
ARG TARGETARCH

WORKDIR /go/src/github.com/nginxinc/kubernetes-ingress/nginx-ingress/cmd/nginx-ingress
COPY go.mod go.sum /go/src/github.com/nginxinc/kubernetes-ingress/nginx-ingress/
RUN go mod download
COPY . /go/src/github.com/nginxinc/kubernetes-ingress/nginx-ingress/
RUN CGO_ENABLED=0 GO111MODULE=on go build -ldflags "-s -w -X main.version=${VERSION} -X main.gitCommit=${GIT_COMMIT}" -o /tmp/nginx-ingress
RUN CGO_ENABLED=0 GO111MODULE=on GOOS=linux GOARCH=$TARGETARCH go build -trimpath -ldflags "-s -w -X main.version=${IC_VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o /tmp/nginx-ingress


FROM base AS container
COPY --from=builder /tmp/nginx-ingress /
COPY --chown=nginx:0 --from=builder /tmp/nginx-ingress /


FROM base AS goreleaser
ARG TARGETARCH
ARG TARGETVARIANT

COPY --chown=nginx:0 dist/kubernetes-ingress_linux_$TARGETARCH${TARGETVARIANT:+_7}/nginx-ingress /
Loading

0 comments on commit c0b36a5

Please sign in to comment.