Compile and release a Go application

Using Codefresh pipelines

Goreleaser is a helper utility that allows you to easily create the following for Go applications:

  • Binary packages for each OS/arch
  • Archives
  • GitHub releases
  • Docker images
  • Snap/RPM/deb/Homebrew

Codefresh can also create Docker images on its own, but Goreleaser is still useful for the binary artifact creation capability.

Run Goreleaser with docker

You can see the example project at https://github.com/codefresh-contrib/goreleaser-sample-app. The repository contains a simple Golang web application with a goreleaser configuration.

There is already a Docker image for Goreleaser so it is very easy to use it in Codefresh pipeline. In the most simple case you case run goreleaser in a freestyle step.

YAML

  ReleaseMyApp:
    title: Creating packages
    stage: release
    image: 'goreleaser/goreleaser'
    commands:
      - goreleaser --snapshot --skip-publish --rm-dist

More typically however you also need to provide a GitHub token so that GitHub releases are also available. There are two ways to do that.

Create a CI pipeline that compiles/releases Go

In most cases you want to just reuse the Git integration already defined in Codefresh. This pipeline is using the GitHub token from Git integration in order to allow GitHub access.

codefresh.yml

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
  GetGitToken:
    title: Reading GitHub token
    stage: release
    image: codefresh/cli
    commands:
      - cf_export GITHUB_TOKEN=$(codefresh get context github-1 --decrypt -o yaml | yq -y .spec.data.auth.password)     
  ReleaseMyApp:
    title: Creating packages
    stage: release
    image: 'goreleaser/goreleaser'
    commands:
      - goreleaser --rm-dist 

Note that GoReleaser requires a GitHub API token (GITHUB_TOKEN) with the repo scope to deploy artifacts to GitHub. Here we use cf_export and the codefresh CLI in order to ask Codefresh about the existing token (that was used in git integrations). In your case you need to change github-1 with the name of your GitHub integration.

It also possible to pass a GITHUB_TOKEN directly in the pipeline, if you don’t want to re-use the existing one. This is an alternative way of allowing Goreleaser to create GitHub releases.

Passing a specific github token in the pipeline

Passing a specific github token in the pipeline

You could also store the token in shared configuration. Regardless of the way you choose to pass the GitHub token, the final step is to make sure that your pipeline is only executed for tag events.

Run pipeline only on tag creation

Run pipeline only on tag creation

This means that this pipeline will not run on normal commits. It is also possible to use step conditionals for more complex cases.

Codefresh YAML for pipeline definitions
Steps in pipelines
Creating pipelines
How Codefresh pipelines work