Skip to content

Commit

Permalink
use goreleaser (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jandelgado authored Oct 28, 2021
1 parent 37a8870 commit 7aa7d46
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 95 deletions.
71 changes: 35 additions & 36 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,51 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
- name: Checkout code
uses: actions/checkout@v2
- name: Run linters
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.17.x
- name: Checkout code
uses: actions/checkout@v2
- name: Run linters
uses: golangci/golangci-lint-action@v2
with:
version: v1.29

test:
strategy:
matrix:
go-version: [1.15.x]
go-version: [1.17.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
run: go test -v -covermode=count
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
run: go test -v -covermode=count

coverage:
runs-on: ubuntu-latest
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: 1.15.x
- name: Checkout code
uses: actions/checkout@v2
- name: Calc coverage
run: |
go test -v -covermode=count -coverprofile=coverage.out
- name: Convert coverage.out to coverage.lcov
uses: jandelgado/[email protected]
- name: Coveralls
uses: coverallsapp/[email protected]
with:
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: 1.17.x
- name: Checkout code
uses: actions/checkout@v2
- name: Calc coverage
run: |
go test -v -covermode=count -coverprofile=coverage.out
- name: Convert coverage.out to coverage.lcov
uses: jandelgado/[email protected]
- name: Coveralls
uses: coverallsapp/[email protected]
with:
github-token: ${{ secrets.github_token }}
path-to-lcov: coverage.lcov

82 changes: 36 additions & 46 deletions .github/workflows/upload_assets.yml
Original file line number Diff line number Diff line change
@@ -1,65 +1,55 @@
on:
release:
types: [created]
push:
tags:
- 'v*'

name: Upload release assets after release is created
name: Upload release assets after tagging
jobs:
build:
name: build binaries
name: create assets
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
go-version: 1.17.x
- name: Checkout code
uses: actions/checkout@v2
- name: build
run: |
echo "GO111MODULE=on" >> $GITHUB_ENV
GOOS=windows GOARCH=amd64 go build -o bin/ci-test-windows-amd64.exe
GOOS=linux GOARCH=amd64 go build -o bin/ci-test-linux-amd64
GOOS=darwin GOARCH=amd64 go build -o bin/ci-test-macos-amd64
- name: upload artifacts
uses: actions/upload-artifact@master
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
name: binaries
path: bin/
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

upload:
name: Upload release assets
docker-image:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
name: create docker image
runs-on: ubuntu-latest
needs: [build]
steps:

- name: Branch name
id: branch_name
run: |
echo ::set-output name=SOURCE_NAME::${GITHUB_REF#refs/*/}
echo ::set-output name=SOURCE_BRANCH::${GITHUB_REF#refs/heads/}
echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}
- uses: actions/checkout@v1

- name: Download build artefacts
uses: actions/download-artifact@v1
- name: Checkout code
uses: actions/checkout@v2
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
name: binaries
path: bin/
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: build Docker image
run: |
VERSION=$(git describe --tags)
docker build --build-arg "version=$VERSION" --tag ${IMAGE_NAME} .
- name: Create asset zips
env:
SOURCE_TAG: ${{ steps.branch_name.outputs.SOURCE_TAG }}
- name: push Docker image
run: |
mkdir assets
zip assets/ci-test-${SOURCE_TAG}-windows-amd64.zip bin/ci-test-windows-amd64.exe README.md
zip assets/ci-test-${SOURCE_TAG}-linux-amd64.zip bin/ci-test-linux-amd64 README.md
zip assets/ci-test-${SOURCE_TAG}-darwin-amd64.zip bin/ci-test-darwin-amd64 README.md
sha256sum assets/*zip > assets/SHASUMS256.txt
TAG=$(git describe --tags)
docker tag ${IMAGE_NAME} ${REGISTRY}/${IMAGE_NAME}:${GITHUB_SHA}
docker tag ${IMAGE_NAME} ${REGISTRY}/${IMAGE_NAME}:${TAG}
docker tag ${IMAGE_NAME} ${REGISTRY}/${IMAGE_NAME}:latest
docker push ${REGISTRY}/${IMAGE_NAME}:${GITHUB_SHA}
docker push ${REGISTRY}/${IMAGE_NAME}:${TAG}
docker push ${REGISTRY}/${IMAGE_NAME}:latest
- name: Upload release assets
uses: skx/github-action-publish-binaries@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
args: './assets/*'
35 changes: 35 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
dir: .
goos:
- linux
- darwin
- windows
- freebsd
archives:
- files:
- README.md
- LICENSE
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# example docker image for
# https://github.com/jandelgado/golang-ci-template-github-actions/

FROM golang:1.17-alpine as builder
ARG version

WORKDIR /go/src/app
ADD . /go/src/app

RUN CGO_ENABLED=0 \
go build -ldflags "-s -w" -o app

FROM gcr.io/distroless/base-debian10
LABEL maintainer="Jan Delgado <[email protected]>"

COPY --from=builder /go/src/app/app /
ENTRYPOINT ["/app"]
47 changes: 35 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,43 @@
## Info

This repository serves as a template for github-actions integrated go projects.
It consists of a `hello, world!` like example in source file `main.go` which
gets compiled into binary `golang-ci-template-github-actions`. The CI runs some
[linters](https://github.com/golangci/golangci-lint-action) on the code, before
the unit tests are executed. When the build stage was successful, build
artifacts are uploaded and available in the CI job status.

For demonstration purposes, a linux, macos and windows target are created and
packetized in a zip-archive.
It consists of a `hello, world!` like example in source file [main.go](main.go)
which gets compiled into the binary `golang-ci-template-github-actions`. The CI
runs some [linters](https://github.com/golangci/golangci-lint-action) on the
code, before the unit tests are executed. Test coverage is uploaded to
coveralls.io.

[goreleaser](https://github.com/goreleaser/goreleaser) is used to create the
final multi-plattform assets, which are automatically uploaded to the
[release](https://github.com/jandelgado/golang-ci-template-github-actions/releases/latest).
The [release-process](#creating-a-release) is triggered by pushing a git tag to
the repository.

Finally, a docker image is built, which gets published to
[ghcr.io](https://github.com/jandelgado/golang-ci-template-github-actions/pkgs/container/golang-ci-template-github-actions).
Run it with

```console
$ docker run --rm ghcr.io/jandelgado/golang-ci-template-github-actions:latest
hello, world!
```

## Creating a release

On your repositories home (github.com) go to `Releases` > `create release`.
When a new release is created, the released-artifacts are automatically
uploaded to github and available on the [releases page](/releases)
A new release is created by creating a git tag and pushing it, e.g.:

```console
$ git tag -a "v1.2.3" -m "this is release v1.2.3"
$ git push origin v1.2.3
```

The push of the new tag triggers the CI, which uses goreleaser to:
* build multiplatform release artifacts
* create a new release
* upload the artifacts, which are then available on the [releases page](/jandelgado/golang-ci-template-github-actions/releases).

Finally, a docker image is built, which gets published to
[ghcr.io](https://github.com/jandelgado/golang-ci-template-github-actions/pkgs/container/golang-ci-template-github-actions).

## Test coverage (coveralls)

Expand All @@ -49,5 +72,5 @@ with the test coverage to affected pull requests:

## Author

Jan Delgado
(c) copyright 2021 by Jan Delgado

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/jandelgado/golang-ci-template-github-actions

go 1.12
go 1.17

0 comments on commit 7aa7d46

Please sign in to comment.