Skip to content

Instantly share code, notes, and snippets.

@blues-man
Created September 2, 2020 23:27
Show Gist options
  • Save blues-man/6276dcfa7434bb0d0bf0742fa4156209 to your computer and use it in GitHub Desktop.
Save blues-man/6276dcfa7434bb0d0bf0742fa4156209 to your computer and use it in GitHub Desktop.
OpenShift BuildConfig Jenkins Pipeline for Spring boot Petclinic with image promotion
apiVersion: v1
kind: BuildConfig
metadata:
annotations:
pipeline.alpha.openshift.io/uses: '[{"name": "jenkins", "namespace": "", "kind": "DeploymentConfig"}]'
labels:
app: petclinic
name: petclinic
name: petclinic-pipeline
spec:
runPolicy: Serial
source:
type: None
strategy:
jenkinsPipelineStrategy:
env:
- name: DEV_PROJECT
value: 'petclinic-dev'
- name: PROD_PROJECT
value: 'petclinic-prod'
jenkinsfile: |-
pipeline {
agent {
label 'maven'
}
stages {
stage('Build App') {
steps {
git branch: 'main', url: 'https://github.com/spring-projects/spring-petclinic.git'
sh "mvn install -DskipTests=true"
}
}
stage('Test') {
steps {
sh "mvn test"
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
stage('Create Builder'){
when {
expression {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
return !openshift.selector("bc","petclinic").exists();
}
}
}
}
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.newBuild("--name=petclinic", "--image-stream=openshift/redhat-openjdk18-openshift:1.8", "--binary")
}
}
}
}
}
stage('Build Image') {
steps {
sh "cp target/*.jar target/petclinic.jar"
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.selector("bc", "petclinic").startBuild("--from-file=target/petclinic.jar", "--wait=true")
}
}
}
}
}
stage('Create DeploymentConfig for DEV'){
when {
expression {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
return !openshift.selector("deploymentconfig","petclinic").exists();
}
}
}
}
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
def app = openshift.newApp("petclinic:latest", "--as-deployment-config=true")
app.narrow("svc").expose();
def dc = openshift.selector("dc", "petclinic")
while (dc.object().spec.replicas != dc.object().status.availableReplicas) {
sleep 10
}
openshift.set("triggers", "dc/petclinic", "--manual")
}
}
}
}
}
stage('Deploy DEV') {
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.selector("deploymentconfig", "petclinic").rollout().latest();
}
}
}
}
}
stage('Promote to PROD?') {
steps {
timeout(time:19, unit:'MINUTES') {
input message: "Promote to PROD?", ok: "Promote"
}
script {
openshift.withCluster() {
openshift.tag("${env.DEV_PROJECT}/petclinic:latest", "${env.PROD_PROJECT}/petclinic:prod")
}
}
}
}
stage('Create DeploymentConfig for PROD'){
when {
expression {
openshift.withCluster() {
openshift.withProject(env.PROD_PROJECT) {
return !openshift.selector("deploymentconfig","petclinic").exists();
}
}
}
}
steps {
script {
openshift.withCluster() {
openshift.withProject(env.PROD_PROJECT) {
def app = openshift.newApp("petclinic:prod", "--as-deployment-config=true")
app.narrow("svc").expose();
def dc = openshift.selector("dc", "petclinic")
while (dc.object().spec.replicas != dc.object().status.availableReplicas) {
sleep 10
}
openshift.set("triggers", "dc/petclinic", "--manual")
}
}
}
}
}
stage('Deploy PROD') {
steps {
script {
openshift.withCluster() {
openshift.withProject(env.PROD_PROJECT) {
openshift.selector("deploymentconfig", "petclinic").rollout().latest();
}
}
}
}
}
}
}
type: JenkinsPipeline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment