|
1 | 1 | # Building Continuous Delivery (CD) Pipelines
|
2 | 2 |
|
3 | 3 | ## Writing and using a shared library
|
| 4 | + |
| 5 | +1. Set up a new GitHub repository named `jenkins-standard-go-pipeline`. It will define a standard pipeline definition for Go projects implemented as shared library. |
| 6 | +2. Add the file `vars/standard.groovy` that defines the declarative pipeline as global variable. Make the Go tool name and golang-ci version configurable with the help of parameters. |
| 7 | +3. Push the changes to the `master` branch and configure the shared library in Jenkins. |
| 8 | +4. Configure the shared library for consumption in Jenkins with the name `go-pipeline`. |
| 9 | +5. Set up a new GitHub repository named `go-project-by-template`. Initialize a new Go project by running `go mod init github.com/bmuschko/hello-world` and adding a simple `main.go` file. |
| 10 | +6. Add a new `Jenkinsfile`. Consume the shared library and call the global variable. |
| 11 | +7. Trigger a build and visualize the pipeline in Jenkins. |
| 12 | + |
| 13 | +<details><summary>Show Solution</summary> |
| 14 | +<p> |
| 15 | + |
| 16 | +The directory structure of shared library repository should have the following structure. |
| 17 | + |
| 18 | +``` |
| 19 | +. |
| 20 | +└── vars |
| 21 | + └── standard.groovy |
| 22 | +
|
| 23 | +1 directory, 1 file |
| 24 | +``` |
| 25 | + |
| 26 | +Define the pipeline as global variable in the file `standard.groovy`. |
| 27 | + |
| 28 | +```groovy |
| 29 | +def call(String goToolName = 'go-1.12', String golangCiVersion = 'v1.12.5') { |
| 30 | + pipeline { |
| 31 | + agent any |
| 32 | + tools { |
| 33 | + go "$goToolName" |
| 34 | + } |
| 35 | + environment { |
| 36 | + GO111MODULE = 'on' |
| 37 | + } |
| 38 | + stages { |
| 39 | + stage('Compile') { |
| 40 | + steps { |
| 41 | + sh 'go build' |
| 42 | + } |
| 43 | + } |
| 44 | + stage('Test') { |
| 45 | + environment { |
| 46 | + CODECOV_TOKEN = credentials('CODECOV_TOKEN') |
| 47 | + } |
| 48 | + steps { |
| 49 | + sh 'go test ./... -coverprofile=coverage.txt' |
| 50 | + sh "curl -s https://codecov.io/bash | bash -s -" |
| 51 | + } |
| 52 | + } |
| 53 | + stage('Code Analysis') { |
| 54 | + steps { |
| 55 | + sh "curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin $golangCiVersion" |
| 56 | + sh 'golangci-lint run' |
| 57 | + } |
| 58 | + } |
| 59 | + stage('Release') { |
| 60 | + when { |
| 61 | + buildingTag() |
| 62 | + } |
| 63 | + environment { |
| 64 | + GITHUB_TOKEN = credentials('GITHUB_TOKEN') |
| 65 | + } |
| 66 | + steps { |
| 67 | + sh 'curl -sL https://git.io/goreleaser | bash' |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +Configure the shared library under _Manage Jenkins > Configure System_. |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +The directory structure should look as shown below. |
| 80 | + |
| 81 | +``` |
| 82 | +$ tree |
| 83 | +. |
| 84 | +├── Jenkinsfile |
| 85 | +├── go.mod |
| 86 | +└── main.go |
| 87 | +
|
| 88 | +0 directories, 3 files |
| 89 | +``` |
| 90 | + |
| 91 | +The `Jenkinsfile` uses the shared library and calls the global variable. Optionally, you can configure the pipeline by passing in parameters. |
| 92 | + |
| 93 | +```groovy |
| 94 | +@Library('go-pipeline') _ |
| 95 | +
|
| 96 | +standard() |
| 97 | +``` |
| 98 | + |
| 99 | +The resulting build will go through all the pipeline stages defined in the shared library. |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +</p> |
| 104 | +</details> |
0 commit comments