Use goreleaser for build process (#212) (#216)

* Revamp build process (#212)

* implement native multi-arch builds

* Create ci.Dockerfile

* Create .goreleaser.yml

* Create goreleaser.Dockerfile

---------

Co-authored-by: mgdigital <mgdigital@users.noreply.github.com>

* Update goreleaser action

* Add prefix

* Prettier

---------

Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com>
This commit is contained in:
mgdigital
2024-04-30 17:48:33 +01:00
committed by GitHub
parent 5c01817f8b
commit 6e49b87bcc
4 changed files with 294 additions and 61 deletions
+154 -61
View File
@@ -2,103 +2,196 @@ name: Deploy Image to GHCR
on:
push:
branches:
- "main"
tags:
- "v*.*.*"
# pull_request:
env:
REGISTRY_IMAGE: ghcr.io/bitmagnet-io/bitmagnet
REGISTRY: ghcr.io
REGISTRY_IMAGE: ghcr.io/${{ github.repository }}
permissions:
contents: write
packages: write
jobs:
build:
name: Build distribution binaries
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
- name: linux/amd64
key: amd64
- name: linux/arm/v7
key: armv7
- name: linux/arm64
key: arm64
steps:
- name: "Checkout GitHub Action"
uses: actions/checkout@main
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
- name: Checkout
uses: actions/checkout@v4
with:
images: ${{ env.REGISTRY_IMAGE }}
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: "Login to GitHub Container Registry"
uses: docker/login-action@v3
- name: Set up Go
uses: actions/setup-go@v5
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
go-version: "1.22.0"
cache: true
- name: Build and push by digest
id: build
uses: docker/build-push-action@v5
- name: Run GoReleaser build
if: github.event_name == 'pull_request'
uses: goreleaser/goreleaser-action@v5
with:
context: .
platforms: ${{ matrix.platform.name }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
distribution: goreleaser
version: latest
args: release --clean --skip=validate --skip=publish --parallelism 5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Run GoReleaser build and publish tags
if: startsWith(github.ref, 'refs/tags/')
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload digest
- name: Upload assets
uses: actions/upload-artifact@v4
with:
name: digests-${{ matrix.platform.key }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
name: assets
path: |
dist/*.tar.gz
dist/*.zip
dist/*.apk
dist/*.deb
dist/*.rpm
merge:
docker:
name: Build Docker images
runs-on: ubuntu-latest
needs:
- build
strategy:
fail-fast: true
matrix:
platform:
- linux/386
- linux/amd64
- linux/amd64/v2
- linux/amd64/v3
- linux/arm/v6
- linux/arm/v7
- linux/arm64
# - linux/mips64le
# - linux/mips64
- linux/ppc64le
# - linux/riscv64
- linux/s390x
steps:
- name: Download digests
uses: actions/download-artifact@v4
- name: Checkout
uses: actions/checkout@v4
with:
pattern: digests-*
merge-multiple: true
path: /tmp/digests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
fetch-depth: 0
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{version}},prefix=v
type=semver,pattern={{major}}.{{minor}},prefix=v
type=ref,event=branch
type=ref,event=pr
flavor: |
latest=auto
- name: "Login to GitHub Container Registry"
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Supported Architectures
run: docker buildx ls
- name: Build and publish image
id: docker_build
uses: docker/build-push-action@v5
with:
context: .
file: ./ci.Dockerfile
platforms: ${{ matrix.platform }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=${{ (github.event.pull_request.head.repo.full_name == github.repository || github.event_name != 'pull_request') && 'true' || 'false' }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
- name: Export image digest
id: digest-prep
run: |
mkdir -p /tmp/digests
digest="${{ steps.docker_build.outputs.digest }}"
echo "manifest-hash=${digest#sha256:}" >> "$GITHUB_OUTPUT"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload image digest
uses: actions/upload-artifact@v4
with:
name: docker-digests-${{ steps.digest-prep.outputs.manifest-hash }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
docker-merge:
name: Publish Docker multi-arch manifest
if: ${{ github.event.pull_request.head.repo.full_name == github.repository || github.event_name != 'pull_request' }}
runs-on: ubuntu-latest
needs: [docker]
steps:
- name: Download image digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: docker-digests-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
tags: |
type=semver,pattern={{version}},prefix=v
type=semver,pattern={{major}}.{{minor}},prefix=v
type=ref,event=branch
type=ref,event=pr
flavor: |
latest=auto
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
+93
View File
@@ -0,0 +1,93 @@
before:
hooks:
- go mod tidy
builds:
- id: bitmagnet
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
- freebsd
goarch:
- amd64
- arm
- arm64
goarm:
- 6
ignore:
- goos: windows
goarch: arm
- goos: windows
goarch: arm64
- goos: darwin
goarch: arm
- goos: freebsd
goarch: arm
- goos: freebsd
goarch: arm64
main: ./main.go
binary: bitmagnet
archives:
- id: bitmagnet
builds:
- bitmagnet
format_overrides:
- goos: windows
format: zip
name_template: >-
{{ .ProjectName }}_
{{- .Version }}_
{{- .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else }}{{ .Arch }}{{ end }}
release:
prerelease: auto
footer: |
**Full Changelog**: https://github.com/bitmagnet-io/bitmagnet/compare/{{ .PreviousTag }}...{{ .Tag }}
## Docker images
- `docker pull ghcr.io/bitmagnet-io/bitmagnet:{{ .Tag }}`
## What to do next?
- Read the [documentation](https://bitmagnet.io)
- Join our [Discord server](https://discord.gg/6mFNszX8qM)
checksum:
name_template: "{{ .ProjectName }}_{{ .Version }}_checksums.txt"
changelog:
sort: asc
use: github
filters:
exclude:
- Merge pull request
- Merge remote-tracking branch
- Merge branch
groups:
- title: "New Features"
regexp: "^.*feat[(\\w)]*:+.*$"
order: 0
- title: "Bug fixes"
regexp: "^.*fix[(\\w)]*:+.*$"
order: 10
- title: Other work
order: 999
nfpms:
- package_name: bitmagnet
homepage: https://bitmagnet.io
maintainer: bitmagnet <packages@bitmagnet.io>
description: |-
bitmagnet is a self-hosted BitTorrent indexer.
formats:
- apk
- deb
- rpm
- archlinux
+40
View File
@@ -0,0 +1,40 @@
# build app
FROM --platform=$BUILDPLATFORM golang:1.22-alpine3.19 AS app-builder
RUN apk add --no-cache git tzdata
ENV SERVICE=bitmagnet
WORKDIR /src
# Cache Go modules
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
ARG VERSION=dev
ARG REVISION=dev
ARG BUILDTIME
ARG TARGETOS TARGETARCH TARGETVARIANT
RUN --network=none --mount=target=. \
export GOOS=$TARGETOS; \
export GOARCH=$TARGETARCH; \
[[ "$GOARCH" == "amd64" ]] && export GOAMD64=$TARGETVARIANT; \
[[ "$GOARCH" == "arm" ]] && [[ "$TARGETVARIANT" == "v6" ]] && export GOARM=6; \
[[ "$GOARCH" == "arm" ]] && [[ "$TARGETVARIANT" == "v7" ]] && export GOARM=7; \
echo $GOARCH $GOOS $GOARM$GOAMD64; \
go build -ldflags "-s -w -X github.com/bitmagnet-io/bitmagnet/internal/version.GitTag=${VERSION}" -o /build/bitmagnet main.go
# build runner
FROM alpine:latest AS runner
LABEL org.opencontainers.image.source = "https://github.com/bitmagnet-io/bitmagnet"
LABEL org.opencontainers.image.licenses = "MIT"
LABEL org.opencontainers.image.base.name = "alpine:latest"
RUN apk --no-cache add ca-certificates curl tzdata jq iproute2-ss
COPY --link --from=app-builder /build/bitmagnet* /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/bitmagnet"]
+7
View File
@@ -0,0 +1,7 @@
FROM alpine:latest
LABEL org.opencontainers.image.source = "https://github.com/bitmagnet-io/bitmagnet"
RUN ["apk", "--no-cache", "add", "ca-certificates","curl","iproute2-ss"]
COPY bitmagnet /usr/local/bin/bitmagnet
ENTRYPOINT ["/usr/local/bin/bitmagnet"]