Skip to content

Instantly share code, notes, and snippets.

@heristop
Last active February 13, 2023 11:16
Show Gist options
  • Save heristop/7a9f449ce03fd0086d082bff850055c5 to your computer and use it in GitHub Desktop.
Save heristop/7a9f449ce03fd0086d082bff850055c5 to your computer and use it in GitHub Desktop.
OpenShift Gitflow sample with Jenkins pipeline
pipeline {
agent any
options {
buildDiscarder(logRotator(daysToKeepStr: '4'))
}
stages {
stage('Configure env') {
steps {
script {
NAMESPACE = "${env.PROJECT}-dev"
DC = "${env.PROJECT}-dev"
IMAGE_TST = "tst"
IMAGE_ENV = "dev"
if ("RCT" == "${ENVIRONMENT}") {
DC = "${env.PROJECT}-rct"
IMAGE_ENV = "rct"
}
if ("VAL" == "${ENVIRONMENT}") {
NAMESPACE = "${env.PROJECT}-val"
DC = "${env.PROJECT}-val"
IMAGE_TST = "tstmast"
IMAGE_ENV = "val"
}
}
}
}
stage('Build') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${NAMESPACE}") {
// Patch BC
def jsonBranchPatch = "{\"spec\":{\"source\":{\"git\":{\"ref\":\"${BRANCH}\"}}}}}"
echo "Patch: ${jsonBranchPatch}"
def patchBranch = openshift.raw("patch bc/${env.PROJECT}-gitflow --patch='$jsonBranchPatch'")
echo "Result: ${patchBranch.out}"
def jsonImagePatch = "{\"spec\":{\"strategy\":{\"sourceStrategy\":{\"from\":{\"name\":\"${DOCKER_IMAGE}\"}}}}}}"
echo "Patch: ${jsonImagePatch}"
def patchImage = openshift.raw("patch bc/${env.PROJECT}-gitflow --patch='$jsonImagePatch'")
echo "Result: ${patchImage.out}"
openshift.startBuild("${env.PROJECT}-gitflow --wait").logs('-f')
}
}
}
}
}
stage('Update tst configuration') {
steps {
script {
git url: 'https://my-app.git', branch: "${BRANCH}", credentialsId: 'my-app-credentials-id'
}
script {
openshift.withCluster() {
openshift.withProject("${NAMESPACE}") {
openshift.apply("-f openshift/configMaps/${IMAGE_TST}-config.yaml")
}
}
}
}
}
stage('Deploy Image to tst') {
steps {
script {
openshift.withCluster() {
openshift.tag("${NAMESPACE}/${env.PROJECT}:gitflow", "${NAMESPACE}/${env.PROJECT}:${IMAGE_TST}")
}
}
}
}
stage('Check availability of tst') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${NAMESPACE}") {
timeout(time: 10, unit: 'MINUTES') {
if ("true" == "${SKIP_TESTS}") {
return
}
// Scale up DC test
def dc = openshift.selector('dc', "${env.PROJECT}-${IMAGE_TST}")
dc.scale("--replicas=1")
// Scale up BDD test
def bdd = openshift.selector('dc', "postgresql10-${IMAGE_TST}")
bdd.scale("--replicas=1")
def latestDeploymentVersion = dc.object().status.latestVersion
def rc = openshift.selector('rc', "${env.PROJECT}-${IMAGE_TST}-${latestDeploymentVersion}")
rc.untilEach(1){
def rcMap = it.object()
return (rcMap.status.replicas.equals(rcMap.status.readyReplicas))
}
}
}
}
}
}
}
stage('Run tests in tst pod') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${NAMESPACE}") {
if ("true" == "${SKIP_TESTS}") {
return
}
def podSelector = openshift.selector("pods", [ deploymentconfig: "${env.PROJECT}-${IMAGE_TST}"])
def pod = openshift.selector("${podSelector.name()}").object()
def resultTest = openshift.exec("${pod.metadata.name}", "sh /project-path/bin/run-tests.sh")
echo "${resultTest.out}"
// Scale down DC test
def dc = openshift.selector('dc', "${env.PROJECT}-${IMAGE_TST}")
dc.scale("--replicas=0")
// Scale down BDD test
def bdd = openshift.selector('dc', "postgresql10-${IMAGE_TST}")
bdd.scale("--replicas=0")
}
}
}
}
}
stage('Update configuration') {
steps {
script {
git url: 'https://my-app.git', branch: "${BRANCH}", credentialsId: 'my-app-credentials-id'
}
script {
openshift.withCluster() {
openshift.withProject("${NAMESPACE}") {
openshift.apply("-f openshift/configMaps/${ENVIRONMENT.toLowerCase()}-config.yaml")
}
}
}
}
}
stage('Deploy Image to env') {
steps {
script {
openshift.withCluster() {
openshift.tag("${NAMESPACE}/${env.PROJECT}:${IMAGE_TST}", "${NAMESPACE}/${env.PROJECT}:${IMAGE_ENV}")
}
}
}
}
stage('Check availability of env') {
steps {
script {
openshift.withCluster() {
openshift.withProject("${NAMESPACE}") {
def dc = openshift.selector('dc', "${DC}")
// this will wait until the desired replicas are available
dc.rollout().status()
}
}
}
}
}
}
post {
always {
deleteDir()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment