Skip to content

Commit

Permalink
Future proof pipeline scripts, fixes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
pabrahamsson committed Dec 13, 2018
1 parent c42bd51 commit 4cd53a6
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 193 deletions.
163 changes: 89 additions & 74 deletions basic-tomcat/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,95 +1,110 @@
#!/usr/bin/groovy

////
// This pipeline requires the following plugins:
// Kubernetes Plugin 0.10
////

String ocpApiServer = env.OCP_API_SERVER ? "${env.OCP_API_SERVER}" : "https://openshift.default.svc.cluster.local"

node('master') {

env.NAMESPACE = readFile('/var/run/secrets/kubernetes.io/serviceaccount/namespace').trim()
env.TOKEN = readFile('/var/run/secrets/kubernetes.io/serviceaccount/token').trim()
env.OC_CMD = "oc --token=${env.TOKEN} --server=${ocpApiServer} --certificate-authority=/run/secrets/kubernetes.io/serviceaccount/ca.crt --namespace=${env.NAMESPACE}"

env.APP_NAME = "${env.JOB_NAME}".replaceAll(/-?pipeline-?/, '').replaceAll(/-?${env.NAMESPACE}-?\/?/, '')
println "Starting Pipeline for Application: ${APP_NAME}"
println "${env.JOB_NAME}, ${env.NAMESPACE}"
def projectBase = "${env.NAMESPACE}".replaceAll(/-build/, '')
env.STAGE0 = "${projectBase}-build"
env.STAGE1 = "${projectBase}-dev"
env.STAGE2 = "${projectBase}-stage"
env.STAGE3 = "${projectBase}-prod"
library identifier: "pipeline-library@master",
retriever: modernSCM(
[
$class: "GitSCMSource",
remote: "https://github.com/redhat-cop/pipeline-library.git"
]
)

openshift.withCluster() {
env.POM_FILE = env.BUILD_CONTEXT_DIR ? "${env.BUILD_CONTEXT_DIR}/pom.xml" : "pom.xml"
env.TARGET = env.BUILD_CONTEXT_DIR ? "${env.BUILD_CONTEXT_DIR}/target" : "target"
env.APP_NAME = "${JOB_NAME}".replaceAll(/-build.*/, '')
env.BUILD = openshift.project()
env.DEV = "${APP_NAME}-dev"
env.STAGE = "${APP_NAME}-stage"
env.PROD = "${APP_NAME}-prod"
echo "Starting Pipeline for ${APP_NAME}..."
}

node('maven') {

stage('SCM Checkout') {
git url: "${APPLICATION_SOURCE_REPO}", branch: "${APPLICATION_SOURCE_REF}"
}

stage('Build') {

String pomFileLocation = env.BUILD_CONTEXT_DIR ? "${env.BUILD_CONTEXT_DIR}/pom.xml" : "pom.xml"
sh "mvn clean install -DskipTests=true -f ${pomFileLocation}"

pipeline {
agent {
label 'maven'
}

stage('Build Image') {

String target = env.BUILD_CONTEXT_DIR ? "${env.BUILD_CONTEXT_DIR}/target" : "target"

sh """
ls ${target}/*
rm -rf oc-build && mkdir -p oc-build/deployments
for t in \$(echo "jar;war;ear" | tr ";" "\\n"); do
cp -rfv ./${target}/*.\$t oc-build/deployments/ 2> /dev/null || echo "No \$t files"
done
"""
openshift.withCluster() {
openshift.withProject("${STAGE0}") {
openshift.selector("bc", "${APP_NAME}").startBuild("--from-dir=oc-build").logs("-f")
stages {
stage('Git Checkout') {
steps {
git url: "${APPLICATION_SOURCE_REPO}", branch: "${APPLICATION_SOURCE_REF}"
}
}
}

stage ("Promote from Build to Dev") {
openshift.withCluster() {
openshift.tag("${env.STAGE0}/${env.APP_NAME}:latest", "${env.STAGE1}/${env.APP_NAME}:latest")
stage('Build') {
steps {
sh "mvn clean install -DskipTests=true -f ${POM_FILE}"
}
}
}

stage("Verify Deployment to ${env.STAGE1}") {

openshiftVerifyDeployment(deploymentConfig: "${env.APP_NAME}", namespace: "${STAGE1}", verifyReplicaCount: true)
stage('Unit Test') {
steps {
sh "mvn test -f ${POM_FILE}"
}
}

input "Promote Application to Stage?"
}
stage('Build Container Image') {
steps {
sh """
ls ${TARGET}/*
rm -rf oc-build && mkdir -p oc-build/deployments
for t in \$(echo "jar;war;ear" | tr ";" "\\n"); do
cp -rfv ./${TARGET}/*.\$t oc-build/deployments/ 2> /dev/null || echo "No \$t files"
done
"""
binaryBuild(projectName: env.BUILD, buildConfigName: env.APP_NAME, artifactsDirectoryName: "oc-build")
}
}

stage("Promote To ${env.STAGE2}") {
sh """
${env.OC_CMD} tag ${env.STAGE1}/${env.APP_NAME}:latest ${env.STAGE2}/${env.APP_NAME}:latest
"""
}
stage('Promote from Build to Dev') {
steps {
tagImage(sourceImageName: env.APP_NAME, sourceImagePath: env.BUILD, toImagePath: env.DEV)
}
}

stage("Verify Deployment to ${env.STAGE2}") {
stage('Verify Deployment to Dev') {
steps {
verifyDeployment(projectName: env.DEV, targetApp: env.APP_NAME)
}
}

openshiftVerifyDeployment(deploymentConfig: "${env.APP_NAME}", namespace: "${STAGE2}", verifyReplicaCount: true)
stage('Promotion gate (Stage)') {
steps {
script {
input message: 'Promote Application to Stage?'
}
}
}

input "Promote Application to Prod?"
}
stage('Promote from Dev to Stage') {
steps {
tagImage(sourceImageName: env.APP_NAME, sourceImagePath: env.DEV, toImagePath: env.STAGE)
}
}

stage("Promote To ${env.STAGE3}") {
sh """
${env.OC_CMD} tag ${env.STAGE2}/${env.APP_NAME}:latest ${env.STAGE3}/${env.APP_NAME}:latest
"""
}
stage('Verify Deployment to Stage') {
steps {
verifyDeployment(projectName: env.STAGE, targetApp: env.APP_NAME)
}
}

stage("Verify Deployment to ${env.STAGE3}") {
stage('Promotion gate (Prod)') {
steps {
script {
input message: 'Promote Application to Prod?'
}
}
}

openshiftVerifyDeployment(deploymentConfig: "${env.APP_NAME}", namespace: "${STAGE3}", verifyReplicaCount: true)
stage('Promote from Stage to Prod') {
steps {
tagImage(sourceImageName: env.APP_NAME, sourceImagePath: env.STAGE, toImagePath: env.PROD)
}
}

stage('Verify Deployment to Prod') {
steps {
verifyDeployment(projectName: env.PROD, targetApp: env.APP_NAME)
}
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions blue-green-spring/.openshift/builds/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ objects:
env:
- name: "APPLICATION_SOURCE_REPO"
value: "${APPLICATION_SOURCE_REPO}"
- name: "APPLICATION_SOURCE_REF"
value: "${APPLICATION_SOURCE_REF}"
- apiVersion: v1
kind: BuildConfig
metadata:
Expand Down Expand Up @@ -90,6 +92,10 @@ parameters:
name: APPLICATION_SOURCE_REPO
required: true
value: https://github.com/redhat-cop/spring-rest.git
- description: Source code ref for demo app
name: APPLICATION_SOURCE_REF
required: true
value: master
- description: GitHub trigger secret
from: '[a-zA-Z0-9]{8}'
generate: expression
Expand Down
Loading

0 comments on commit 4cd53a6

Please sign in to comment.