Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use goreleaser for build process (#212) #216

Merged
merged 6 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 133 additions & 40 deletions .github/workflows/ghcr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.22.0"
cache: true

- name: Run GoReleaser build
if: github.event_name == 'pull_request'
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean --skip=validate --skip=publish --parallelism 5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- 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 assets
uses: actions/upload-artifact@v4
with:
name: assets
path: |
dist/*.tar.gz
dist/*.zip
dist/*.apk
dist/*.deb
dist/*.rpm

docker:
name: Build Docker images
runs-on: ubuntu-latest
strategy:
fail-fast: false
fail-fast: true
matrix:
platform:
- name: linux/amd64
key: amd64
- name: linux/arm/v7
key: armv7
- name: linux/arm64
key: arm64
- 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: "Checkout GitHub Action"
uses: actions/checkout@main
- name: Checkout
uses: actions/checkout@v4
with:
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: Docker meta
- 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: 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
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Supported Architectures
run: docker buildx ls

- name: Build and push by digest
id: build
- name: Build and publish image
id: docker_build
uses: docker/build-push-action@v5
with:
context: .
platforms: ${{ matrix.platform.name }}
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 }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
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 digest
- name: Export image digest
id: digest-prep
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
digest="${{ steps.docker_build.outputs.digest }}"
echo "manifest-hash=${digest#sha256:}" >> "$GITHUB_OUTPUT"
touch "/tmp/digests/${digest#sha256:}"

- name: Upload digest
- name: Upload image digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ matrix.platform.key }}
name: docker-digests-${{ steps.digest-prep.outputs.manifest-hash }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

merge:
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:
- build
needs: [docker]
steps:
- name: Download digests
- name: Download image digests
uses: actions/download-artifact@v4
with:
pattern: digests-*
merge-multiple: true
path: /tmp/digests
pattern: docker-digests-*
merge-multiple: true

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta

- 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"
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- 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 changes: 93 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
description: |-
bitmagnet is a self-hosted BitTorrent indexer.
formats:
- apk
- deb
- rpm
- archlinux
40 changes: 40 additions & 0 deletions ci.Dockerfile
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions goreleaser.Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
Loading