- a "push to any branch" trigger for your regular CI (doesn't invoke goreleaser)
- a "push to tag" trigger which invokes goreleaser
The push to any branch trigger could use a Dockerfile or a cloudbuild.yaml,
whichever you prefer.
You should have a dedicated cloudbuild.release.yaml that is only used by the "push to
tag" trigger.
In this example we're creating a new release every time a new tag is pushed.
See [Using Encrypted Resources](https://cloud.google.com/cloud-build/docs/securing-builds/use-encrypted-secrets-credentials) for how to encrypt and base64-encode your github token.
The clone that the build uses [has no
tags](https://issuetracker.google.com/u/1/issues/113668706), which is why we
must explicitly run git tag $TAG_NAME (note that $TAG_NAME is only set when
your build is triggered by a "push to tag".) This will allow goreleaser to
create a release with that version, but it won't be able to build a proper
changelog containing just the messages from the commits since the prior tag.
Next, in the GitLab sidebar add the variables `DOCKER_USERNAME`, `DOCKER_PASSWORD` and `GITHUB_TOKEN` through Project --> Settings --> CI / CD --> Variables.\
Make sure they are set to *Masked* (*Protection* is not needed).
To push to some other Docker registry (e.g. to a GitLab registry), set different variables in the file above:
```txt
CI_REGISTRY: gitlab.example.com:4567
DOCKER_REGISTRY: $CI_REGISTRY
DOCKER_USERNAME: gitlab-ci-token
DOCKER_PASSWORD: $CI_JOB_TOKEN
```
Make sure the `image_templates` in the file `.goreleaser.yml` reflect that custom registry!
Codefresh uses Docker based pipelines where all steps must be Docker containers. Using Goreleaser is very easy via the [existing Docker image](https://hub.docker.com/r/goreleaser/goreleaser/).
Here is an example pipeline that builds a Go application and then uses Goreleaser.
```yaml
version: '1.0'
stages:
- prepare
- build
- release
steps:
main_clone:
title: 'Cloning main repository...'
type: git-clone
repo: '${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}'
revision: '${{CF_REVISION}}'
stage: prepare
BuildMyApp:
title: Compiling go code
stage: build
image: 'golang:1.12'
commands:
- go build
ReleaseMyApp:
title: Creating packages
stage: release
image: 'goreleaser/goreleaser'
commands:
- goreleaser --rm-dist
```
You need to pass the variable `GITHUB_TOKEN` in the Codefresh UI that contains credentials to your Github account or load it from [shared configuration](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/shared-configuration/).
You should also restrict this pipeline to run only on tags when you add [git triggers](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/git-triggers/) on it.
More details can be found in the [goreleaser example page](https://codefresh.io/docs/docs/learn-by-example/golang/goreleaser/).