在DevOps过程中CI/CD是最主要的工具,通过它的Pipeline可以驱动整个开发、测试、交付和部署过程。在OpenShift 4.x中自带两个CI/CD引擎:Jenkins和Tekton。其中Jenkins是目前最为常用的CI/CD工具,而Tekton是是直接运行在Kubernetes上的原生CI/CD。我们在本章节介绍如何部署运行一套Jenkins的环境,进而利用Pipeline将应用在开发、测试和生产环境中进行升迁部署。
$ export USERID=USER-ID
$ oc new-project ${USERID}-pipeline-jenkins
$ oc new-project ${USERID}-pipeline-dev
$ oc new-project ${USERID}-pipeline-test
$ oc new-project ${USERID}-pipeline-prod
$ oc new-app jenkins-ephemeral -n ${USERID}-pipeline-jenkins
$ oc get pod -w -n ${USERID}-pipeline-jenkins
$ oc get route jenkins -n ${USERID}-pipeline-jenkins -o template --template '{{.spec.host}}'
$ oc policy add-role-to-user edit system:serviceaccount:${USERID}-pipeline-jenkins:jenkins -n ${USERID}-pipeline-dev
$ oc policy add-role-to-user edit system:serviceaccount:${USERID}-pipeline-jenkins:jenkins -n ${USERID}-pipeline-test
$ oc policy add-role-to-user edit system:serviceaccount:${USERID}-pipeline-jenkins:jenkins -n ${USERID}-pipeline-prod
$ oc policy add-role-to-group system:image-puller system:serviceaccounts:${USERID}-pipeline-test -n ${USERID}-pipeline-dev
$ oc policy add-role-to-group system:image-puller system:serviceaccounts:${USERID}-pipeline-prod -n ${USERID}-pipeline-dev
$ oc new-app --name=myapp openshift/php:7.1~https://github.com/liuxiaoyu-git/cotd.git -n ${USERID}-pipeline-dev
$ oc get pod -n ${USERID}-pipeline-dev -w
NAME READY STATUS RESTARTS AGE
myapp-1-build 0/1 Completed 0 2m45s
myapp-1-deploy 0/1 Completed 0 36s
myapp-1-fgh9v 1/1 Running 0 27s
$ oc expose service myapp -n ${USERID}-pipeline-dev
$ oc get route myapp -n ${USERID}-pipeline-dev -o template --template '{{.spec.host}}'
$ oc get dc myapp -o yaml -n ${USERID}-pipeline-dev | sed 's/automatic: true/automatic: false/g' | oc replace -f -
deploymentconfig.apps.openshift.io/myapp replaced
$ curl -LO https://raw.githubusercontent.com/liuxiaoyu-git/OpenShift-HOL/master/jenkins-pipeline-demo1.yaml
sPipeline
def USERID = "USER-ID"
def devProj = "$USERID-pipeline-dev"
pipeline {
agent any
stages{
stage("Build") {
steps{
script{
openshift.withCluster() {
openshift.withProject("${devProj}") {
echo '*** Build Starting ***'
openshift.selector('bc', 'myapp').startBuild("--wait").logs('-f')
echo '*** Build Complete ***'
}
}
}
}
}
stage("Deploy and Verify in Development Env"){
steps{
script{
openshift.withCluster() {
openshift.withProject("${devProj}") {
echo '*** Deployment Starting ***'
openshift.selector('dc', 'myapp').rollout().latest()
echo '*** Deployment Complete ***'
}
}
}
}
}
}
}
$ oc create -f jenkins-pipeline-demo1.yaml -n ${USERID}-pipeline-jenkins
start-build jenkins-pipeline-dembuildconfig.build.openshift.io/jenkins-pipeline-demo created
$ oc start-build jenkins-pipeline-demo -n ${USERID}-pipeline-jenkins
build.build.openshift.io/jenkins-pipeline-demo-1 started
Version”并修改版本号,然后“Commit Changes”。
$ oc get all -n ${USERID}-pipeline-test
No resources found in pipeline-test namespace.
$ oc get all -n ${USERID}-pipeline-prod
No resources found in pipeline-prod namespace.
$ oc tag myapp:latest myapp:testready myapp:prodready -n ${USERID}-pipeline-dev
Tag myapp:testready set to myapp@sha256:566ee55311f0cd0a80291c2345e7a1b72ca3eb84455a032433e2e3d11f39efbb.
Tag myapp:prodready set to myapp@sha256:566ee55311f0cd0a80291c2345e7a1b72ca3eb84455a032433e2e3d11f39efbb.
$ oc get is -n ${USERID}-pipeline-dev
NAME IMAGE REPOSITORY TAGS UPDATED
myapp default-route-openshift-image-registry.apps.cluster.example.opentlc.com/pipeline-dev/myapp prodready,testready,latest 9 seconds ago
$ oc new-app ${USERID}-pipeline-dev/myapp:testready --name=myapp -n ${USERID}-pipeline-test
$ oc new-app ${USERID}-pipeline-dev/myapp:prodready --name=myapp -n ${USERID}-pipeline-prod
$ oc expose service myapp -n ${USERID}-pipeline-test
$ oc expose service myapp -n ${USERID}-pipeline-prod
$ oc get dc myapp -o yaml -n ${USERID}-pipeline-test | sed 's/automatic: true/automatic: false/g' | oc replace -f -
$ oc get dc myapp -o yaml -n ${USERID}-pipeline-prod | sed 's/automatic: true/automatic: false/g' | oc replace -f -
$ curl -LO https://raw.githubusercontent.com/liuxiaoyu-git/OpenShift-HOL/master/jenkins-pipeline-demo2.yaml
$ oc replace -f jenkins-pipeline-demo2.yaml -n ${USERID}-pipeline-jenkins
$ oc start-build jenkins-pipeline-demo -n ${USERID}-pipeline-jenkins
$ oc get route myapp -n ${USERID}-pipeline-dev -o template --template '{{.spec.host}}'
$ oc get route myapp -n ${USERID}-pipeline-test -o template --template '{{.spec.host}}'
$ oc get route myapp -n ${USERID}-pipeline-prod -o template --template '{{.spec.host}}'