Once you run this pipeline Codefresh will create a Docker image for the Golang application:
The big advantage of this workflow is that the Dockerfile you use can define any Go version and dependency tool. As long as the Dockerfile is self-contained (i.e. it compiles GO on its own), the pipeline will work as expected.
In the example application, the simple (unoptimized) Dockerfile has an old Go version that still requires GOPATH folders.
Run unit tests as part of the pipeline
If you want to run Go specific steps in your pipeline, you can use freestyle steps with any GO image that you want. If your GO application is using GO modules, this is even easier as you don’t need to place the application into a specific GOPATH compliant directory first.
This pipeline is running unit tests as a separate step and then builds the docker image.
If the unit tests fail, then the docker image will never be created (Codefresh automatically stops a pipeline when there is an error).
Notice that in this case we have added module support in the Go application. The new Dockerfile is the following:
The Dockerfile will also automatically take advantage of the Codefresh distributed docker cache.
Create a multi-stage Docker image for GO
Especially with Go applications, the recommended way to create Docker images is with multi-stage builds. This makes the resulting Docker image as compact as possible.
You can also embed unit tests in the Docker creation process, which guarantee the correctness of image (integration tests are best kept in the pipeline).
Here is the new Dockerfile:
Codefresh has native support for multi-stage builds. The pipeline is the same as the first one with just two steps.
You should see a much smaller Docker image at the end.