Skip to content

Commit 7a537b8

Browse files
committed
Add Jenkinsfile for quickstarter job creation with branch selection
1 parent 35f7bdc commit 7a537b8

8 files changed

Lines changed: 1240 additions & 6 deletions

File tree

tests/quickstarter/QUICKSTARTERS_TESTS.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Index
44
- [Overview](#overview)
55
- [Prerequisites](#prerequisites)
6+
- [Environment Setup](resources/README.md)
67
- [How to Run](#how-to-run)
78
- [Logging and Output](#logging-and-output)
89
- [Templates and Variables](#templates-and-variables)
@@ -57,12 +58,6 @@ A test is a sequence of steps such as:
5758
cd ods-core/tests
5859
make test-quickstarter QS=<quickstarter> PROJECT_NAME=<project>
5960
```
60-
- Go test directly:
61-
```bash
62-
cd ods-core/tests/quickstarter
63-
go test -v -run TestQuickstarter -timeout 30m \
64-
-args -quickstarter=<quickstarter> -project=<project> -testPhase=devtest
65-
```
6661

6762
## Logging and Output
6863

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
def dockerRegistry
2+
def ODS_PROJECT
3+
def odsNamespace
4+
def QUICK_STARTERS_URL
5+
def odsTag
6+
def JENKINS_URL
7+
def FOLDER_NAME
8+
def BITBUCKET_URL
9+
def CREDENTIALS_ID
10+
11+
properties([
12+
parameters([
13+
string( name: 'QUICK_STARTERS_URL',
14+
defaultValue: 'https://github.com/opendevstack/ods-quickstarters.git',
15+
description: 'URL of the quickstarters repository'),
16+
string(
17+
name: 'configFileId',
18+
defaultValue: 'quickstarter-test-config',
19+
description: 'ID of the managed config file containing environment configuration'
20+
)
21+
])
22+
])
23+
24+
node {
25+
dockerRegistry = env.DOCKER_REGISTRY
26+
27+
stage('Load Configuration') {
28+
echo "Loading configuration from managed file: quickstarter-test-config"
29+
def configContent
30+
configFileProvider([configFile(fileId: 'quickstarter-test-config', variable: 'CONFIG_FILE')]) {
31+
// Source the .env file to load variables
32+
configContent = readProperties(file: env.CONFIG_FILE)
33+
echo "✓ Configuration loaded successfully"
34+
}
35+
odsTag = '4.x'
36+
ODS_PROJECT = configContent.ODS_PROJECT
37+
ODS_NAMESPACE = configContent.ODS_NAMESPACE ?: 'ods'
38+
BITBUCKET_URL = configContent.BITBUCKET_URL
39+
CREDENTIALS_ID = configContent.CREDENTIALS_ID_PATTERN
40+
JENKINS_URL = configContent.JENKINS_URL
41+
ODS_QUICKSTARTERS_TESTS_BRANCH = configContent.ODS_QUICKSTARTERS_TESTS_BRANCH ?: 'master'
42+
QUICK_STARTERS_URL = params.QUICK_STARTERS_URL
43+
44+
// Extract repository name from URL
45+
def repoName = QUICK_STARTERS_URL.tokenize('/')[-1]
46+
FOLDER_NAME = repoName.replaceAll('\\.git$', '')
47+
48+
echo """\
49+
✓ Configuration loaded successfully
50+
Docker Registry: ${dockerRegistry}
51+
ODS Project: ${ODS_PROJECT}
52+
ODS Namespace: ${ODS_NAMESPACE}
53+
Bitbucket URL: ${BITBUCKET_URL}
54+
Jenkins URL: ${JENKINS_URL}
55+
Quickstarters Repo URL: ${QUICK_STARTERS_URL}
56+
ODS Core - Quickstarters Branch: ${ODS_QUICKSTARTERS_TESTS_BRANCH}
57+
FOLDER_NAME: ${FOLDER_NAME}
58+
""".stripIndent()
59+
}
60+
}
61+
62+
def conts = containerTemplate(
63+
name: 'jnlp',
64+
image: "${dockerRegistry}/${ODS_NAMESPACE}/jenkins-agent-base:${odsTag}",
65+
workingDir: '/tmp',
66+
)
67+
68+
def podLabel = "ods-qs-create-jobs-${UUID.randomUUID().toString()}"
69+
70+
podTemplate(
71+
cloud: 'openshift',
72+
label: podLabel,
73+
containers: [conts],
74+
volumes: [],
75+
serviceAccount: 'jenkins'
76+
) {
77+
node(podLabel) {
78+
stage('Init'){
79+
80+
git url: "${BITBUCKET_URL}/scm/${ODS_PROJECT}/ods-core.git",
81+
branch: "${ODS_QUICKSTARTERS_TESTS_BRANCH}",
82+
credentialsId: "${CREDENTIALS_ID}"
83+
84+
dir('tests/quickstarter/resources/scripts/repo'){
85+
git url: "$QUICK_STARTERS_URL",
86+
credentialsId: "${CREDENTIALS_ID}"
87+
88+
}
89+
}
90+
91+
stage('Select Branches') {
92+
script {
93+
def availableBranches = []
94+
dir('tests/quickstarter/resources/scripts/repo') {
95+
availableBranches = sh(
96+
script: 'git branch -r | grep -v HEAD | sed "s/.*origin\\///" | sort -u',
97+
returnStdout: true
98+
).trim().split('\n').toList()
99+
100+
echo "Available branches in ${QUICK_STARTERS_URL}:"
101+
availableBranches.each { branch ->
102+
echo " - ${branch}"
103+
}
104+
}
105+
106+
// Prompt for interactive branch selection
107+
// Create boolean parameters for each branch
108+
def branchParams = availableBranches.collect { branch ->
109+
booleanParam(
110+
name: branch.replaceAll('[^a-zA-Z0-9_]', '_'),
111+
description: "Process branch: ${branch}",
112+
defaultValue: false
113+
)
114+
}
115+
116+
def userInput = input(
117+
message: 'Select branches to process',
118+
parameters: branchParams
119+
)
120+
121+
// Collect selected branches
122+
def selectedBranches = []
123+
availableBranches.each { branch ->
124+
def paramName = branch.replaceAll('[^a-zA-Z0-9_]', '_')
125+
if (userInput instanceof Boolean) {
126+
// Single branch case
127+
if (userInput) {
128+
selectedBranches.add(branch)
129+
}
130+
} else {
131+
// Multiple branches case
132+
if (userInput[paramName]) {
133+
selectedBranches.add(branch)
134+
}
135+
}
136+
}
137+
138+
env.BRANCHES_TO_PROCESS = selectedBranches.join(' ')
139+
echo "Selected branches: ${env.BRANCHES_TO_PROCESS}"
140+
}
141+
}
142+
143+
stage('Create jobs') {
144+
sh(script: """
145+
cd tests/quickstarter/resources/scripts
146+
./create_jobs.sh \\
147+
--quickstarters-repository ${QUICK_STARTERS_URL} \\
148+
--jenkins-url ${JENKINS_URL} \\
149+
--project ${ODS_PROJECT} \\
150+
--ods-ref 4.x \\
151+
--branches "${BRANCHES_TO_PROCESS}" \\
152+
--folder-name "${FOLDER_NAME}" \\
153+
--bitbucket-url ${BITBUCKET_URL} \\
154+
--credentials-id ${CREDENTIALS_ID} \\
155+
--ods-quickstarters-test-branch ${ODS_QUICKSTARTERS_TESTS_BRANCH} \\
156+
--no-clone
157+
""")
158+
}
159+
}
160+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// List of QS that we want to exclude from the execution of the job.
2+
def excludedQS = params.excludedQuickstarters?:''
3+
4+
// Jenkins DeploymentConfig environment variables
5+
def dockerRegistry
6+
def credentialsId
7+
def sonarQualityProfile
8+
def sonarQualityGate
9+
10+
// Load configuration from managed config file
11+
node {
12+
stage('Load Configuration') {
13+
echo "Loading configuration from managed file: quickstarter-test-config"
14+
def configContent
15+
def configProps = [:]
16+
configFileProvider([configFile(fileId: 'quickstarter-test-config', variable: 'CONFIG_FILE')]) {
17+
// Source the .env file to load variables
18+
configContent = readProperties(file: env.CONFIG_FILE)
19+
echo "✓ Configuration loaded successfully"
20+
}
21+
22+
// Set variables from config and parameters
23+
dockerRegistry = env.DOCKER_REGISTRY
24+
project = params.project ?: env.ODS_PROJECT
25+
projectId = env.PROJECT_ID
26+
odsRef = params.quickstarterRef ?: env.ODS_GIT_REF
27+
odsNamespace = env.ODS_NAMESPACE
28+
odsGitRef = env.ODS_GIT_REF
29+
odsImageTag = env.ODS_IMAGE_TAG ?: '4.x'
30+
sharedLibraryRef = env.SHARED_LIBRARY_REF ?: odsImageTag
31+
agentImageTag = env.AGENT_IMAGE_TAG ?: odsImageTag
32+
odsMainBitbucketProject = env.ODS_BITBUCKET_PROJECT ?: 'ods'
33+
34+
// Construct repository URLs from config
35+
QUICK_STARTERS_URL = "${params.quickstartersRepositoryUrl}"
36+
QUICK_STARTERS_BRANCH = params.quickstarterRef ?: env.ODS_GIT_REF
37+
38+
ODS_CONFIGURATION_URL = "${configContent.BITBUCKET_URL}/scm/${project}/ods-configuration.git"
39+
ODS_CONFIGURATION_BRANCH = configContent.ODS_CONFIGURATION_BRANCH
40+
41+
ODS_CORE_URL = "${configContent.BITBUCKET_URL}/scm/${project}/ods-core.git"
42+
ODS_QUICKSTARTERS_TESTS_BRANCH = configContent.ODS_QUICKSTARTERS_TESTS_BRANCH ?: 'master'
43+
44+
// Credentials ID from pattern
45+
credentialsId = configContent.CREDENTIALS_ID_PATTERN
46+
47+
// List of QS that we want to exclude from the execution of the job
48+
excludedQS = params.excludedQuickstarters ?: ''
49+
50+
// Sonar config
51+
sonarQualityProfile = configContent.SONAR_QUALITY_PROFILE
52+
sonarQualityGate = configContent.SONAR_QUALITY_GATE
53+
54+
echo """\
55+
✓ Configuration loaded successfully
56+
Project: ${project}
57+
ODS Namespace: ${odsNamespace}
58+
Bitbucket URL: ${configContent.BITBUCKET_URL}
59+
Quickstarters Repo URL: ${QUICK_STARTERS_URL}
60+
Quickstarters Branch: ${ODS_QUICKSTARTERS_TESTS_BRANCH}
61+
ODS Core URL: ${ODS_CORE_URL}
62+
Docker Registry: ${dockerRegistry}
63+
Credentials ID: ${credentialsId}
64+
Sonar Quality Profile: ${sonarQualityProfile}
65+
Sonar Quality Gate: ${sonarQualityGate}
66+
""".stripIndent()
67+
} // End of stage
68+
} // End of node
69+
70+
def conts = containerTemplate(
71+
name: 'jnlp',
72+
image: "${dockerRegistry}/${odsNamespace}/jenkins-agent-golang:4.x",
73+
workingDir: '/tmp',
74+
alwaysPullImage: true,
75+
args: ''
76+
)
77+
78+
79+
def podLabel = "qs-tests"
80+
podTemplate(
81+
label: podLabel,
82+
cloud: 'openshift',
83+
containers: [conts],
84+
volumes: [],
85+
serviceAccount: 'jenkins'
86+
) {
87+
node(podLabel) {
88+
89+
stage('Init') {
90+
currentBuild.description = "Testing QS"
91+
echo "${WORKSPACE}"
92+
sh "ls -Al ${WORKSPACE}"
93+
sh "pwd"
94+
sh "ls -Al"
95+
96+
// Retrieve the quickstarter repository in the folder that corresponds to its name.
97+
def quickstartersDirName = "${QUICK_STARTERS_URL}".substring(QUICK_STARTERS_URL.lastIndexOf('/') + 1).replace('.git', '')
98+
dir(quickstartersDirName) {
99+
echo "Checking out Quickstarters repository from ${QUICK_STARTERS_URL} with branch ${QUICK_STARTERS_BRANCH}"
100+
git branch: "${QUICK_STARTERS_BRANCH}",
101+
credentialsId: "${project}-cd-cd-user-with-password",
102+
url: "${QUICK_STARTERS_URL}"
103+
}
104+
105+
// Retrive the configuration repository in the folder that corresponds to its name.
106+
dir('ods-configuration') {
107+
echo "Checking out ODS Configuration repository from ${ODS_CONFIGURATION_URL} with branch ${ODS_CONFIGURATION_BRANCH}"
108+
git branch: "${ODS_CONFIGURATION_BRANCH}",
109+
credentialsId: "${project}-cd-cd-user-with-password",
110+
url: "${ODS_CONFIGURATION_URL}"
111+
}
112+
113+
dir('ods-core') {
114+
echo "Checking out ODS Core repository from ${ODS_CORE_URL} with branch ${ODS_QUICKSTARTERS_TESTS_BRANCH}"
115+
git branch: "${ODS_QUICKSTARTERS_TESTS_BRANCH}",
116+
credentialsId: "${project}-cd-cd-user-with-password",
117+
url: "${ODS_CORE_URL}"
118+
119+
echo "Writing quickstarters exclusion list to ./tests/quickStartersExclusionList.txt"
120+
writeFile file: './tests/quickStartersExclusionList.txt', text: excludedQS
121+
}
122+
123+
echo "${WORKSPACE}"
124+
sh "ls -Al ${WORKSPACE}"
125+
sh "pwd"
126+
sh "ls -Al"
127+
128+
}
129+
130+
stage('updatecred') {
131+
withCredentials([
132+
usernamePassword(
133+
credentialsId: "${project}-cd-cd-user-with-password",
134+
passwordVariable: 'PASSWD',
135+
usernameVariable: 'USER')
136+
]) {
137+
sh (returnStdout: false, script: '''
138+
#!/bin/sh -e
139+
set +x
140+
pushd ods-configuration/
141+
user_id=$(echo -n "$USER" | base64)
142+
user_pwd=$(echo -n "$PASSWD" | base64)
143+
trigger_secret_base64=$(oc get secret webhook-proxy -o json | jq -r '.data."trigger-secret"')
144+
trigger_secret=$(echo $(oc get secret webhook-proxy -o json | jq -r '.data."trigger-secret"') | base64 --decode)
145+
sed -i~ "/^CD_USER_ID=/s/=.*/=$USER/" ods-core.env
146+
sed -i~ "/^CD_USER_ID_B64=/s/=.*/=$user_id/" ods-core.env
147+
sed -i~ "/^CD_USER_PWD_B64=/s/=.*/=$user_pwd/" ods-core.env
148+
sed -i~ "/^PIPELINE_TRIGGER_SECRET_B64=/s/=.*/=$trigger_secret_base64/" ods-core.env
149+
sed -i~ "/^PIPELINE_TRIGGER_SECRET=/s/=.*/=$trigger_secret/" ods-core.env
150+
sed -i~ "/^ODS_BITBUCKET_PROJECT=/s/=.*/=$project/" ods-core.env
151+
popd
152+
''')
153+
}
154+
}
155+
156+
stage('Test') {
157+
echo "${WORKSPACE}"
158+
159+
// If we select 'all' no one parameter will be provided, so it will try to test all the Quickstarters
160+
// that have a test defined in the testdata folder.
161+
def quickstarter_to_test = ""
162+
if(params.quickstarter && params.quickstarter != "all") {
163+
quickstarter_to_test = "-q ${quickstarter}"
164+
}
165+
166+
167+
// In different environments SONAR_QUALITY_PROFILE can be different, with this env variable we provide
168+
// the needed value, in BI this value is 'Sonar way'
169+
withEnv([
170+
"CGO_ENABLED=0",
171+
"GOCACHE=${WORKSPACE}/.cache",
172+
"GOMODCACHE=${WORKSPACE}/.cache",
173+
"SONAR_QUALITY_PROFILE=sonar BI way",
174+
"TMPL_SonarQualityGate=ODS Default Quality Gate",
175+
"ODS_GIT_REF=${odsRef}"]) {
176+
sh """
177+
cd ods-core/tests
178+
./quickstarter-test.sh -p ${project} ${quickstarter_to_test} || true
179+
"""
180+
}
181+
}
182+
183+
stage('Get test results') {
184+
archiveArtifacts artifacts: 'ods-core/tests/*.txt', followSymlinks: false
185+
archiveArtifacts artifacts: 'ods-core/tests/test-quickstarter-report.xml', followSymlinks: false
186+
junit(testResults:"ods-core/tests/*.xml", allowEmptyResults:true)
187+
}
188+
} // End of node
189+
}

0 commit comments

Comments
 (0)