oc new-build --name=app s2i-java11-maven~http://URL-TO-REPO
oc new-project app
#!/bin/bash
#Create app project
oc new-project app
#Create Build Configuration
oc new-build --name=app s2i-java11-maven~$APP_GIT_URL -n app
oc new-app --name=app-test --image-stream=app/app:test --allow-missing-imagestream-tags
oc tag app:latest app:test -n app
oc new-project app-test
oc policy add-role-to-user system:image-puller system:serviceaccount:app-test:default -n app
#!/bin/bash
#Create app-test project
oc new-project app-test
#Add permissions to pull image from app project
oc adm policy add-role-to-user system:image-puller \
system:serviceaccount:app-test:default -n app
#Create DeploymentConfiguration
oc new-app --name=app-test --image-stream=app/app:test --allow-missing-imagestream-tags
#Remove unnecessary Image Stream
oc delete is app-test -n app-test
#Add readiness probe
oc set probe dc/app-test --readiness --get-url=http://:8080/actuator/health -n app-test
oc expose dc/app-test
oc expose scv/app-test
#!/bin/bash
#Create project prod
oc new-project app-prod
#Add permissions to pull image from app project
oc policy add-role-to-user system:image-puller \
system:serviceaccount:app-prod:default -n app
#Create blue and green deployments
oc new-app --name=app-blue --image-stream=app/app:prod-blue \
--allow-missing-imagestream-tags -n app-prod
oc new-app --name=app-green --image-stream=app/app:prod-green \
--allow-missing-imagestream-tags -n app-prod
#Remove unnecessary Image Streams
oc delete is --all -n app-prod
#Expose prod applications
oc expose deploymentconfig.apps.openshift.io/app-blue --port=8080 -n app-prod
oc expose deploymentconfig.apps.openshift.io/app-green --port=8080 -n app-prod
oc expose service app-blue --hostname=blue.apps-crc.testing -n app-prod
oc expose service app-green --hostname=green.apps-crc.testing -n app-prod
#Add readiness probe
oc set probe dc/app-blue --readiness --get-url=http://:8080/actuator/health -n app-prod
oc set probe dc/app-green --readiness --get-url=http://:8080/actuator/health -n app-prod
oc expose svc/app-green
oc set route-backends app-green app-green=70 app-blue=30
#!/bin/bash
#Create production Route
oc expose service app-green --name prod \
--hostname=prod.apps-crc.testing -n app-prod
oc set route-backends prod app-green=100 app-blue=0 -n app-prod
oc start-build app -n app
oc tag app:latest app:test -n app
oc tag app:test app:prod-blue -n app
oc tag app:test app:prod-green -n app
oc set route-backends prod \
app-green=0 app-blue=100 -n app-prod
oc set route-backends prod \
app-green=100 app-blue=0 -n app-prod
oc new-app jenkins-persistent
oc get templates -n openshift | grep jenkins
jenkins-ephemeral Jenkins service, without persistent storage....
jenkins-persistent Jenkins service, with persistent storage....
kind: BuildConfig
apiVersion: v1
metadata:
name: app-pipeline
namespace: cicd
labels:
name: app-pipeline
spec:
strategy:
type: JenkinsPipeline
jenkinsPipelineStrategy:
jenkinsfile: |-
echo 'hello world!'
oc start-build app-pipeline -n cicd
oc apply -f app-pipeline.yaml -n cicd
stage('build') {
echo 'build'
}
stage('deploy') {
echo 'deploy'
}
oc start-build app-pipeline -n cicd
node {
stage('build') {
echo 'build'
}
stage('deploy') {
echo 'deploy'
}
}
oc start-build app-pipeline -n cicd
node('slave') {
stage('build') {
echo 'build'
}
stage('deploy') {
echo 'deploy'
}
}
node {
stage('build') {
sh 'oc start-build app -n app'
}
stage('deploy test') {
sh 'oc tag app:latest app:test -n app'
sh 'oc rollout latest app-test -n app-test'
}
}
oc start-build app-pipeline -n cicd
kind: BuildConfig
apiVersion: v1
metadata:
name: app-pipeline
namespace: cicd
labels:
name: app-pipeline
spec:
strategy:
type: JenkinsPipeline
jenkinsPipelineStrategy:
jenkinsfile: |-
node {
stage("build") {
.....
}
}
node {
stage("build") {
openshift.withCluster() {
openshift.withProject('app') {
def buildConfig = openshift.selector("bc", "app")
def build = buildConfig.startBuild("--incremental")
build.untilEach() {
return it.object().status.phase == "Complete"
}
}
}
}
}
node {
stage("build") {
openshiftBuild bldCfg: 'app', namespace: 'app'
}
}
node {
stage("build") {.....}
stage("deploy to test") {
openshift.withCluster() {
openshift.withProject('app-test') {
openshift.tag( 'app/app:latest', 'app/app:test')
def deployConfig = openshift.selector("dc", "app-test")
def replicasNumber = deployConfig.object().spec.replicas
deployConfig.rollout().latest()
def latestDeploymentVersion =
deployConfig.object().status.latestVersion
def replicationController =
openshift.selector('rc', "app-test-${latestDeploymentVersion}")
replicationController.untilEach() {
def replicationControllerObject = it.object()
return (replicasNumber.equals(
replicationControllerObject.status.readyReplicas))
}
}
}
}
}
node {
stage("build") {.....}
stage("deploy to test") {
openshiftDeploy depCfg: 'app-test', namespace: 'app-test'
}
}
node {
stage("build") {.....}
stage("deploy to test") {
openshift.withCluster() {
openshift.withProject('app-test') {
openshift.tag( 'app/app:latest', 'app/app:test')
deploy("app-test")
}
}
}
}
def deploy(String deploymentConfigurationName) {
def deployConfig = openshift.selector("dc", deploymentConfigurationName)
def replicasNumber = deployConfig.object().spec.replicas
deployConfig.rollout().latest()
def latestDeploymentVersion = deployConfig.object().status.latestVersion
def replicationController =
openshift.selector('rc', "${deploymentConfigurationName}-${latestDeploymentVersion}")
replicationController.untilEach() {
def replicationControllerObject = it.object()
return (replicasNumber.equals(
replicationControllerObject.status.readyReplicas))
}
}
node {
stage("build") {.....}
stage("deploy to test") {.....}
stage("manual testing") {
input message: 'Should deploy to production as blue?', ok: 'Yes'
}
}
def deploy(String deploymentConfigurationName) {.....}
node {
stage("build") {.....}
stage("deploy to test") {.....}
stage("manual testing") {.....}
stage("deploy blue on production") {
openshift.withCluster() {
openshift.withProject('app-prod') {
openshift.tag('app/app:latest', 'app/app:prod-blue')
deploy("app-blue")
}
}
}
}
def deploy(String deploymentConfigurationName) {.....}
node {
stage("build") {.....}
stage("deploy to test") {.....}
stage("manual testing") {.....}
stage("deploy blue on production") {.....}
stage("internal testing blue") {
input message: 'Should swich traffic to blue?', ok: 'Yes'
}
}
def deploy(String deploymentConfigurationName) {.....}
node {
stage("build") {.....}
stage("deploy to test") {.....}
stage("manual testing") {.....}
stage("deploy on production as blue") {.....}
stage("internal testing blue") {.....}
stage("swich traffic to blue") {
openshift.withCluster() {
openshift.withProject('app-prod') {
def routeObject = openshift.selector("route", "prod").object()
routeObject.spec.alternateBackends[0].weight=100
routeObject.spec.to.weight=0
openshift.apply(routeObject)
}
}
}
}
def deploy(String deploymentConfigurationName) {.....}
stage("swich traffic to blue") {
sh 'oc set route-backends prod app-green=0 app-blue=100 -n app-prod'
}
node {
stage("build") {.....}
stage("deploy to test") {.....}
stage("manual testing") {.....}
stage("deploy on production as blue") {.....}
stage("internal testing blue") {.....}
stage("swich traffic to blue") {
openshift.withCluster() {
openshift.withProject('app-prod') {
switchTraffic('prod', 0, 100)
}
}
}
def deploy(String deploymentConfigurationName) {.....}
def switchTraffic(String routeName, int greenWeight, int blueWeight) {
def routeObject = openshift.selector("route", routeName).object()
routeObject.spec.alternateBackends[0].weight=blueWeight
routeObject.spec.to.weight=greenWeight
openshift.apply(routeObject)
}
node {
stage("build") {.....}
stage("deploy to test") {.....}
stage("manual testing") {.....}
stage("deploy blue on production") {.....}
stage("internal testing blue") {.....}
stage("swich traffic to blue") {.....}
stage("testing blue on production") {
input message: 'Should deploy to production as blue?', ok: 'Yes'
}
}
def deploy(String deploymentConfigurationName) {.....}
def switchTraffic(String routeName, int greenWeight, int blueWeight) {....}
node {
stage("build") {.....}
stage("deploy to test") {.....}
stage("manual testing") {.....}
stage("deploy blue on production") {.....}
stage("internal testing blue") {.....}
stage("swich traffic to blue") {.....}
stage("testing blue on production") {.....}
stage("deploy green on production") {
openshift.withCluster() {
openshift.withProject('app-prod') {
openshift.tag('app/app:latest', 'app/app:prod-green')
deploy("app-green")
}
}
}
}
def deploy(String deploymentConfigurationName) {.....}
def switchTraffic(String routeName, int greenWeight, int blueWeight) {....}
node {
stage("build") {.....}
stage("deploy to test") {.....}
stage("manual testing") {.....}
stage("deploy blue on production") {.....}
stage("internal testing blue") {.....}
stage("swich traffic to blue") {.....}
stage("testing blue on production") {.....}
stage("deploy green on production") {.....}
stage("swich traffic to green") {
openshift.withCluster() {
openshift.withProject('app-prod') {
switchTraffic('app-green', 100, 0)
}
}
}
}
def deploy(String deploymentConfigurationName) {.....}
def switchTraffic(String routeName, int greenWeight, int blueWeight) {....}
#!/bin/bash
#Add permissions for jenkins
oc policy add-role-to-user edit system:serviceaccount:cicd:jenkins -n app
oc policy add-role-to-user edit system:serviceaccount:cicd:jenkins -n app-test
oc policy add-role-to-user edit system:serviceaccount:cicd:jenkins -n app-prod
#!/bin/bash
#Turn off the triggers for dc in project app-test
oc set triggers dc/app-test --from-config --remove -n app-test
oc patch dc/app-test \
-p '[{"op": "replace", "path": "/spec/triggers/0/imageChangeParams/automatic", "value":false}]' \
--type=json -n app-test
#Turn off the triggers for dc in project app-prod
oc set triggers dc/app-green --from-config --remove -n app-prod
oc set triggers dc/app-blue --from-config --remove -n app-prod
oc patch dc/app-green -p ....
oc patch dc/app-blue -p ....
oc apply -f app-pipeline.yaml