Skip to content

Commit edba081

Browse files
committed
Write up description and add images
1 parent 185335f commit edba081

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

13-advanced-jenkinsfile.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ You can implement the "Test" stage as follows.
3131
```groovy
3232
stage('Test') {
3333
environment {
34-
CODECOV_TOKEN = credentials('codecov_token')
34+
CODECOV_TOKEN = credentials('CODECOV_TOKEN')
3535
}
3636
steps {
3737
sh 'go test ./... -coverprofile=coverage.txt'
@@ -63,7 +63,7 @@ stage('Release') {
6363
buildingTag()
6464
}
6565
environment {
66-
GITHUB_TOKEN = credentials('github_token')
66+
GITHUB_TOKEN = credentials('GITHUB_TOKEN')
6767
}
6868
steps {
6969
sh 'curl -sL https://git.io/goreleaser | bash'

14-shared-library.md

+101
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,104 @@
11
# Building Continuous Delivery (CD) Pipelines
22

33
## 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+
![Shared Library Configuration](./images/14-shared-library/shared-library-config.png)
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+
![Pipeline View](./images/14-shared-library/pipeline-view.png)
102+
103+
</p>
104+
</details>
1.19 MB
Loading
Loading

0 commit comments

Comments
 (0)