ci: docker buildx (#8)

* fix: goreleaser main path

* ignore dist folder

* ci: docker buildx support
This commit is contained in:
Felipe Martin Garcia 2022-08-10 16:39:32 +02:00 committed by GitHub
parent 5543a19df1
commit 99845f1ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 123 additions and 44 deletions

View File

@ -22,5 +22,9 @@ jobs:
with:
go-version: ${{ matrix.go_version }}
- name: Build
run: make build
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: build --single-target --snapshot

34
.github/workflows/_docker-buildx.yml vendored Normal file
View File

@ -0,0 +1,34 @@
name: "Build Docker"
on:
workflow_call:
workflow_dispatch:
jobs:
buildx:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
name: Build Docker
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/download-artifact@v3
with:
name: dist
path: dist
- name: Buildx
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login -u "${{ github.repository_owner }}" --password-stdin ghcr.io
REPOSITORY=ghcr.io/${{ github.repository }}
TAG=latest
if [ -z "$(git tag --points-at HEAD)" ]
then
TAG="dev"
fi
CONTAINER_BUILDX_OPTIONS="--push --output=type=registry --tag $REPOSITORY:$(git describe --tags) --tag $REPOSITORY:$TAG" make buildx

View File

@ -1,9 +1,7 @@
name: goreleaser
on:
push:
tags:
- "v*" # Versioning tags
workflow_call:
permissions:
contents: write # Required to upload dist files
@ -29,3 +27,8 @@ jobs:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/upload-artifact@v3
with:
name: dist
path: ./dist/*

View File

@ -6,7 +6,7 @@ on:
- latest
concurrency:
group: ci-tests-${{ github.ref }}-1
group: ci-pull-request-${{ github.ref }}
cancel-in-progress: true
jobs:

20
.github/workflows/push_version.yml vendored Normal file
View File

@ -0,0 +1,20 @@
name: goreleaser
on:
push:
branches: [latest]
tags: ["v*"]
permissions:
contents: write # Required to upload dist files
concurrency:
group: ci-push-version-${{ github.ref }}
cancel-in-progress: true
jobs:
goreleaser:
uses: ./.github/workflows/_goreleaser.yml
docker-buildx:
needs: goreleaser
uses: ./.github/workflows/_docker-buildx.yml

1
.gitignore vendored
View File

@ -16,6 +16,7 @@ coverage.*
# CI
build/
dist/
# Editors
*~

View File

@ -8,12 +8,24 @@ builds:
- linux
- windows
- darwin
main: ./cmd/server
goarch:
- amd64
- arm64
- arm
goarm:
- 6
- 7
ignore:
- goos: windows
goarch: arm
- goos: windows
goarch: arm64
main: ./cmd/bazaar
ldflags:
- -s -w
archives:
- replacements:
darwin: macos
linux: linux
windows: windows
386: i386
amd64: x86_64
source:

View File

@ -1,12 +1,12 @@
# Build stage
ARG GOLANG_VERSION
ARG ALPINE_VERSION
FROM docker.io/library/golang:${GOLANG_VERSION}-alpine${ALPINE_VERSION} AS builder
WORKDIR /src
COPY . .
RUN apk add --no-cache ca-certificates tzdata make && \
make build
FROM docker.io/library/alpine:${ALPINE_VERSION} AS builder
ARG TARGETARCH
ARG TARGETOS
ARG TARGETVARIANT
COPY dist/bazaar_${TARGETOS}_${TARGETARCH}${TARGETVARIANT}/bazaar /usr/bin/bazaar
RUN apk add --no-cache ca-certificates tzdata make
# Server image
FROM scratch
@ -15,8 +15,9 @@ ENV PORT 8080
LABEL org.opencontainers.image.source="https://github.com/fmartingr/bazaar"
LABEL maintainer="Felipe Martin <github@fmartingr.com>"
COPY --from=builder /src/build/bazaar /usr/bin/
COPY --from=builder /usr/bin/bazaar /usr/bin/bazaar
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ENTRYPOINT ["/usr/bin/bazaar"]

View File

@ -1,8 +1,5 @@
PROJECT_NAME := bazaar
GOLANG_VERSION=1.19
VERSION_COMMIT := $git rev-parse --short HEAD)
SOURCE_FILES ?=./internal/... ./cmd/... ./pkg/...
TEST_OPTIONS ?= -v -failfast -race -bench=. -benchtime=100000x -cover -coverprofile=coverage.out
@ -10,24 +7,20 @@ TEST_TIMEOUT ?=1m
CLEAN_OPTIONS ?=-modcache -testcache
LD_FLAGS := -X main.version=$(VERSION) -s -w
CGO_ENABLED := 0
BUILDS_PATH := ./build
BUILDS_PATH := ./dist
FROM_MAKEFILE := y
CONTAINER_RUNTIME := podman
CONTAINERFILE_NAME := Containerfile
CONTAINER_GOLANG_VERSION := ${GOLANG_VERSION}
CONTAINER_ALPINE_VERSION := 3.16
CONTAINER_IMAGE_NAME := fmartingr/${PROJECT_NAME}
CONTAINER_IMAGE_TAG := dev
BUILDX_PLATFORMS := linux/amd64,arm64,linux/arm/v7
# Common exports
export FROM_MAKEFILE
export VERSION_COMMIT
export LD_FLAGS
export CGO_ENABLED
export SOURCE_FILES
@ -37,10 +30,9 @@ export BUILDS_PATH
export CONTAINER_RUNTIME
export CONTAINERFILE_NAME
export CONTAINER_GOLANG_VERSION
export CONTAINER_ALPINE_VERSION
export CONTAINER_IMAGE_NAME
export CONTAINER_IMAGE_TAG
export BUILDX_PLATFORMS
.PHONY: all
all: help
@ -67,13 +59,12 @@ clean: ### clean test cache, build files
.PHONY: build
build: clean ### builds the project for the setup os/arch combinations
$(info: Make: Build)
@go build -a -v -ldflags "${LD_FLAGS}" -o ${BUILDS_PATH}/bazaar ./cmd/bazaar/*.go
@chmod +x ${BUILDS_PATH}/bazaar
@goreleaser build --rm-dist --snapshot
.PHONY: build-container-image
build-container:
$(info: Make: Container image)
@bash scripts/build-container-image.sh
.PHONY: buildx
buildx:
$(info: Make: Buildx)
@bash scripts/buildx.sh
.PHONY: quick-run
quick-run: ### Executes the project using golang

View File

@ -1,9 +0,0 @@
#!/usr/bin/env bash
set -e
if [ -z "$FROM_MAKEFILE" ]; then
echo "Do not call this file directly - use the make command"
exit 1
fi
${CONTAINER_RUNTIME} build --build-arg "GOLANG_VERSION=${CONTAINER_GOLANG_VERSION}" --build-arg="ALPINE_VERSION=${CONTAINER_ALPINE_VERSION}" -t ${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG} -f ${CONTAINERFILE_NAME} .

22
scripts/buildx.sh Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -e
if [ -z "$FROM_MAKEFILE" ]; then
echo "Do not call this file directly - use the make command"
exit 1
fi
CONTAINER_RUNTIME=docker # Forcing docker
if [ "$CONTAINER_RUNTIME" == "docker" ]; then
$CONTAINER_RUNTIME buildx create --use --name bazaar_builder
fi
cp -r dist/bazaar_linux_arm_7 dist/bazaar_linux_armv7
cp -r dist/bazaar_linux_amd64_v1 dist/bazaar_linux_amd64
$CONTAINER_RUNTIME buildx build -f ${CONTAINERFILE_NAME} --platform=${BUILDX_PLATFORMS} --build-arg "ALPINE_VERSION=${CONTAINER_ALPINE_VERSION}" ${CONTAINER_BUILDX_OPTIONS} .
if [ "$CONTAINER_RUNTIME" == "docker" ]; then
$CONTAINER_RUNTIME buildx rm bazaar_builder
fi