diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e2201d1..34a95e5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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/gcov2lcov-action@v1.0.6 - - name: Coveralls - uses: coverallsapp/github-action@v1.1.2 - 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/gcov2lcov-action@v1.0.6 + - name: Coveralls + uses: coverallsapp/github-action@v1.1.2 + with: github-token: ${{ secrets.github_token }} path-to-lcov: coverage.lcov - diff --git a/.github/workflows/upload_assets.yml b/.github/workflows/upload_assets.yml index a31db25..2971412 100644 --- a/.github/workflows/upload_assets.yml +++ b/.github/workflows/upload_assets.yml @@ -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/*' diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..9f21709 --- /dev/null +++ b/.goreleaser.yml @@ -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:' diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b500182 --- /dev/null +++ b/Dockerfile @@ -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 " + +COPY --from=builder /go/src/app/app / +ENTRYPOINT ["/app"] diff --git a/README.md b/README.md index 79e332f..7c6cbe5 100644 --- a/README.md +++ b/README.md @@ -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) @@ -49,5 +72,5 @@ with the test coverage to affected pull requests: ## Author -Jan Delgado +(c) copyright 2021 by Jan Delgado diff --git a/go.mod b/go.mod index 7973ad2..6a595a0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/jandelgado/golang-ci-template-github-actions -go 1.12 +go 1.17