Skip to content

Commit

Permalink
Chore/build pipeline (#6)
Browse files Browse the repository at this point in the history
* chg: chore: add linter

* chg: chore: update go

* new: chore: add pipeline

* chg: fix: dockerfile path

* chg: fix: main branch name

* chg: fix: add goimports

* chg: fix: remove goimports

* chg: fix: use goreleaser

* new: chore: add goreleaser config

* new: docs: add LICENSE

* chg: fix: container version

* chg: fix: dockerfile setup
  • Loading branch information
felipemarinho97 authored Apr 28, 2024
1 parent 268ece5 commit 98f1700
Show file tree
Hide file tree
Showing 12 changed files with 984 additions and 14 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/pipeline.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#####################################################
### Copy platform specific binary
FROM bash as copy-binary
ARG TARGETPLATFORM

RUN echo "Target Platform = ${TARGETPLATFORM}"

COPY dist .
RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then cp torrentindexer_linux_amd64_linux_amd64_v1/torrent-indexer /torrent-indexer; fi
RUN if [ "$TARGETPLATFORM" = "linux/386" ]; then cp torrentindexer_linux_386_linux_386/torrent-indexer /torrent-indexer; fi
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then cp torrentindexer_linux_arm64_linux_arm64/torrent-indexer /torrent-indexer; fi
RUN if [ "$TARGETPLATFORM" = "linux/arm/v6" ]; then cp torrentindexer_linux_arm_linux_arm_6/torrent-indexer /torrent-indexer; fi
RUN if [ "$TARGETPLATFORM" = "linux/arm/v7" ]; then cp torrentindexer_linux_arm_linux_arm_7/torrent-indexer /torrent-indexer; fi
RUN chmod +x /torrent-indexer


#####################################################
### Build Final Image
FROM alpine as release
LABEL maintainer="[email protected]"

COPY --from=copy-binary /torrent-indexer /app/

WORKDIR /app

ENTRYPOINT ["/app/torrent-indexer"]
158 changes: 158 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
name: "Pipeline: Test, Lint, Build"
on:
push:
branches:
- main
tags:
- "v*"
pull_request:
branches:
- main

jobs:
go-lint:
name: Lint Go code
runs-on: ubuntu-latest
container: golang:1.22
steps:
- uses: actions/checkout@v4

- name: Config workspace folder as trusted
run: git config --global --add safe.directory $GITHUB_WORKSPACE; git describe --dirty --always --tags

- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: latest
github-token: ${{ secrets.GITHUB_TOKEN }}
args: --timeout 2m

- run: go mod tidy
- name: Verify no changes from go mod tidy
run: |
git status --porcelain
if [ -n "$(git status --porcelain)" ]; then
echo 'To fix this check, run "make format" and commit the changes'
exit 1
fi
go:
name: Test Go code
runs-on: ubuntu-latest
container: golang:1.22
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Config workspace folder as trusted
run: git config --global --add safe.directory $GITHUB_WORKSPACE; git describe --dirty --always --tags

- name: Download dependencies
if: steps.cache-go.outputs.cache-hit != 'true'
continue-on-error: ${{contains(matrix.go_version, 'beta') || contains(matrix.go_version, 'rc')}}
run: go mod download

- name: Test
continue-on-error: ${{contains(matrix.go_version, 'beta') || contains(matrix.go_version, 'rc')}}
run: go test -shuffle=on -race -cover ./... -v

binaries:
name: Build binaries
needs: [go, go-lint]
runs-on: ubuntu-latest
container: goreleaser/goreleaser:v1.25.1
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Config workspace folder as trusted
run: git config --global --add safe.directory $GITHUB_WORKSPACE; git describe --dirty --always --tags

- name: Run GoReleaser - SNAPSHOT
if: startsWith(github.ref, 'refs/tags/') != true
run: goreleaser release --clean --skip=publish --snapshot
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run GoReleaser - RELEASE
if: startsWith(github.ref, 'refs/tags/')
run: goreleaser release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- uses: actions/upload-artifact@v4
with:
name: binaries
path: |
dist
!dist/*.tar.gz
!dist/*.zip
retention-days: 7

docker:
name: Build and publish Docker images
needs: [binaries]
runs-on: ubuntu-latest
env:
DOCKER_IMAGE: ${{secrets.DOCKER_IMAGE}}
steps:
- name: Set up QEMU
id: qemu
uses: docker/setup-qemu-action@v3
if: env.DOCKER_IMAGE != ''

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
if: env.DOCKER_IMAGE != ''

- uses: actions/checkout@v4
if: env.DOCKER_IMAGE != ''

- uses: actions/download-artifact@v4
if: env.DOCKER_IMAGE != ''
with:
name: binaries
path: dist

- name: Login to Docker Hub
if: env.DOCKER_IMAGE != ''
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Login to GitHub Container Registry
if: env.DOCKER_IMAGE != ''
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Docker
if: env.DOCKER_IMAGE != ''
id: meta
uses: docker/metadata-action@v5
with:
labels: |
maintainer=felipemarinho97
images: |
name=${{secrets.DOCKER_IMAGE}}
name=ghcr.io/${{ github.repository }}
tags: |
type=ref,event=pr
type=semver,pattern={{version}}
type=raw,value=develop,enable={{is_default_branch}}
- name: Build and Push
if: env.DOCKER_IMAGE != ''
uses: docker/build-push-action@v5
with:
context: .
file: .github/workflows/pipeline.dockerfile
platforms: linux/amd64,linux/386,linux/arm/v6,linux/arm/v7,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
run:
skip-files:
- scrape.go
133 changes: 133 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# GoReleaser config
project_name: torrent-indexer

builds:
- id: torrentindexer_linux_amd64
env:
- CGO_ENABLED=1
goos:
- linux
goarch:
- amd64
flags:
- -tags=netgo
ldflags:
- "-extldflags '-static -lz'"
- -s -w -X github.com/felipemarinho97/torrent-indexer/consts.gitSha={{.ShortCommit}} -X github.com/felipemarinho97/torrent-indexer/consts.gitTag={{.Version}}

- id: torrentindexer_linux_386
env:
- CGO_ENABLED=1
- PKG_CONFIG_PATH=/i386/lib/pkgconfig
goos:
- linux
goarch:
- "386"
flags:
- -tags=netgo
ldflags:
- "-extldflags '-static'"
- -s -w -X github.com/felipemarinho97/torrent-indexer/consts.gitSha={{.ShortCommit}} -X github.com/felipemarinho97/torrent-indexer/consts.gitTag={{.Version}}

- id: torrentindexer_linux_arm
env:
- CGO_ENABLED=1
- CC=arm-linux-gnueabi-gcc
- CXX=arm-linux-gnueabi-g++
- PKG_CONFIG_PATH=/arm/lib/pkgconfig
goos:
- linux
goarch:
- arm
goarm:
- "5"
- "6"
- "7"
flags:
- -tags=netgo
ldflags:
- "-extldflags '-static'"
- -s -w -X github.com/felipemarinho97/torrent-indexer/consts.gitSha={{.ShortCommit}} -X github.com/felipemarinho97/torrent-indexer/consts.gitTag={{.Version}}

- id: torrentindexer_linux_arm64
env:
- CGO_ENABLED=1
- CC=aarch64-linux-gnu-gcc
- CXX=aarch64-linux-gnu-g++
- PKG_CONFIG_PATH=/arm64/lib/pkgconfig
goos:
- linux
goarch:
- arm64
flags:
- -tags=netgo
ldflags:
- "-extldflags '-static'"
- -s -w -X github.com/felipemarinho97/torrent-indexer/consts.gitSha={{.ShortCommit}} -X github.com/felipemarinho97/torrent-indexer/consts.gitTag={{.Version}}

- id: torrentindexer_windows_386
env:
- CGO_ENABLED=1
- CC=i686-w64-mingw32-gcc
- CXX=i686-w64-mingw32-g++
- PKG_CONFIG_PATH=/mingw32/lib/pkgconfig
goos:
- windows
goarch:
- "386"
flags:
- -tags=netgo
ldflags:
- "-extldflags '-static'"
- -s -w -X github.com/felipemarinho97/torrent-indexer/consts.gitSha={{.ShortCommit}} -X github.com/felipemarinho97/torrent-indexer/consts.gitTag={{.Version}}

- id: torrentindexer_windows_amd64
env:
- CGO_ENABLED=1
- CC=x86_64-w64-mingw32-gcc
- CXX=x86_64-w64-mingw32-g++
- PKG_CONFIG_PATH=/mingw64/lib/pkgconfig
goos:
- windows
goarch:
- amd64
flags:
- -tags=netgo
ldflags:
- "-extldflags '-static'"
- -s -w -X github.com/felipemarinho97/torrent-indexer/consts.gitSha={{.ShortCommit}} -X github.com/felipemarinho97/torrent-indexer/consts.gitTag={{.Version}}

- id: torrentindexer_darwin_amd64
env:
- CGO_ENABLED=1
- CC=o64-clang
- CXX=o64-clang++
- PKG_CONFIG_PATH=/darwin/lib/pkgconfig
goos:
- darwin
goarch:
- amd64
flags:
- -tags=netgo
ldflags:
- -s -w -X github.com/felipemarinho97/torrent-indexer/consts.gitSha={{.ShortCommit}} -X github.com/felipemarinho97/torrent-indexer/consts.gitTag={{.Version}}

archives:
- format_overrides:
- goos: windows
format: zip

checksum:
name_template: "{{ .ProjectName }}_checksums.txt"

snapshot:
name_template: "{{ .Tag }}-SNAPSHOT"

release:
draft: true

changelog:
# sort: asc
filters:
exclude:
- "^docs:"
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 as builder
FROM golang:1.22 as builder

WORKDIR /go/src/app
COPY . .
Expand All @@ -8,6 +8,7 @@ RUN go install -v ./...
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest
LABEL maintainer="[email protected]"

RUN apk --no-cache add ca-certificates

Expand Down
Loading

0 comments on commit 98f1700

Please sign in to comment.