diff --git a/.github/workflows/_build.yml b/.github/workflows/_build.yml index b07ebac..62a2c53 100644 --- a/.github/workflows/_build.yml +++ b/.github/workflows/_build.yml @@ -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 diff --git a/.github/workflows/_docker-buildx.yml b/.github/workflows/_docker-buildx.yml new file mode 100644 index 0000000..f4b7f3e --- /dev/null +++ b/.github/workflows/_docker-buildx.yml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/_goreleaser.yml similarity index 82% rename from .github/workflows/release.yml rename to .github/workflows/_goreleaser.yml index bf56245..9d40b79 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/_goreleaser.yml @@ -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/* diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 307794f..2f9f715 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -6,7 +6,7 @@ on: - latest concurrency: - group: ci-tests-${{ github.ref }}-1 + group: ci-pull-request-${{ github.ref }} cancel-in-progress: true jobs: diff --git a/.github/workflows/push_version.yml b/.github/workflows/push_version.yml new file mode 100644 index 0000000..7af0b7b --- /dev/null +++ b/.github/workflows/push_version.yml @@ -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 diff --git a/.gitignore b/.gitignore index f70e817..bc61eda 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ coverage.* # CI build/ +dist/ # Editors *~ diff --git a/.goreleaser.yml b/.goreleaser.yml index 3b47548..0d8992e 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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: diff --git a/Containerfile b/Containerfile index 90e402d..9d51d2a 100644 --- a/Containerfile +++ b/Containerfile @@ -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 " -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"] diff --git a/Makefile b/Makefile index 92e752f..4451763 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/scripts/build-container-image.sh b/scripts/build-container-image.sh deleted file mode 100644 index 23421d3..0000000 --- a/scripts/build-container-image.sh +++ /dev/null @@ -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} . diff --git a/scripts/buildx.sh b/scripts/buildx.sh new file mode 100644 index 0000000..3ac5d2f --- /dev/null +++ b/scripts/buildx.sh @@ -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