|
| 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