Forked from rafaeltuelho/openshift-jenkinsfile-tasks-app-example.groovy
Created
July 17, 2020 07:37
-
-
Save linuxdevops-34/30738d69e52236aab03110e5cbc70c13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set your project Prefix using your GUID | |
def prefix = "user22" | |
// Routes | |
def clusterDomain = "apps.cluster-19ae.sandbox478.opentlc.com" | |
def tasksDevRoute = "tasks-${prefix}-tasks-dev.${clusterDomain}" | |
def ocpRegistryRoute = "default-route-openshift-image-registry.${clusterDomain}" | |
def nexusRegistryRoute = "nexus-registry-${prefix}-cicd.${clusterDomain}" | |
// Set variable globally to be available in all stages | |
// Set Maven command to always include Nexus Settings | |
def mvnCmd = "mvn -s ./nexus_openshift_settings.xml" | |
// Set Development and Production Project Names | |
def devProject = "${prefix}-tasks-dev" | |
def prodProject = "${prefix}-tasks-prod" | |
// Set the tag for the development image: version + build number | |
def devTag = "0.0-0" | |
// Set the tag for the production image: version | |
def prodTag = "0.0" | |
def otherDeploymentName = "tasks-green" | |
def activeServiceName = "" | |
pipeline { | |
agent { | |
// Using the Jenkins Agent Pod that we defined earlier | |
label "maven-appdev" | |
} | |
stages { | |
// Checkout Source Code and calculate Version Numbers and Tags | |
stage('Checkout Source') { | |
steps { | |
// TBD: Get code from protected Git repository | |
git credentialsId: 'gogs-private-repos-ok', url: 'http://gogs-user22-cicd.apps.cluster-19ae.sandbox478.opentlc.com/CICDLabs/openshift-tasks-private.git' | |
script { | |
def pom = readMavenPom file: 'pom.xml' | |
def version = pom.version | |
// Set the tag for the development image: version + build number | |
devTag = "${version}-" + currentBuild.number | |
// Set the tag for the production image: version | |
prodTag = "${version}" | |
} | |
} | |
} | |
// Using Maven build the war file | |
// Do not run tests in this step | |
stage('Build War File') { | |
steps { | |
echo "Building version ${devTag}" | |
sh "${mvnCmd} clean package -DskipTests" | |
} | |
} | |
// Using Maven run the unit tests | |
stage('Unit Tests') { | |
steps { | |
echo "Running Unit Tests" | |
sh "${mvnCmd} test" | |
//displays the results of tests in the Jenkins Task Overview | |
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml']) | |
} | |
} | |
//Using Maven call SonarQube for Code Analysis | |
stage('Code Analysis') { | |
steps { | |
echo "Running Code Analysis" | |
sh "${mvnCmd} sonar:sonar -Dsonar.host.url=http://sonarqube-${prefix}-cicd.${clusterDomain}/ -Dsonar.projectName=${JOB_BASE_NAME} -Dsonar.projectVersion=${devTag}" | |
} | |
} | |
// Publish the built war file to Nexus | |
stage('Publish to Nexus') { | |
steps { | |
echo "Publish to Nexus" | |
sh "${mvnCmd} deploy -DskipTests -DaltDeploymentRepository=nexus::default::http://nexus-${prefix}-cicd.${clusterDomain}/repository/releases" | |
} | |
} | |
// Build the OpenShift Image in OpenShift and tag it. | |
stage('Build and Tag OpenShift Image') { | |
steps { | |
echo "Building OpenShift container image tasks:${devTag}" | |
// TBD: Start binary build in OpenShift using the file we just published. | |
// Either use the file from your | |
// workspace (filename to pass into the binary build | |
// is openshift-tasks.war in the 'target' directory of | |
// your current Jenkins workspace). | |
// OR use the file you just published into Nexus: | |
// "--from-file=http://nexus3.${prefix}-nexus.svc.cluster.local:8081/repository/releases/org/jboss/quickstarts/eap/tasks/${prodTag}/tasks-${prodTag}.war" | |
// sh "oc whoami" | |
// sh "oc project ${devProject}" | |
// sh "oc start-build tasks --from-file=target/openshift-tasks.war --wait" | |
// sh "oc tag tasks:latest tasks:${devTag}" | |
script { | |
openshift.withCluster() { | |
openshift.withProject("${devProject}") { | |
openshift.selector("bc", "tasks").startBuild("--from-file=./target/openshift-tasks.war", "--wait=true") | |
openshift.tag("tasks:latest", "tasks:${devTag}") | |
} | |
} | |
} | |
} | |
} | |
// Deploy the built image to the Development Environment. | |
stage('Deploy to Dev') { | |
steps { | |
echo "Deploy container image to Development Project" | |
// TBD: Deploy the image | |
// 1. Update the image on the dev deployment config | |
// 2. Update the config maps with the potentially changed properties files | |
// 3. Reeploy the dev deployment | |
// 4. Wait until the deployment is running | |
// The following code will accomplish that by | |
// comparing the requested replicas | |
// (rc.spec.replicas) with the running replicas | |
// (rc.status.readyReplicas) | |
// | |
script { | |
openshift.withCluster() { | |
openshift.withProject("${devProject}") { | |
openshift.set( | |
"image", | |
"dc/tasks", | |
"tasks=image-registry.openshift-image-registry.svc:5000/${devProject}/tasks:${devTag}" | |
) | |
// Update the Config Map which contains the users for the Tasks application | |
// (just in case the properties files changed in the latest commit) | |
openshift.selector('configmap', 'tasks-config').delete() | |
def configmap = openshift.create( | |
'configmap', | |
'tasks-config', | |
'--from-file=./configuration/application-users.properties', | |
'--from-file=./configuration/application-roles.properties' | |
) | |
// Deploy the development application. | |
openshift.selector("dc", "tasks").rollout().latest(); | |
def dc = openshift.selector("dc", "tasks").object() | |
def dc_version = dc.status.latestVersion | |
def rc = openshift.selector("rc", "tasks-${dc_version}").object() | |
echo "Waiting for ReplicationController tasks-${dc_version} to be ready" | |
while (rc.spec.replicas != rc.status.readyReplicas) { | |
sleep 5 | |
rc = openshift.selector("rc", "tasks-${dc_version}").object() | |
} | |
} | |
} | |
} | |
} | |
} | |
// Run Integration Tests in the Development Environment. | |
stage('Integration Tests') { | |
steps { | |
echo "Running Integration Tests" | |
script { | |
def status = "000" | |
// Create a new task called "integration_test_1" | |
echo "Creating task" | |
status = sh(returnStdout: true, script: "curl -sw '%{response_code}' -o /dev/null -u 'tasks:redhat1' -H 'Content-Length: 0' -X POST http://${tasksDevRoute}/ws/tasks/integration_test_1").trim() | |
echo "Status: " + status | |
if (status != "201") { | |
error 'Integration Create Test Failed!' | |
} | |
sleep 3 | |
echo "Retrieving tasks" | |
status = sh(returnStdout: true, script: "curl -sw '%{response_code}' -o /dev/null -u 'tasks:redhat1' -H 'Accept: application/json' -X GET http://${tasksDevRoute}/ws/tasks/1").trim() | |
echo "Status: " + status | |
if (status != "200") { | |
error 'Integration Get Test Failed!' | |
} | |
echo "Deleting tasks" | |
status = sh(returnStdout: true, script: "curl -sw '%{response_code}' -o /dev/null -u 'tasks:redhat1' -X DELETE http://${tasksDevRoute}/ws/tasks/1").trim() | |
echo "Status: " + status | |
if (status != "204") { | |
error 'Integration Create Test Failed!' | |
} | |
} | |
} | |
} | |
// Copy Image to Nexus Docker Registry | |
stage('Copy Image to Nexus Docker Registry') { | |
steps { | |
script{ | |
echo "Copy image to Nexus Docker Registry" | |
// TBD. Use skopeo to copy | |
sh "skopeo --version" | |
sh "skopeo copy --src-tls-verify=false --dest-tls-verify=false --src-creds=openshift:\$(oc whoami -t) --dest-creds=admin:admin123 docker://${ocpRegistryRoute}/${devProject}/tasks:${devTag} docker://${nexusRegistryRoute}/tasks:${prodTag}" | |
// Tag the built image with the production tag. | |
openshift.withCluster() { | |
openshift.withProject("${prodProject}") { | |
openshift.tag("${devProject}/tasks:${devTag}", "${devProject}/tasks:${prodTag}") | |
} | |
} | |
} | |
} | |
} | |
// Blue/Green Deployment into Production | |
// ------------------------------------- | |
// Do not activate the new version yet. | |
stage('Blue/Green Production Deployment') { | |
steps { | |
echo "Blue/Green Deployment" | |
// TBD: 1. Determine which application is active | |
// 2. Update the image for the other application | |
// 3. Deploy into the other application | |
// 4. Update Config maps for other application | |
// 5. Wait until application is running | |
// See above for example code | |
script { | |
openshift.withCluster() { | |
openshift.withProject("${prodProject}") { | |
//determine which service is active | |
def tasksRoute = openshift.selector('route', 'tasks').object(); | |
activeServiceName = tasksRoute.spec.to.name; | |
echo ">>> tasks route is pointing to ${activeServiceName} service <<<" | |
otherDeploymentName = activeServiceName.contains('blue') ? 'tasks-green' : 'tasks-blue'; | |
echo "\n\t the other deployment is ${otherDeploymentName} !!!\n" | |
openshift.set( | |
"image", | |
"dc/${otherDeploymentName}", | |
"${otherDeploymentName}=${nexusRegistryRoute}/tasks:${prodTag}" | |
) | |
// Update the Config Map which contains the users for the Tasks application | |
// (just in case the properties files changed in the latest commit) | |
openshift.selector("configmap", "${otherDeploymentName}-config").delete() | |
def configmap = openshift.create( | |
"configmap", | |
"${otherDeploymentName}-config", | |
"--from-file=./configuration/application-users.properties", | |
"--from-file=./configuration/application-roles.properties" | |
) | |
// Deploy the other application. | |
openshift.selector("dc", "${otherDeploymentName}").rollout().latest(); | |
def dc = openshift.selector("dc", "${otherDeploymentName}").object() | |
def dc_version = dc.status.latestVersion | |
def rc = openshift.selector("rc", "${otherDeploymentName}-${dc_version}").object() | |
echo "Waiting for ReplicationController ${otherDeploymentName}-${dc_version} to be ready" | |
while (rc.spec.replicas != rc.status.readyReplicas) { | |
sleep 5 | |
rc = openshift.selector("rc", "${otherDeploymentName}-${dc_version}").object() | |
} | |
} | |
} | |
} | |
} | |
} | |
stage('Switch over to new Version') { | |
steps { | |
input "Switch Production?" | |
echo "Switching Production application to ${otherDeploymentName}." | |
script { | |
openshift.withCluster() { | |
openshift.withProject("${prodProject}") { | |
def route = openshift.selector("route/tasks").object() | |
route.spec.to.name="${otherDeploymentName}" | |
openshift.apply(route) | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment