Skip to content

Commit

Permalink
Migration to Github Actions and multiarch builds
Browse files Browse the repository at this point in the history
**What**
- Create makefile for locally building and testing
- Create github workflows
- Use buildx to create multiarch builds
- Remove travisci

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler authored and alexellis committed Feb 12, 2021
1 parent c72f1f9 commit c00d590
Show file tree
Hide file tree
Showing 18 changed files with 368 additions and 327 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name:

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'

jobs:
build:
strategy:
matrix:
go-version: [ 1.13.x ]
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@master
with:
fetch-depth: 1
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Make all
run: make test
- name: Docker meta
id: meta
uses: crazy-max/ghaction-docker-meta@v1
with:
images: "ghcr.io/${{ github.repository }}"
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Test and Build multiarch
id: docker_build
uses: docker/build-push-action@v2
with:
context: ./
file: ./Dockerfile
outputs: "type=image,push=false"
platforms: linux/amd64,linux/arm64,linux/arm/v7
# throw away tag, since we don't push the image
tags: openfaas/multiarch:test
75 changes: 75 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: publish

on:
push:
tags:
- '*'

jobs:
publish:
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@master
with:
fetch-depth: 1
-
name: Docker meta
id: meta
uses: crazy-max/ghaction-docker-meta@v1
with:
images: "ghcr.io/${{ github.repository }}"
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Docker Login
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.DOCKER_PASSWORD }}
-
name: Build binaries
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile.redist
platforms: linux/amd64
build-args: |
VERSION=${{ steps.meta.outputs.version }}
GIT_COMMIT=${{ github.sha }}
load: true
push: false
# tag doesn't matter because we will never push this
tags: redist/throwaway:latest
-
name: Copy binaries to host
run: ./ci/copy_redist.sh redist/throwaway
-
name: Create SHA of binaries
run: ./ci/hashgen.sh
-
name: Docker Build and Push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm/v7,linux/arm64
target: release
build-args: |
VERSION=${{ steps.meta.outputs.version }}
GIT_COMMIT=${{ github.sha }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
-
name: Upload binaries and their SHA to Github Release
uses: alexellis/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
asset_paths: '["./bin/*"]'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ build
template
**/*.sha256

bin
59 changes: 0 additions & 59 deletions .travis.yml

This file was deleted.

47 changes: 35 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
ARG goversion=1.15

FROM teamserverless/license-check:0.3.9 as license-check

FROM golang:1.15 as build
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:$goversion as builder

ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

ARG GIT_COMMIT="000000"
ARG VERSION="dev"

COPY --from=license-check /license-check /usr/bin/

ARG CGO_ENABLED=0
ARG GO111MODULE="on"
ENV GOFLAGS=-mod=vendor
ARG GOPROXY=""

WORKDIR /go/src/github.com/openfaas/of-watchdog
WORKDIR /app
COPY vendor vendor
COPY config config
COPY executor executor
COPY metrics metrics
COPY version.go .
COPY main.go .
COPY go.mod .
COPY go.sum .

RUN license-check -path /go/src/github.com/openfaas/of-watchdog --verbose=false "Alex Ellis" "OpenFaaS Author(s)"
RUN license-check -path /app --verbose=false "Alex Ellis" "OpenFaaS Author(s)"
RUN gofmt -l -d $(find . -type f -name '*.go' -not -path "./vendor/*")
RUN go test -v ./...

RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 \
go build --ldflags "-s -w \
-X github.com/openfaas/of-watchdog/main.GitCommit=${GIT_COMMIT} \
-X github.com/openfaas/of-watchdog/main.Version=${VERSION}" \
-a -installsuffix cgo -o fwatchdog


FROM scratch as release

# Run a gofmt and exclude all vendored code.
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))"
LABEL org.label-schema.license="MIT" \
org.label-schema.vcs-url="https://github.com/openfaas/of-watchdog" \
org.label-schema.vcs-type="Git" \
org.label-schema.name="openfaas/of-watchdog" \
org.label-schema.vendor="openfaas" \
org.label-schema.docker.schema-version="1.0"

RUN go test -mod=vendor -v ./...
COPY --from=builder /app/fwatchdog /fwatchdog

# Stripping via -ldflags "-s -w"
RUN CGO_ENABLED=0 GOOS=linux go build -mod=vendor -a -ldflags "-s -w" -installsuffix cgo -o of-watchdog . \
&& CGO_ENABLED=0 GOOS=darwin go build -mod=vendor -a -ldflags "-s -w" -installsuffix cgo -o of-watchdog-darwin . \
&& GOARM=6 GOARCH=arm CGO_ENABLED=0 GOOS=linux go build -mod=vendor -a -ldflags "-s -w" -installsuffix cgo -o of-watchdog-armhf . \
&& GOARCH=arm64 CGO_ENABLED=0 GOOS=linux go build -mod=vendor -a -ldflags "-s -w" -installsuffix cgo -o of-watchdog-arm64 . \
&& GOOS=windows CGO_ENABLED=0 go build -mod=vendor -a -ldflags "-s -w" -installsuffix cgo -o of-watchdog.exe .
ENTRYPOINT ["/fwatchdog"]
6 changes: 0 additions & 6 deletions Dockerfile.packager

This file was deleted.

81 changes: 81 additions & 0 deletions Dockerfile.redist
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
ARG goversion=1.14

FROM teamserverless/license-check:0.3.9 as license-check

FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:$goversion as builder

ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

ARG GIT_COMMIT="000000"
ARG VERSION="dev"

COPY --from=license-check /license-check /usr/bin/

ARG CGO_ENABLED=0
ARG GO111MODULE="on"
ENV GOFLAGS=-mod=vendor
ARG GOPROXY=""

WORKDIR /app
COPY vendor vendor
COPY config config
COPY executor executor
COPY metrics metrics
COPY version.go .
COPY main.go .
COPY go.mod .
COPY go.sum .


# use multiple stages here because buildkit will run these in parallel,
# if possible
FROM builder as linux
RUN GOOS=linux CGO_ENABLED=0 \
go build --ldflags "-s -w \
-X github.com/openfaas/of-watchdog/main.GitCommit=${GIT_COMMIT} \
-X github.com/openfaas/of-watchdog/main.Version=${VERSION}" \
-a -installsuffix cgo -o fwatchdog

FROM builder as darwin
RUN GOOS=darwin CGO_ENABLED=0 \
go build --ldflags "-s -w \
-X github.com/openfaas/of-watchdog/main.GitCommit=${GIT_COMMIT} \
-X github.com/openfaas/of-watchdog/main.Version=${VERSION}" \
-a -installsuffix cgo -o fwatchdog-darwin

FROM builder as windows
RUN GOOS=windows CGO_ENABLED=0 \
go build --ldflags "-s -w \
-X github.com/openfaas/of-watchdog/main.GitCommit=${GIT_COMMIT} \
-X github.com/openfaas/of-watchdog/main.Version=${VERSION}" \
-a -installsuffix cgo -o fwatchdog.exe

FROM builder as arm
RUN GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=0 \
go build --ldflags "-s -w \
-X github.com/openfaas/of-watchdog/main.GitCommit=${GIT_COMMIT} \
-X github.com/openfaas/of-watchdog/main.Version=${VERSION}" \
-a -installsuffix cgo -o fwatchdog-armhf

FROM builder as arm64
RUN GOOS=linux GOARCH=arm64 CGO_ENABLED=0 \
go build --ldflags "-s -w \
-X github.com/openfaas/of-watchdog/main.GitCommit=${GIT_COMMIT} \
-X github.com/openfaas/of-watchdog/main.Version=${VERSION}" \
-a -installsuffix cgo -o fwatchdog-arm64

# Release stage
FROM scratch

WORKDIR /bin

COPY --from=linux /app/fwatchdog /bin
COPY --from=darwin /app/fwatchdog-darwin /bin
COPY --from=arm /app/fwatchdog-armhf /bin
COPY --from=windows /app/fwatchdog.exe /bin
COPY --from=arm64 /app/fwatchdog-arm64 /bin

CMD ["/bin/fwatchdog"]
48 changes: 48 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

.GIT_COMMIT=$(shell git rev-parse HEAD)
.GIT_VERSION=$(shell git describe --tags --always --dirty 2>/dev/null)
.GIT_UNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no)
ifneq ($(.GIT_UNTRACKEDCHANGES),)
.GIT_VERSION := $(.GIT_VERSION)-$(shell date +"%s")
endif


.IMAGE=ghcr.io/openfaas/of-watchdog
TAG?=latest

export GOFLAGS=-mod=vendor

.PHONY: test
test: fmt
@echo "+ $@"
@go test -v ./...

.PHONY: fmt
fmt:
@echo "+ $@"
@gofmt -l -d $(shell find . -type f -name '*.go' -not -path "./vendor/*")


.PHONY: build
build:
@echo "+ $@"
@docker build \
--build-arg GIT_COMMIT=${.GIT_COMMIT} \
--build-arg VERSION=${.GIT_VERSION} \
-t ${.IMAGE}:${TAG} .

.PHONY: redist
redist:
@echo "+ $@"
@docker build \
--build-arg GIT_COMMIT=${.GIT_COMMIT} \
--build-arg VERSION=${.GIT_VERSION} \
-f Dockerfile.redist \
-t ${.IMAGE}:${TAG} .
@./ci/copy_redist.sh ${.IMAGE} ${TAG}
@./ci/hashgen.sh

# use this with
# `./ci/copy_redist.sh $(make print-image) && ./ci/hashgen.sh`
print-image:
@echo ${.IMAGE}
Loading

0 comments on commit c00d590

Please sign in to comment.