-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.groovy
183 lines (173 loc) · 5.86 KB
/
build.groovy
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import groovy.transform.Field
@Field String PROJECTS = "block-explorer-indexer block-explorer" as String
@Field String APPS = 'api worker scheduler block-explorer'
def redeployProjects(String deploymentsString, String namespace, String link) {
Collection deployments = deploymentsString.split(' ')
deployments.each { project ->
// Modify the workload path according to your project naming convention
def workloadPath = "${link}:${namespace}:${project}"
echo "Deploying ${project} in ${workloadPath}"
rancherRedeploy alwaysPull: true, images: '', credential: 'RANCHER_SCMAIN', workload: workloadPath
}
}
static Boolean isMainBranch(String branchName) {
return branchName == 'dev' || branchName == 'prod'
}
Boolean shouldBundle() {
def commitMessage = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
print commitMessage
String branchName = env.BRANCH_NAME
return commitMessage.contains('bundletest') || isMainBranch(branchName)
}
String getBundleSuffix() {
if (env.BRANCH_NAME == "prod") {
return "prod"
} else {
return "dev"
}
}
def buildDockerImage(String project) {
if (shouldBundle()) {
sh """
DOCKER_BUILDKIT=1
docker build --pull \
-t \$IMAGE_PATH/${project}:${getBundleSuffix()} \
--build-arg GITHUB_SHA=${env.GIT_SHA} \
--build-arg BRANCH_NAME=${env.BRANCH_NAME} \
${project}
"""
} else {
sh "DOCKER_BUILDKIT=1 docker build ${project}"
}
}
def pushDockerImage(String projects) {
sh """
for i in ${projects}; do \
docker push --all-tags \$IMAGE_PATH/\$i; \
done
"""
}
pipeline {
options {
// disableConcurrentBuilds();
buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '5', numToKeepStr: '5'))
}
environment {
CI = true
GIT_SHA = "${sh(returnStdout: true, script: 'echo ${GIT_COMMIT} | cut -c1-12').trim()}"
IMAGE_PATH = 'goharbor.goharbor.svc.cluster.local:80/rootscan'
}
agent {
node {
label 'alpine1'
}
}
stages {
stage('Install') {
parallel {
stage('pnpm install') {
steps {
sh 'pnpm install --frozen-lockfile --unsafe-perm'
}
}
stage('Debug') {
steps {
sh 'node --version'
sh 'npm --version'
sh 'pnpm --version'
sh 'python --version'
sh 'pre-commit --version'
sh 'printenv'
script {
def branchName = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
echo "The current branch name is: ${branchName}"
echo "GIT_SHA is: ${GIT_SHA}"
}
}
}
stage('Docker Login') {
steps {
withCredentials([usernamePassword(credentialsId: 'harbor-token', usernameVariable: 'HARBOR_ROBOT_USER', passwordVariable: 'HARBOR_ROBOT_USER_TOKEN')]) {
sh 'docker login -u "${HARBOR_ROBOT_USER}" --password "${HARBOR_ROBOT_USER_TOKEN}" ${IMAGE_PATH}'
}
}
}
}
}
stage('Build and Lint') {
parallel {
stage('Lint') {
steps {
sh 'pnpm nx reset'
sh 'pnpm run lint'
}
}
// stage('Test') {
// steps {
// sh 'pnpm run test'
// }
// }
stage('block-explorer-indexer') {
steps {
script {
buildDockerImage('block-explorer-indexer')
}
}
}
stage('block-explorer') {
steps {
script {
buildDockerImage('block-explorer')
}
}
}
}
}
stage('Push') {
when {
expression {
return shouldBundle()
}
}
parallel {
stage('Push docker images') {
steps {
script {
pushDockerImage(PROJECTS)
}
}
}
}
}
stage('Deploy') {
parallel {
stage('dev') {
when {
anyOf {
branch 'dev'
}
}
steps {
script {
redeployProjects(APPS, 'rootscan-porcini', '/project/c-m-pmrf7rl9:p-hrxsr/workload/deployment')
redeployProjects(APPS, 'rootscan-root', '/project/c-m-pmrf7rl9:p-hrxsr/workload/deployment')
}
}
}
stage('prod') {
when {
anyOf {
branch 'prod'
}
}
steps {
script {
redeployProjects(APPS, 'rootscan-porcini', '/project/c-m-h8j7fnkg:p-8djdg/workload/deployment')
redeployProjects(APPS, 'rootscan-root', '/project/c-m-h8j7fnkg:p-8djdg/workload/deployment')
}
}
}
}
}
}
}