欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

構(gòu)建及部署jenkins?pipeline實(shí)現(xiàn)持續(xù)集成持續(xù)交付腳本

 更新時(shí)間:2022年03月01日 15:24:54   作者:kl  
這篇文章主要為大家介紹了構(gòu)建及部署jenkins?pipeline實(shí)現(xiàn)持續(xù)集成持續(xù)交付腳本,喲需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步

前言

之前的文章中  jenkins插件Pipeline腳本jenkinsfile操作指南

已經(jīng)全面介紹過jenkins pipeline的特點(diǎn)及用途,以及實(shí)操了一把,將我們的構(gòu)建產(chǎn)物jar包丟到了目標(biāo)主機(jī)。這篇是接著上篇的實(shí)操,實(shí)現(xiàn)構(gòu)建即部署的腳本實(shí)現(xiàn)。會在之前的git clone(拉源碼),maven build(構(gòu)建),deploy jar(上傳jia包)的基礎(chǔ)上,在新增兩個(gè)步驟start app(啟動服務(wù)),check health(檢查應(yīng)用健康),真正實(shí)現(xiàn)持續(xù)交付,持續(xù)集成。

新增的步驟腳本

stage('start app') {
            steps {
                script {
                    if ('production' == "${profile}") {
                        sshagent(credentials: ['deploy_token']) {
                            sh 'ssh it@192.1xx.0.96 "sh /home/deploy/start.sh > /dev/null 2>&1 &"'
                            echo "xx系統(tǒng)192.1xx.0.96啟動完成"
                        }
                    }
                    if ('uat' == "${profile}") {
                        echo 'xx系統(tǒng)啟動完成'
                    }
                }
            }
        }
        stage('check health') {
            steps {
                script {
                    def healthUrl = null
                    if ('production' == "${profile}") {
                        healthUrl = "https://api.xx.cn:8016/health"
                    }
                    if ('uat' == "${profile}") {
                        healthUrl = ""
                    }
                    echo "睡眠兩分鐘,待應(yīng)用完全準(zhǔn)備好"
                    Thread.sleep((long) 1000 * 60 * 2)//睡眠兩分鐘
                    def shellStr = sh(script: "curl ${healthUrl}", returnStdout: true)
                    def map = null
                    try {
                        echo "應(yīng)用健康檢查結(jié)果:${shellStr}"
                        map = new JsonSlurper().parseText(shellStr)
                    } catch (Exception e) {
                    }
                    if (map != null && "UP" == map.get("status")) {
                        echo "應(yīng)用健康運(yùn)行"
                    } else {
                        Thread.sleep((long) 1000 * 60 * 1)//睡眠1分鐘
                        shellStr = sh(script: "curl ${healthUrl}", returnStdout: true)
                        map = new JsonSlurper().parseText(shellStr)
                        if (map == null || "UP" != map.get("status")) {
                            throw new RuntimeException("應(yīng)用不穩(wěn)定,請檢查服務(wù)是否正常")
                        } else {
                            echo "應(yīng)用健康運(yùn)行"
                        }
                    }
                }
            }
        }

需要注意的點(diǎn):

關(guān)于執(zhí)行啟動應(yīng)用腳本

  • 部署的腳本需要先在目標(biāo)主機(jī)寫好,一般的如果應(yīng)用是部署在tomcat下的話,直接執(zhí)行關(guān)閉腳本,然后執(zhí)行啟動腳本就好了。
  • 不過現(xiàn)在都是微服務(wù),spring boot這種應(yīng)用直接打成了jar了,需要使用nohup這種方式使進(jìn)程后臺運(yùn)行,如:nohup java -jar /home/xx-app.jar &。在jenkins中直接調(diào)用這種腳本的時(shí)候要注意。使用> /dev/null 2>&1 &將遠(yuǎn)程主機(jī)響應(yīng)重定向下,不然jenkins進(jìn)程會一直等待目標(biāo)主機(jī)的啟動腳本進(jìn)程內(nèi)容輸出。

關(guān)于健康檢查

執(zhí)行啟動應(yīng)用的腳本后,并不知道應(yīng)用是否真正的啟動起來了。這個(gè)時(shí)候需要一個(gè)健康檢查機(jī)制檢查下應(yīng)用的健康狀況,這里涉及到一個(gè)小技巧以及兩種健康檢查的方式

線程休眠

jenkins的構(gòu)建步驟執(zhí)行到健康檢查時(shí),需要讓線程休眠1~2分鐘左右,等待應(yīng)用完全啟動。第一次健康檢查如果失敗了,有可能是應(yīng)用沒有完全啟動,在休眠指定時(shí)間,如果還是失敗了,那么久判定這個(gè)應(yīng)用啟動失敗,拋出異常,讓這次ci結(jié)束并標(biāo)記失敗

健康檢查方式

1.http接口的方式:如上,使用了應(yīng)用內(nèi)提供的一個(gè)健康檢查接口,去執(zhí)行http的接口,然后拿到結(jié)果判定,一般spring boot提供了健康檢查的接口, 只需要添加如下依賴,spring-boot-starter-actuator,應(yīng)用就會多一個(gè)/health接口,如果應(yīng)用健康,會返回如下數(shù)據(jù)

2.檢查應(yīng)用運(yùn)行進(jìn)程:當(dāng)有些服務(wù)沒有使用http容器時(shí),如dubbo服務(wù)。需要使用檢查應(yīng)用進(jìn)程的方式來檢查應(yīng)用是否啟動了,具體方式如下:

stage('check health') {
            steps {
                script {
                    def healthUrl = null
                    if ('production' == "${profile}") {
                        healthUrl = "ssh it@192.xxx.10.159 'ps -ef|grep xx-service'"
                    }
                    if ('uat' == "${profile}") {
                        healthUrl = ""
                    }
                    echo "睡眠兩分鐘,待應(yīng)用完全準(zhǔn)備好"
                    Thread.sleep((long) 1000 * 60 * 1)//睡眠1分鐘
                    String shellStr = sh(script: "${healthUrl}", returnStdout: true)
                    echo "應(yīng)用健康檢查結(jié)果:${shellStr}"
                    if (shellStr.indexOf("/home/xx-service-1.0.0-") > 0) {
                        echo "應(yīng)用健康運(yùn)行"
                    } else {
                        Thread.sleep((long) 1000 * 60 * 1)//睡眠0.5分鐘
                        shellStr = sh(script: "${healthUrl}", returnStdout: true)
                        if (shellStr.indexOf("/home/xx-service-1.0.0-") > 0) {
                            echo "應(yīng)用健康運(yùn)行"
                        } else {
                            throw new RuntimeException("應(yīng)用不穩(wěn)定,請檢查服務(wù)是否正常")
                        }
                    }
                }
            }
        }

遇到的問題及小技巧

小技巧:

可以將jenkinsfile文件加上.groovy的后綴,因?yàn)閖enkinsfile的腳本搬來就是Groovy實(shí)現(xiàn)的。然后在IDE里寫腳本的時(shí)候就會有智能提示,而且會語法校驗(yàn)。記得在添加構(gòu)建任務(wù)的時(shí)候也加上.groovy,默認(rèn)是沒有的

問題:

在聲明式的jenkinsfile寫有些Groovy腳本會觸發(fā)jenkins的腳本執(zhí)行安全策略,而腳本模式下不會有這個(gè)問題,因?yàn)槟_本模式可以選擇在Groovy沙箱中運(yùn)行,如:

具體的安全策略異常如下:

[Pipeline] // node
Scripts not permitted to use new java.lang.Object. Administrators can decide whether to approve or reject this signature.
[Pipeline] End of Pipeline
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.Object
	at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectNew(StaticWhitelist.java:184)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:148)
	at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:197)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedConstructor(Checker.java:202)
	at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.constructorCall(SandboxInvoker.java:21)
	at WorkflowScript.run(WorkflowScript:58)

解決方案:

安裝插件permissive script security plugin,然后配置下jenkins.xml,加入-Dpermissive-script-security.enabled=true。重啟jenkins就好了

文末結(jié)語

jenkins插件pipeline集成持續(xù)交付管道全面介紹

jenkins插件Pipeline腳本jenkinsfile操作指南

通過這三篇jenkins pipeline的系列文章,相信你已經(jīng)入門pipeline流式構(gòu)建的腳本編寫了,基于groovy腳本建模非常靈活,基于此我們可以新增更多的玩法,比如健康檢查成功后,通過一些即時(shí)通訊工具通知構(gòu)建的結(jié)果,如微信,釘釘?shù)?。圍繞持續(xù)集成ci/cd肯定還有很多很多的場景,歡迎在下方留言一起探討。

以上就是構(gòu)建及部署jenkins pipeline實(shí)現(xiàn)持續(xù)集成持續(xù)交付腳本的詳細(xì)內(nèi)容,更多關(guān)于jenkins pipeline實(shí)現(xiàn)持續(xù)集成持續(xù)交付腳本的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論