-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathJenkinsfile
More file actions
185 lines (167 loc) · 6.34 KB
/
Jenkinsfile
File metadata and controls
185 lines (167 loc) · 6.34 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
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
184
185
@Library('podTemplateLib')
import net.santiment.utils.podTemplates
import java.util.UUID
properties([
buildDiscarder(
logRotator(artifactDaysToKeepStr: '30',
artifactNumToKeepStr: '',
daysToKeepStr: '30',
numToKeepStr: ''))
])
slaveTemplates = new podTemplates()
def numPartitions = 4
slaveTemplates.dockerTemplate { label ->
node(label) {
stage('Build Test Image') {
container('docker') {
def scmVars = checkout scm
def gitHead = scmVars.GIT_COMMIT.substring(0, 7)
def sanitizedBranchName = env.BRANCH_NAME.replaceAll('/', '-')
def changeId = env.CHANGE_ID ?: 'none'
def imageTag = "sanbase-test:${scmVars.GIT_COMMIT}-${env.BUILD_ID}-${sanitizedBranchName}-${changeId}"
sh "docker build \
-t ${imageTag} \
-f Dockerfile-test . \
--progress plain"
// Store variables for later stages
env.TEST_IMAGE_TAG = imageTag
env.GIT_HEAD = gitHead
env.GIT_COMMIT_FULL = scmVars.GIT_COMMIT
}
}
stage('Run Tests') {
container('docker') {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
def imageTag = env.TEST_IMAGE_TAG
def runToken = UUID.randomUUID().toString().substring(0, 8)
def buildSuffix = "${env.GIT_COMMIT_FULL}-${env.BUILD_ID}-${runToken}"
def networkName = "test-net-${buildSuffix}"
env.TEST_RUN_SUFFIX = buildSuffix
def failuresDir = "${pwd()}/test_failures_${buildSuffix}"
sh "mkdir -p ${failuresDir}"
sh "docker network create ${networkName} >/dev/null 2>&1 || true"
for (int i = 1; i <= numPartitions; i++) {
def postgresContainerName = "test-postgres-${buildSuffix}-p${i}"
sh "docker rm -f ${postgresContainerName} >/dev/null 2>&1 || true"
sh "docker run \
--rm --name ${postgresContainerName} \
--network ${networkName} \
--network-alias test-db-p${i} \
-e POSTGRES_PASSWORD=password \
--health-cmd 'pg_isready -U postgres' \
--health-interval 2s \
--health-timeout 5s \
--health-retries 10 \
-d pgvector/pgvector:pg17"
}
sh """
set -e
for i in \$(seq 1 ${numPartitions}); do
name="test-postgres-${buildSuffix}-p\$i"
echo "Waiting for \$name to be ready..."
for attempt in \$(seq 1 30); do
if docker exec "\$name" pg_isready -U postgres 2>/dev/null; then
echo "\$name is ready"
break
fi
if [ \$attempt -eq 30 ]; then
echo "ERROR: \$name did not become ready within 60 seconds"
docker logs "\$name" 2>&1 || true
exit 1
fi
sleep 2
done
done
"""
try {
def partitions = [:]
for (int i = 1; i <= numPartitions; i++) {
def partition = i
def dbHost = "test-db-p${partition}"
partitions["Partition ${partition}"] = {
sh "docker run --rm \
--network ${networkName} \
--env DATABASE_URL=postgres://postgres:password@${dbHost}:5432/postgres \
--env MIX_TEST_PARTITION=${partition} \
-v ${failuresDir}:/app/_build/test/failures \
-t ${imageTag} \
mix test --partitions ${numPartitions} \
--formatter Sanbase.FailedTestFormatter \
--formatter ExUnit.CLIFormatter \
--slowest 20"
}
}
timeout(time: 25, unit: 'MINUTES') {
parallel partitions
}
} finally {
sh """
for i in \$(seq 1 ${numPartitions}); do
docker rm -f test-postgres-${buildSuffix}-p\$i 2>/dev/null || true
done
docker network rm ${networkName} 2>/dev/null || true
"""
}
}
}
}
stage('Summarize Test Failures') {
container('docker') {
def buildSuffix = env.TEST_RUN_SUFFIX ?: "${env.GIT_COMMIT_FULL}-${env.BUILD_ID}"
def failuresDir = "${pwd()}/test_failures_${buildSuffix}"
sh """
set +x
echo ''
echo '========================================'
echo ' Combined results from all partitions'
echo '========================================'
if ls ${failuresDir}/partition_*.txt 1>/dev/null 2>&1; then
echo 'Failing tests across all partitions:'
cut -f2 ${failuresDir}/partition_*.txt | sort
echo ''
echo 'Re-run with:'
echo " mix test \$(cut -f2 ${failuresDir}/partition_*.txt | tr '\\n' ' ')"
rm -rf ${failuresDir}
exit 1
fi
echo 'All partitions passed!'
rm -rf ${failuresDir}
"""
}
}
if (env.BRANCH_NAME == 'master') {
stage('Build & Push') {
container('docker') {
withCredentials([
string(
credentialsId: 'SECRET_KEY_BASE',
variable: 'SECRET_KEY_BASE'
),
string(
credentialsId: 'PARITY_URL',
variable: 'PARITY_URL'
),
string(
credentialsId: 'aws_account_id',
variable: 'aws_account_id'
)
]) {
def awsRegistry = "${env.aws_account_id}.dkr.ecr.eu-central-1.amazonaws.com"
docker.withRegistry("https://${awsRegistry}", 'ecr:eu-central-1:ecr-credentials') {
sh "docker build \
-t ${awsRegistry}/sanbase:${env.BRANCH_NAME} \
-t ${awsRegistry}/sanbase:${env.GIT_COMMIT_FULL} \
--build-arg SECRET_KEY_BASE=${env.SECRET_KEY_BASE} \
--build-arg PARITY_URL=${env.PARITY_URL} \
--build-arg GIT_HEAD=${env.GIT_HEAD} \
--build-arg GIT_COMMIT=${env.GIT_COMMIT_FULL} . \
--progress plain"
sh "docker push ${awsRegistry}/sanbase:${env.BRANCH_NAME}"
sh "docker push ${awsRegistry}/sanbase:${env.GIT_COMMIT_FULL}"
}
}
}
}
}
}
}