javascript
使用Spring Boot应用程序将代码管道化
這是有關基于本地Docker compose堆棧的持續交付的一系列帖子中的最后一篇(請參閱此處的第一篇和第二篇文章 )。 在這篇文章中,我使用一個簡單的Spring Boot項目來展示如何利用“ 管道作為代碼 ”的概念。 請注意,這僅是示例,還有更多可能。 我使用的應用程序來自Spring Boot站點 。 Jenkinsfile的靈感來自于這篇文章中的內容,但是我不得不修改一些東西才能使其與我的堆棧一起使用。 我的項目的資源可以在這里找到。 我將在這篇文章中解釋最重要的片段。
我使用的管道包含以下階段:
- 建設階段
- 部署階段
- 煙霧測試階段
建設階段
在構建階段,我使用GitLab插件檢出項目的源代碼。 我還將當前的commitId放在工作目錄中的textFile中。 接下來,我使用Maven(在Jenkins配置中我們稱為“ M3”的代碼,如我在此處所述)打包代碼。 我還要確保將commitId作為參數傳遞給Maven。
部署階段
在部署步驟中,我通過將'true'發布到/ shutdown路徑來關閉應用程序的運行實例。 然后,我只需運行上一步中構建的jar。 之后,作業將等待,直到應用程序響應簡單的請求。
冒煙測試
在這個簡單的測試步驟中,我將返回的已部署服務的commitId與我們簽出最新提交的代碼時得到的commitId進行了比較。 如果一切順利,則這兩個id應該匹配,如果鏈中沒有什么地方出錯了。
本示例僅此而已。 讓我們看看這對源代碼意味著什么。 由于這是一個Maven項目,因此我從pom.xml開始:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><!-- used for metrics like status, health etc --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><!-- used for unit tests --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> </dependencies> 該項目不需要特殊的依賴關系。 “ spring-boot-starter-web ”用于我們的REST控制器。 ' sprint-boot-starter-actuator '可用于檢查運行狀況等等 。
最后,使用“ spring-boot-starter-test”可以(對單元進行)單元測試。
讓我們看一下Java源代碼。 該應用程序僅啟動Spring Boot應用程序。 Controller類也非?;A:
如您所見,當GET請求進入“ /”時,我只是返回一個固定的字符串。 測試類具有以下測試代碼:
/*** Created by pascal on 19/01/2017.*/ @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class HelloControllerTest {@Autowiredprivate MockMvc mvc;@Testpublic void getHello() throws Exception {mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("Greetings from Spring Boot!")));} }我想這也很簡單,我希望將固定字符串作為對GET請求的響應。 Java代碼旁邊是“ application.properties”文件:
server.port=8888 info.app.name=@project.name@ info.app.description=@project.description@ info.app.version=@project.version@ info.app.commitid=@commitid@ endpoints.shutdown.enabled=true除了兩個功能屬性,我們正在其上運行應用程序的端口(8888),還具有通過調用端點來關閉應用程序的功能(endpoints.shutdown.enabled = true),其余的將在調用端點'/時顯示。信息'。 因為我們過濾了資源,所以Maven將參數@…@替換為實際值:
... <resources><!-- used for variable substitution in application.properties --><!-- https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering --><resource><directory>src/main/resources</directory><filtering>true</filtering></resource> </resources> ...最后,我們在項目中有了Jenkinsfile:
import groovy.json.JsonSlurper;properties([[$class: 'GitLabConnectionProperty', gitLabConnection: 'my-gitlab-connection']])node{stage 'Build, Test and Package'env.PATH = "${tool 'M3'}/bin:${env.PATH}"checkout scm// workaround, taken from https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/gitcommit/gitcommit.groovydef commitid = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()def workspacePath = pwd()sh "echo ${commitid} > ${workspacePath}/expectedCommitid.txt"withMaven(maven: 'M3',mavenSettingsConfig: 'a1adf035-653b-410d-b5a6-16b6da77b322',mavenLocalRepo: '.repository') {// Run the maven buildsh "mvn clean package -Dcommitid=${commitid}"} }node{stage 'Stop, Deploy and Start'// shutdownsh 'curl -X POST http://localhost:8888/shutdown || true'// copy file to target locationsh 'cp target/*.jar /tmp/'// start the applicationsh 'nohup java -jar /tmp/*.jar &'// wait for application to respondsh 'while ! httping -qc1 http://localhost:8888 ; do sleep 1 ; done' }node{stage 'Smoketest'def workspacePath = pwd()sh "curl --retry-delay 10 --retry 5 http://localhost:8888/info -o ${workspacePath}/info.json"if (deploymentOk()){return 0} else {return 1} }def deploymentOk(){def workspacePath = pwd()expectedCommitid = new File("${workspacePath}/expectedCommitid.txt").text.trim()actualCommitid = readCommitidFromJson()println "expected commitid from txt: ${expectedCommitid}"println "actual commitid from json: ${actualCommitid}"return expectedCommitid == actualCommitid }def readCommitidFromJson() {def workspacePath = pwd()def slurper = new JsonSlurper()def json = slurper.parseText(new File("${workspacePath}/info.json").text)def commitid = json.app.commitidreturn commitid }我之前描述了腳本的工作。 Jenkins安裝必須匹配三個重要的常量:
- 在聲明中: properties([[$class: 'GitLabConnectionProperty', gitLabConnection: 'my-gitlab-connection']]) “ 我-gitlab連接 ”我給我的gitlabConnection在詹金斯為我描述了插件的名稱相匹配這里 。
- 正如我在聲明中的“ M3”之前所述:
env.PATH = "${tool 'M3'}/bin:${env.PATH}"必須與Jenkins中的Maven安裝相匹配,如我在此處所述。 - 最后一行是mavenSettingsConfig: 'a1adf035-653b-410d-b5a6-16b6da77b322' 。 本ID這里提到的是一個復制的設置文件我設置了配置文件提供插件描述這里 。
這就是項目來源的全部。 接下來,讓我向您展示如何在Jenkins中創建管道作業。 在儀表板中,選擇創建“管道”類型的新作業:
接下來配置此作業,其中最重要的是使用從git獲得的Jenkinsfile。 要配置它,我們必須使用用戶名/密碼登錄到Gitlab(我在這里還沒有找到使用Gitlab插件的方法。如果要將Jenkinsfiles與項目分開,也可以在此處使用另一個存儲庫。來源): 現在,當我運行作業時,它將在最后一步失敗,并顯示以下錯誤:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:不允許腳本使用新的java.io.File java.lang.String
在org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectNew(StaticWhitelist.java:187)
…。
要使此工作成功運行,有一項最終設置。 默認情況下,管道作業不允許某些操作,因此我必須告訴Jenkins在這種情況下是允許的。
為此,請轉到“管理Jenkins”并轉到“進程內腳本批準”:
提到了可能的安全漏洞,您必須先批準該漏洞,然后作業才能執行操作: 在單擊“批準”按鈕后,重新運行該作業,將需要批準另一個漏洞才能使作業成功完成。 現在,構建將成功完成所有三個階段,如儀表板所示: 到此結束了連續交付和流水線作為代碼的示例。 如前所述,這只是管道的一個非常簡單的示例,但是您可以使用它來入門該概念并從中獲得更多收益。
翻譯自: https://www.javacodegeeks.com/2017/03/pipeline-code-spring-boot-application.html
總結
以上是生活随笔為你收集整理的使用Spring Boot应用程序将代码管道化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 91手机助手安卓版(91手机助手安卓)
- 下一篇: (linux的tab)