Skip to content

Commit 7aa7d46

Browse files
authored
use goreleaser (#10)
1 parent 37a8870 commit 7aa7d46

File tree

6 files changed

+159
-95
lines changed

6 files changed

+159
-95
lines changed

.github/workflows/test.yml

+35-36
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,51 @@ jobs:
1111
lint:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- name: Install Go
15-
uses: actions/setup-go@v2
16-
with:
17-
go-version: 1.15.x
18-
- name: Checkout code
19-
uses: actions/checkout@v2
20-
- name: Run linters
21-
uses: golangci/golangci-lint-action@v2
22-
with:
23-
version: v1.29
14+
- name: Install Go
15+
uses: actions/setup-go@v2
16+
with:
17+
go-version: 1.17.x
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
- name: Run linters
21+
uses: golangci/golangci-lint-action@v2
22+
with:
23+
version: v1.29
2424

2525
test:
2626
strategy:
2727
matrix:
28-
go-version: [1.15.x]
28+
go-version: [1.17.x]
2929
platform: [ubuntu-latest, macos-latest, windows-latest]
3030
runs-on: ${{ matrix.platform }}
3131
steps:
32-
- name: Install Go
33-
if: success()
34-
uses: actions/setup-go@v2
35-
with:
36-
go-version: ${{ matrix.go-version }}
37-
- name: Checkout code
38-
uses: actions/checkout@v2
39-
- name: Run tests
40-
run: go test -v -covermode=count
32+
- name: Install Go
33+
if: success()
34+
uses: actions/setup-go@v2
35+
with:
36+
go-version: ${{ matrix.go-version }}
37+
- name: Checkout code
38+
uses: actions/checkout@v2
39+
- name: Run tests
40+
run: go test -v -covermode=count
4141

4242
coverage:
4343
runs-on: ubuntu-latest
4444
steps:
45-
- name: Install Go
46-
if: success()
47-
uses: actions/setup-go@v2
48-
with:
49-
go-version: 1.15.x
50-
- name: Checkout code
51-
uses: actions/checkout@v2
52-
- name: Calc coverage
53-
run: |
54-
go test -v -covermode=count -coverprofile=coverage.out
55-
- name: Convert coverage.out to coverage.lcov
56-
uses: jandelgado/gcov2lcov-action@v1.0.6
57-
- name: Coveralls
58-
uses: coverallsapp/github-action@v1.1.2
59-
with:
45+
- name: Install Go
46+
if: success()
47+
uses: actions/setup-go@v2
48+
with:
49+
go-version: 1.17.x
50+
- name: Checkout code
51+
uses: actions/checkout@v2
52+
- name: Calc coverage
53+
run: |
54+
go test -v -covermode=count -coverprofile=coverage.out
55+
- name: Convert coverage.out to coverage.lcov
56+
uses: jandelgado/gcov2lcov-action@v1.0.6
57+
- name: Coveralls
58+
uses: coverallsapp/github-action@v1.1.2
59+
with:
6060
github-token: ${{ secrets.github_token }}
6161
path-to-lcov: coverage.lcov
62-

.github/workflows/upload_assets.yml

+36-46
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,55 @@
11
on:
2-
release:
3-
types: [created]
2+
push:
3+
tags:
4+
- 'v*'
45

5-
name: Upload release assets after release is created
6+
name: Upload release assets after tagging
67
jobs:
78
build:
8-
name: build binaries
9+
name: create assets
910
runs-on: ubuntu-latest
1011
steps:
1112
- name: Install Go
1213
uses: actions/setup-go@v2
1314
with:
14-
go-version: 1.15.x
15+
go-version: 1.17.x
1516
- name: Checkout code
1617
uses: actions/checkout@v2
17-
- name: build
18-
run: |
19-
echo "GO111MODULE=on" >> $GITHUB_ENV
20-
GOOS=windows GOARCH=amd64 go build -o bin/ci-test-windows-amd64.exe
21-
GOOS=linux GOARCH=amd64 go build -o bin/ci-test-linux-amd64
22-
GOOS=darwin GOARCH=amd64 go build -o bin/ci-test-macos-amd64
23-
- name: upload artifacts
24-
uses: actions/upload-artifact@master
18+
- name: Run GoReleaser
19+
uses: goreleaser/goreleaser-action@v2
2520
with:
26-
name: binaries
27-
path: bin/
21+
version: latest
22+
args: release --rm-dist
23+
env:
24+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2825

29-
upload:
30-
name: Upload release assets
26+
docker-image:
27+
env:
28+
REGISTRY: ghcr.io
29+
IMAGE_NAME: ${{ github.repository }}
30+
name: create docker image
3131
runs-on: ubuntu-latest
32-
needs: [build]
3332
steps:
34-
35-
- name: Branch name
36-
id: branch_name
37-
run: |
38-
echo ::set-output name=SOURCE_NAME::${GITHUB_REF#refs/*/}
39-
echo ::set-output name=SOURCE_BRANCH::${GITHUB_REF#refs/heads/}
40-
echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}
41-
42-
- uses: actions/checkout@v1
43-
44-
- name: Download build artefacts
45-
uses: actions/download-artifact@v1
33+
- name: Checkout code
34+
uses: actions/checkout@v2
35+
- name: Log in to the Container registry
36+
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
4637
with:
47-
name: binaries
48-
path: bin/
38+
registry: ${{ env.REGISTRY }}
39+
username: ${{ github.actor }}
40+
password: ${{ secrets.GITHUB_TOKEN }}
41+
- name: build Docker image
42+
run: |
43+
VERSION=$(git describe --tags)
44+
docker build --build-arg "version=$VERSION" --tag ${IMAGE_NAME} .
4945
50-
- name: Create asset zips
51-
env:
52-
SOURCE_TAG: ${{ steps.branch_name.outputs.SOURCE_TAG }}
46+
- name: push Docker image
5347
run: |
54-
mkdir assets
55-
zip assets/ci-test-${SOURCE_TAG}-windows-amd64.zip bin/ci-test-windows-amd64.exe README.md
56-
zip assets/ci-test-${SOURCE_TAG}-linux-amd64.zip bin/ci-test-linux-amd64 README.md
57-
zip assets/ci-test-${SOURCE_TAG}-darwin-amd64.zip bin/ci-test-darwin-amd64 README.md
58-
sha256sum assets/*zip > assets/SHASUMS256.txt
48+
TAG=$(git describe --tags)
49+
docker tag ${IMAGE_NAME} ${REGISTRY}/${IMAGE_NAME}:${GITHUB_SHA}
50+
docker tag ${IMAGE_NAME} ${REGISTRY}/${IMAGE_NAME}:${TAG}
51+
docker tag ${IMAGE_NAME} ${REGISTRY}/${IMAGE_NAME}:latest
52+
docker push ${REGISTRY}/${IMAGE_NAME}:${GITHUB_SHA}
53+
docker push ${REGISTRY}/${IMAGE_NAME}:${TAG}
54+
docker push ${REGISTRY}/${IMAGE_NAME}:latest
5955
60-
- name: Upload release assets
61-
uses: skx/github-action-publish-binaries@master
62-
env:
63-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64-
with:
65-
args: './assets/*'

.goreleaser.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
before:
4+
hooks:
5+
# You may remove this if you don't use go modules.
6+
- go mod tidy
7+
builds:
8+
- env:
9+
- CGO_ENABLED=0
10+
dir: .
11+
goos:
12+
- linux
13+
- darwin
14+
- windows
15+
- freebsd
16+
archives:
17+
- files:
18+
- README.md
19+
- LICENSE
20+
replacements:
21+
darwin: Darwin
22+
linux: Linux
23+
windows: Windows
24+
386: i386
25+
amd64: x86_64
26+
checksum:
27+
name_template: 'checksums.txt'
28+
snapshot:
29+
name_template: "{{ incpatch .Version }}-next"
30+
changelog:
31+
sort: asc
32+
filters:
33+
exclude:
34+
- '^docs:'
35+
- '^test:'

Dockerfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# example docker image for
2+
# https://github.com/jandelgado/golang-ci-template-github-actions/
3+
4+
FROM golang:1.17-alpine as builder
5+
ARG version
6+
7+
WORKDIR /go/src/app
8+
ADD . /go/src/app
9+
10+
RUN CGO_ENABLED=0 \
11+
go build -ldflags "-s -w" -o app
12+
13+
FROM gcr.io/distroless/base-debian10
14+
LABEL maintainer="Jan Delgado <jdelgado@gmx.net>"
15+
16+
COPY --from=builder /go/src/app/app /
17+
ENTRYPOINT ["/app"]

README.md

+35-12
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,43 @@
1818
## Info
1919

2020
This repository serves as a template for github-actions integrated go projects.
21-
It consists of a `hello, world!` like example in source file `main.go` which
22-
gets compiled into binary `golang-ci-template-github-actions`. The CI runs some
23-
[linters](https://github.com/golangci/golangci-lint-action) on the code, before
24-
the unit tests are executed. When the build stage was successful, build
25-
artifacts are uploaded and available in the CI job status.
26-
27-
For demonstration purposes, a linux, macos and windows target are created and
28-
packetized in a zip-archive.
21+
It consists of a `hello, world!` like example in source file [main.go](main.go)
22+
which gets compiled into the binary `golang-ci-template-github-actions`. The CI
23+
runs some [linters](https://github.com/golangci/golangci-lint-action) on the
24+
code, before the unit tests are executed. Test coverage is uploaded to
25+
coveralls.io.
26+
27+
[goreleaser](https://github.com/goreleaser/goreleaser) is used to create the
28+
final multi-plattform assets, which are automatically uploaded to the
29+
[release](https://github.com/jandelgado/golang-ci-template-github-actions/releases/latest).
30+
The [release-process](#creating-a-release) is triggered by pushing a git tag to
31+
the repository.
32+
33+
Finally, a docker image is built, which gets published to
34+
[ghcr.io](https://github.com/jandelgado/golang-ci-template-github-actions/pkgs/container/golang-ci-template-github-actions).
35+
Run it with
36+
37+
```console
38+
$ docker run --rm ghcr.io/jandelgado/golang-ci-template-github-actions:latest
39+
hello, world!
40+
```
2941

3042
## Creating a release
3143

32-
On your repositories home (github.com) go to `Releases` > `create release`.
33-
When a new release is created, the released-artifacts are automatically
34-
uploaded to github and available on the [releases page](/releases)
44+
A new release is created by creating a git tag and pushing it, e.g.:
45+
46+
```console
47+
$ git tag -a "v1.2.3" -m "this is release v1.2.3"
48+
$ git push origin v1.2.3
49+
```
50+
51+
The push of the new tag triggers the CI, which uses goreleaser to:
52+
* build multiplatform release artifacts
53+
* create a new release
54+
* upload the artifacts, which are then available on the [releases page](/jandelgado/golang-ci-template-github-actions/releases).
55+
56+
Finally, a docker image is built, which gets published to
57+
[ghcr.io](https://github.com/jandelgado/golang-ci-template-github-actions/pkgs/container/golang-ci-template-github-actions).
3558

3659
## Test coverage (coveralls)
3760

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

5073
## Author
5174

52-
Jan Delgado
75+
(c) copyright 2021 by Jan Delgado
5376

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/jandelgado/golang-ci-template-github-actions
22

3-
go 1.12
3+
go 1.17

0 commit comments

Comments
 (0)