Clarify some details

This commit is contained in:
Benjamin Muschko
2019-11-18 17:11:32 -07:00
parent 248499c8f6
commit 57896f0303
17 changed files with 82 additions and 40 deletions

View File

@@ -1,6 +1,6 @@
# Exercise 13
We'll want to enhance the pipeline by additional stages and implement a release workflow. The project is going to use an external tool called [GoReleaser](https://goreleaser.com/) to publish cross-compiled artifacts to [GitHub Releases](https://help.github.com/en/github/administering-a-repository/creating-releases). The binaries should only be released if the commit has been tagged.
We'll want to enhance the existing Go pipeline by additional stages and implement a release workflow. The project is going to use an external tool called [GoReleaser](https://goreleaser.com/) to publish cross-compiled artifacts to [GitHub Releases](https://help.github.com/en/github/administering-a-repository/creating-releases). The binaries should only be released if the commit has been tagged. You will need to create an account on [CodeCov](https://codecov.io/) and set up a [GitHub token](https://github.com/settings/tokens) to follow along.
## Enhancing a Pipeline With Advanced Features
@@ -8,13 +8,11 @@ We'll want to enhance the pipeline by additional stages and implement a release
* Add a build step that runs the shell command `go test ./...`.
* Generate code coverage metrics by adding the option `-coverprofile=coverage.txt` to the build step.
* Publish the code coverage metrics to CodeCov by sending a curl command `curl -s https://codecov.io/bash | bash -s -`.
* Log into [CodeCov](https://codecov.io/), determine the CodeCov token for the repository (aka Repository Upload Token) and set it up as credential in Jenkins.
* Retrieve the credential and set the value as environment variable named `CODECOV_TOKEN`.
2. Add a stage named `Code Analysis` that uses [golangci-lint](https://github.com/golangci/golangci-lint) to detect issues with the code.
* Add a build step for installing the `golangci-lint` with the shell command `curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.17.1`
* Add a build step for installing the `golangci-lint` with the shell command `curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.18.0`
* Add a build step that runs `golangci-lint` with the shell command `golangci-lint run`.
3. Add a stage named `Release` that uses [GoReleaser](https://github.com/goreleaser/goreleaser).
* Only build this step if the commit has been tagged.
* Set up a credential in Jenkins named `github_token` and set your GitHub token.
* Set up a credential in Jenkins named `github_token` and set your [GitHub token](https://github.com/settings/tokens). Create a new token if you didn't set up one yet.
* Retrieve the credential and set the value as environment variable named `GITHUB_TOKEN`.
* Add a build step that runs the shell command `curl -sL https://git.io/goreleaser | bash`.

View File

@@ -1,16 +1,9 @@
# Solution
Create the credentials for the CodeCov token.
![CodeCov Credentials](./images/codecov_token_credentials.png)
You can implement the "Test" stage as follows.
```groovy
stage('Test') {
environment {
CODECOV_TOKEN = credentials('CODECOV_TOKEN')
}
steps {
sh 'go test ./... -coverprofile=coverage.txt'
sh "curl -s https://codecov.io/bash | bash -s -"
@@ -23,7 +16,7 @@ You can implement the "Code Analysis" stage as follows.
```groovy
stage('Code Analysis') {
steps {
sh 'curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.17.1'
sh 'curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.18.0'
sh 'golangci-lint run'
}
}
@@ -47,4 +40,48 @@ stage('Release') {
sh 'curl -sL https://git.io/goreleaser | bash'
}
}
```
The final `Jenkinsfile` looks similar to the solution below.
```groovy
pipeline {
agent any
tools {
go 'go-1.12'
}
environment {
GO111MODULE = 'on'
}
stages {
stage('Build') {
steps {
sh 'go build'
}
}
stage('Test') {
steps {
sh 'go test ./... -coverprofile=coverage.txt'
sh "curl -s https://codecov.io/bash | bash -s -"
}
}
stage('Code Analysis') {
steps {
sh 'curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.18.0'
sh 'golangci-lint run'
}
}
stage('Release') {
when {
buildingTag()
}
environment {
GITHUB_TOKEN = credentials('GITHUB_TOKEN')
}
steps {
sh 'curl -sL https://git.io/goreleaser | bash'
}
}
}
}
```