forked from rear/rear
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
134 lines (125 loc) · 4.44 KB
/
Copy pathJenkinsfile
File metadata and controls
134 lines (125 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
@Library('cove')
import nable.cove.helpers.ShellHelper
import nable.cove.SecretManager
final String jobName = env.JOB_NAME.split('/')[-2]
final String branchName = env.CHANGE_TARGET ?: env.BRANCH_NAME
final boolean isProd = jobName.endsWith('-prd')
final boolean isProdBranch = branchName == 'master'
final String envType = isProd ? 'prd' : 'dev'
def String repositoryName = 'rear'
def config = [
cloud: envType == 'prd' ? 'cove-agent-prd' : "backup-${envType}",
serviceAccount: envType == 'prd' ? 'ecr' : 'backup',
buildImage: "${nsbuild.ecrHost()}/cove/onprem/develop/rear-builder:v1.1"
]
def secrets = [
jenkins: [
'github-app': [
usernamePassword: [
usernameVariable: 'GITHUB_USERNAME',
passwordVariable: 'GITHUB_PASSWORD'
]
]
],
kubernetes: [
'artifactory': [
'JFROG_USERNAME': 'ARTIFACTORY_USERNAME_COVE',
'JFROG_ACCESS_TOKEN': 'ARTIFACTORY_TOKEN_COVE'
]
]
]
def secretManager
def shellHelper
def shouldBuild = true
pipeline {
agent {
kubernetes {
cloud "${config.cloud}"
yaml nsbuild.agentYaml(config)
defaultContainer nsbuild.defaultContainer(config)
}
}
options {
ansiColor('xterm')
}
stages {
stage('Prepare') {
steps {
script {
if (isProd != isProdBranch) {
echo "Environment mismatch: target branch is ${branchName}, job is ${jobName}. Skipping build."
shouldBuild = false
} else {
echo "Environment match: target branch is ${branchName}, job is ${jobName}. Proceeding with build."
}
secretManager = new SecretManager(
this,
envType,
jenkinsWhitelist: [
'github-app'
],
k8sWhitelist: [
'artifactory'
]
)
shellHelper = new ShellHelper(this, isUnix: true)
}
}
}
stage('Load secrets') {
when {
expression { shouldBuild }
}
agent {
kubernetes {
cloud "${config.cloud}"
yaml secretManager.getK8sPodYaml(secrets)
defaultContainer 'secrets'
customWorkspace 'w'
}
}
steps {
script {
secretManager.loadJenkinsSecrets(secrets.jenkins)
secretManager.loadK8sSecrets(secrets.kubernetes, shellHelper)
}
}
}
stage('Build') {
when {
expression { shouldBuild }
}
environment {
ARTIFACTORY_URL = 'https://mspsolarwinds.jfrog.io/artifactory'
}
steps {
script {
secretManager.withSecrets {
shellHelper.exec('Validate', """
make validate
""")
shellHelper.exec('Run unit tests', """
make test-cove
""")
shellHelper.exec('Build', """
make dist
""")
def repository = (envType == 'dev') ? 'cove-generic-develop-local' : 'cove-generic-release-local'
shellHelper.exec('Upload', """
VERSION="\$(make version)"
PACKAGE="rear-\${VERSION}.tar.gz"
TARGET_PACKAGE="\$PACKAGE"
if [ "${envType}" != "dev" ]; then
COMMIT_REV="\$(git rev-parse HEAD | cut -c 1-8)"
TARGET_PACKAGE="rear-\${VERSION}-\${COMMIT_REV}.tar.gz"
fi
curl -sSf -X PUT -T dist/\${PACKAGE} \
-u \${ARTIFACTORY_USERNAME_COVE}:\${ARTIFACTORY_TOKEN_COVE} \
\${ARTIFACTORY_URL}/${repository}/rear/\${TARGET_PACKAGE}
""")
}
}
}
}
}
}