Skip to content

Commit

Permalink
Build with CI matrix for platforms. (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Aug 17, 2023
1 parent 8a871c1 commit ffaad31
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 116 deletions.
169 changes: 98 additions & 71 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,78 +17,105 @@ env:
GCS_BUCKET: images.metal-pod.io

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'
cache: false

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
args: --build-tags integration -p bugs -p unused --timeout=5m

- name: Test
run: |
make test lint-structs
build:
name: Docker Build
runs-on: ubuntu-latest

strategy:
matrix:
os:
- linux
- windows
- darwin
arch:
- amd64
- arm64
exclude:
- os: linux
arch: arm64
- os: windows
arch: arm64

steps:
- name: Log in to the container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_REGISTRY_USER }}
password: ${{ secrets.DOCKER_REGISTRY_TOKEN }}

- name: Checkout
uses: actions/checkout@v3

- uses: google-github-actions/auth@v1
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'

- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1

- name: Ensure Go Version
uses: actions/setup-go@v3
with:
go-version: '1.21'
# go-version-file: 'go.mod'

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
args: --build-tags integration -p bugs -p unused --timeout=3m

- name: Lint Structs
uses: golangci/golangci-lint-action@v3
with:
args: --enable exhaustruct ./cmd --tests=false --timeout=3m

- name: Make tag
run: |
[ "${GITHUB_EVENT_NAME}" == 'pull_request' ] && echo "TAG_NAME=$(echo $GITHUB_REF | awk -F / '{print $3}')-${GITHUB_HEAD_REF##*/}" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'release' ] && echo "TAG_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'push' ] && echo "TAG_NAME=latest" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'pull_request' ] && echo "TARGET_BINARY_LOCATION=pull-requests/$(echo $GITHUB_REF | awk -F / '{print $3}')-${GITHUB_HEAD_REF##*/}" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'release' ] && echo "TARGET_BINARY_LOCATION=${GITHUB_REF##*/}" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'push' ] && echo "TARGET_BINARY_LOCATION=latest" >> $GITHUB_ENV || true
- name: Build platform binaries
run: |
make extract-binaries
- name: Build and push image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAME }}

- name: Prepare upload
run: |
mkdir -p metalctl/${TARGET_BINARY_LOCATION}
sudo chown -R $(id -u):$(id -g) result
cp result/* metalctl/${TARGET_BINARY_LOCATION}/
- name: Upload image tarballs to GCS
run: gsutil -m cp -r -p metalctl gs://$GCS_BUCKET

- name: Upload Release Asset
uses: softprops/action-gh-release@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
files: |
result/metalctl-*
if: ${{ github.event_name == 'release' }}
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'

- uses: google-github-actions/auth@v1
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'

- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1

- name: Make tag
run: |
[ "${GITHUB_EVENT_NAME}" == 'pull_request' ] && echo "TAG_NAME=$(echo $GITHUB_REF | awk -F / '{print $3}')-${GITHUB_HEAD_REF##*/}" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'release' ] && echo "TAG_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'push' ] && echo "TAG_NAME=latest" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'pull_request' ] && echo "TARGET_BINARY_LOCATION=pull-requests/$(echo $GITHUB_REF | awk -F / '{print $3}')-${GITHUB_HEAD_REF##*/}" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'release' ] && echo "TARGET_BINARY_LOCATION=${GITHUB_REF##*/}" >> $GITHUB_ENV || true
[ "${GITHUB_EVENT_NAME}" == 'push' ] && echo "TARGET_BINARY_LOCATION=latest" >> $GITHUB_ENV || true
- name: Build
run: |
make build GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }}
- name: Log in to the container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_REGISTRY_USER }}
password: ${{ secrets.DOCKER_REGISTRY_TOKEN }}
if: ${{ matrix.os == 'linux' && matrix.arch == 'amd64' }}

- name: Build and push image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAME }}
if: ${{ matrix.os == 'linux' && matrix.arch == 'amd64' }}

- name: Prepare upload
run: |
mkdir -p metalctl/${TARGET_BINARY_LOCATION}
sudo chown -R $(id -u):$(id -g) bin
cp bin/* metalctl/${TARGET_BINARY_LOCATION}/
- name: Upload image tarballs to GCS
run: gsutil -m cp -r -p metalctl gs://$GCS_BUCKET

- name: Upload Release Asset
uses: softprops/action-gh-release@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
files: |
bin/metalctl-*
if: ${{ github.event_name == 'release' }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.18
LABEL maintainer="metal-stack authors <info@metal-stack.io>"
COPY result/metalctl-linux-amd64 /metalctl
COPY bin/metalctl-linux-amd64 /metalctl
ENTRYPOINT ["/metalctl"]
3 changes: 0 additions & 3 deletions Dockerfile.binaries

This file was deleted.

81 changes: 40 additions & 41 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
BINARY := metalctl
MAINMODULE := github.com/metal-stack/metalctl
# the builder is at https://github.com/metal-stack/builder
COMMONDIR := $(or ${COMMONDIR},../builder)

-include $(COMMONDIR)/Makefile.inc
GOOS := linux
GOARCH := amd64
CGO_ENABLED := 1
TAGS := -tags 'netgo'
BINARY := metalctl-$(GOOS)-$(GOARCH)

SHA := $(shell git rev-parse --short=8 HEAD)
GITVERSION := $(shell git describe --long --all)
BUILDDATE := $(shell date --rfc-3339=seconds)
VERSION := $(or ${VERSION},$(shell git describe --tags --exact-match 2> /dev/null || git symbolic-ref -q --short HEAD || git rev-parse --short HEAD))

ifeq ($(CGO_ENABLED),1)
ifeq ($(GOOS),linux)
LINKMODE := -linkmode external -extldflags '-static -s -w'
TAGS := -tags 'osusergo netgo static_build'
endif
endif

LINKMODE := $(LINKMODE) \
-X 'github.com/metal-stack/v.Version=$(VERSION)' \
-X 'github.com/metal-stack/v.Revision=$(GITVERSION)' \
-X 'github.com/metal-stack/v.GitSHA1=$(SHA)' \
-X 'github.com/metal-stack/v.BuildDate=$(BUILDDATE)'

.PHONY: all
all:: markdown lint-structs
all: build test lint-structs markdown

.PHONY: build
build:
go build \
$(TAGS) \
-ldflags \
"$(LINKMODE)" \
-o bin/$(BINARY) \
github.com/metal-stack/metalctl

release:: all
strip bin/$(BINARY) || true
md5sum bin/$(BINARY) > bin/$(BINARY).md5

.PHONY: test
test: build
go test -cover ./...

.PHONY: lint-structs
lint-structs:
Expand All @@ -21,36 +52,4 @@ lint-structs:
markdown:
rm -rf docs
mkdir -p docs
bin/metalctl markdown

.PHONY: build
build:
$(GO) build \
-tags netgo \
-ldflags \
"$(LINKMODE)" \
-o bin/$(BINARY) \
$(MAINMODULE)

.PHONY: lint
lint:
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint golangci-lint run -v

.PHONY: build-platforms
build-platforms:
docker build -f Dockerfile.binaries --no-cache -t platforms-binaries .

.PHONY: extract-binaries
extract-binaries: build-platforms
mkdir -p tmp
mkdir -p result
docker cp $(shell docker create platforms-binaries):/work/bin tmp
mv tmp/bin/metalctl-linux-amd64 result
mv tmp/bin/metalctl-windows-amd64 result
mv tmp/bin/metalctl-darwin-amd64 result
mv tmp/bin/metalctl-darwin-arm64 result
md5sum result/metalctl-linux-amd64 > result/metalctl-linux-amd64.md5
md5sum result/metalctl-windows-amd64 > result/metalctl-windows-amd64.md5
md5sum result/metalctl-darwin-amd64 > result/metalctl-darwin-amd64.md5
md5sum result/metalctl-darwin-arm64 > result/metalctl-darwin-arm64.md5
ls -lh result
bin/$(BINARY) markdown

0 comments on commit ffaad31

Please sign in to comment.