Created
August 25, 2019 13:44
-
-
Save cvicens/d70725edab9b14f5c827e78c54236f3f to your computer and use it in GitHub Desktop.
This file contains 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
apiVersion: v1 | |
kind: BuildConfig | |
metadata: | |
name: inventory-spring-boot-maven-pipeline-complex | |
spec: | |
strategy: | |
jenkinsPipelineStrategy: | |
jenkinsfile: |- | |
// Don't forget to run the commands to create the dev project, and grant the needed roles to the service accounts | |
// $ oc create sa cicd-central-jenkins -n cicd-ext | |
// Cluster role permissions... | |
// $ oc adm policy add-cluster-role-to-user self-provisioner system:serviceaccount:cicd-ext:cicd-central-jenkins | |
// $ oc adm groups new cicd-central system:serviceaccount:cicd-ext:cicd-central-jenkins | |
// $ oc adm policy add-cluster-role-to-user edit system:serviceaccount:cicd-ext:cicd-central-jenkins | |
// $ oc adm policy add-cluster-role-to-user view system:serviceaccount:cicd-ext:cicd-central-jenkins | |
// Project by project permissions... | |
// $ oc policy add-role-to-user edit system:serviceaccount:INTEGRATION_PROJECT_NAME:jenkins -n PROD_PROJECT_NAME | |
// $ oc policy add-role-to-user view system:serviceaccount:INTEGRATION_PROJECT_NAME:jenkins -n PROD_PROJECT_NAME | |
// $ oc policy add-role-to-user system:image-puller system:serviceaccount:PROD_PROJECT_NAME:default -n INTEGRATION_PROJECT_NAME | |
// TODO: change needed | |
def CLUSTER_NAME = "openshift-swedbank" | |
// TODO: change needed | |
def APP_BASE = "apps.cluster-swedbank-21eb.swedbank-21eb.openshiftworkshop.com" | |
def APP_NAME = "inventory" | |
def APP_VERSION = "0.0.1-SNAPSHOT" | |
// TODO: change needed | |
def INTEGRATION_PROJECT_NAME = "coolstore-integration" | |
def PROD_PROJECT_NAME = "coolstore" | |
def LOG_CONFIG_MAP = APP_NAME + "-log-cm" | |
// TODO: change needed | |
def GIT_URL = "https://github.com/redhat-developer-adoption-emea/cloud-native-labs.git" | |
def GIT_REF = "ocp-3.10" | |
def CONTEXT_DIR = "inventory-spring-boot-gradle-openapi/inventory" | |
def BUILD_NAME = APP_NAME | |
def BUILD_IMAGE_STREAM = "openshift/redhat-openjdk18-openshift:1.4" | |
pipeline { | |
agent { | |
label 'maven' | |
} | |
stages { | |
stage('Create Integration Project') { | |
when { | |
expression { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
return !openshift.selector("project", "${INTEGRATION_PROJECT_NAME}").exists(); | |
} | |
} | |
} | |
steps { | |
script { | |
try { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.newProject("${INTEGRATION_PROJECT_NAME}") | |
} | |
} catch ( e ) { | |
// The exception is a hudson.AbortException with details about the failure. | |
"Error encountered: ${e}" | |
} | |
} | |
} | |
} | |
stage('Create Production Project') { | |
when { | |
expression { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
return !openshift.selector("ns", "${PROD_PROJECT_NAME}").exists(); | |
} | |
} | |
} | |
steps { | |
script { | |
try { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.newProject("${PROD_PROJECT_NAME}") | |
def result = openshift.raw("policy add-role-to-user system:image-puller system:serviceaccount:${PROD_PROJECT_NAME}:default -n ${INTEGRATION_PROJECT_NAME}") | |
echo "Adding system:puller role to service account: ${result.out}" | |
} | |
} catch ( e ) { | |
// The exception is a hudson.AbortException with details about the failure. | |
"Error encountered: ${e}" | |
} | |
} | |
} | |
} | |
stage('Prepare') { | |
when { | |
expression { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.withProject("${INTEGRATION_PROJECT_NAME}") { | |
return !openshift.selector("cm", "${LOG_CONFIG_MAP}").exists(); | |
} | |
} | |
} | |
} | |
steps { | |
script { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.withProject("${INTEGRATION_PROJECT_NAME}") { | |
def log_cm_def = [ | |
"kind": "ConfigMap", | |
"apiVersion": "v1", | |
"metadata": [ | |
"name": "${LOG_CONFIG_MAP}", | |
"labels": [ | |
'from':'cicd-external' | |
] | |
], | |
"data": [ | |
"INTEGRATION_PROJECT_NAME": "${INTEGRATION_PROJECT_NAME}", | |
"PROD_PROJECT_NAME": "${PROD_PROJECT_NAME}" | |
] | |
] | |
def log_cm = openshift.create(log_cm_def, '--save-config', '--validate') | |
log_cm.describe() | |
} | |
} | |
} | |
} | |
} | |
stage('Checkout') { | |
steps { | |
git url: "${GIT_URL}", branch: "${GIT_REF}" | |
} | |
} | |
stage('Build') { | |
steps { | |
dir("${CONTEXT_DIR}") { | |
sh "java -version" | |
sh "./gradlew build --no-daemon -x test" | |
} | |
} | |
} | |
stage('Test') { | |
steps { | |
dir("${CONTEXT_DIR}") { | |
sh "./gradlew test --no-daemon" | |
} | |
} | |
} | |
stage('Sonar') { | |
steps { | |
script { | |
dir("${CONTEXT_DIR}") { | |
echo "./gradlew sonarqube -Dsonar.host.url=SONAR_HOST -Dsonar.login=SONAR_TOKEN --no-daemon" | |
} | |
} | |
} | |
} | |
stage('Nexus') { | |
steps { | |
script { | |
dir("${CONTEXT_DIR}") { | |
//APP_VERSION = sh (script: './gradlew -q getVersion --no-daemon', returnStdout: true).trim() | |
echo "curl -v -u NEXUS_USERNAME:NEXUS_PASSWORD --upload-file ./build/libs/${APP_NAME}-${APP_VERSION}.jar NEXUS_HOST/repository/maven-snapshots/NEXUS_PATH/${APP_VERSION}/${APP_NAME}-${APP_VERSION}.jar" | |
} | |
} | |
} | |
} | |
stage('Create Image Builder') { | |
when { | |
expression { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.withProject("${INTEGRATION_PROJECT_NAME}") { | |
return !openshift.selector("bc", "${BUILD_NAME}").exists(); | |
} | |
} | |
} | |
} | |
steps { | |
script { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.withProject("${INTEGRATION_PROJECT_NAME}") { | |
openshift.newBuild("--name=${BUILD_NAME}", "--image-stream=${BUILD_IMAGE_STREAM}", "--binary") | |
} | |
} | |
} | |
} | |
} | |
stage('Build Image') { | |
steps { | |
script { | |
dir("${CONTEXT_DIR}") { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.withProject("${INTEGRATION_PROJECT_NAME}") { | |
openshift.selector("bc", "${BUILD_NAME}").startBuild("--from-file=./build/libs/${APP_NAME}-${APP_VERSION}.jar", "--wait") | |
} | |
} | |
} | |
} | |
} | |
} | |
stage('Approve') { | |
steps { | |
timeout(time:15, unit:'MINUTES') { | |
input message:'Approve Deploy to Prod?' | |
} | |
} | |
} | |
stage('Promote to PROD') { | |
steps { | |
script { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.withProject("${INTEGRATION_PROJECT_NAME}") { | |
openshift.tag("${BUILD_NAME}:latest", "${BUILD_NAME}:prod") | |
} | |
} | |
} | |
} | |
} | |
stage('Create PROD') { | |
when { | |
expression { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.withProject("${PROD_PROJECT_NAME}") { | |
return !openshift.selector('dc', "${APP_NAME}").exists() | |
} | |
} | |
} | |
} | |
steps { | |
script { | |
openshift.withCluster("${CLUSTER_NAME}") { | |
openshift.withProject("${PROD_PROJECT_NAME}") { | |
openshift.newApp("${INTEGRATION_PROJECT_NAME}/${BUILD_NAME}:prod", "--name=${APP_NAME}").narrow('svc').expose() | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
type: JenkinsPipeline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment