网站设置快捷键,哈尔滨建设工程交易中心网站,写作网站官方,建筑人才网 一建港航这是有关基于本地Docker compose堆栈的持续交付的一系列帖子中的最后一篇#xff08;请参阅此处的第一篇和第二篇文章 #xff09;。 在这篇文章中#xff0c;我使用一个简单的Spring Boot项目来展示如何利用“ 管道作为代码 ”的概念。 请注意#xff0c;这仅是示例#… 这是有关基于本地Docker compose堆栈的持续交付的一系列帖子中的最后一篇请参阅此处的第一篇和第二篇文章 。 在这篇文章中我使用一个简单的Spring Boot项目来展示如何利用“ 管道作为代码 ”的概念。 请注意这仅是示例还有更多可能。 我使用的应用程序来自Spring Boot站点 。 Jenkinsfile的灵感来自于这篇文章中的内容但是我不得不修改一些东西才能使其与我的堆栈一起使用。 我的项目的资源可以在这里找到。 我将在这篇文章中解释最重要的片段。 我使用的管道包含以下阶段 建设阶段 部署阶段 烟雾测试阶段 建设阶段 在构建阶段我使用GitLab插件检出项目的源代码。 我还将当前的commitId放在工作目录中的textFile中。 接下来我使用Maven在Jenkins配置中我们称为“ M3”的代码如我在此处所述打包代码。 我还要确保将commitId作为参数传递给Maven。 部署阶段 在部署步骤中我通过将true发布到/ shutdown路径来关闭应用程序的运行实例。 然后我只需运行上一步中构建的jar。 之后作业将等待直到应用程序响应简单的请求。 冒烟测试 在这个简单的测试步骤中我将返回的已部署服务的commitId与我们签出最新提交的代码时得到的commitId进行了比较。 如果一切顺利则这两个id应该匹配如果链中没有什么地方出错了。 本示例仅此而已。 让我们看看这对源代码意味着什么。 由于这是一个Maven项目因此我从pom.xml开始 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependency!-- used for metrics like status, health etc --groupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependency!-- used for unit tests --groupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency
/dependencies 该项目不需要特殊的依赖关系。 “ spring-boot-starter-web ”用于我们的REST控制器。 sprint-boot-starter-actuator 可用于检查运行状况等等 。 最后使用“ spring-boot-starter-test”可以对单元进行单元测试。 让我们看一下Java源代码。 该应用程序仅启动Spring Boot应用程序。 Controller类也非常基础 package hello;import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;RestController
public class HelloController {RequestMapping(/)public String index() {return Greetings from Spring Boot!;}
} 如您所见当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.port8888
info.app.nameproject.name
info.app.descriptionproject.description
info.app.versionproject.version
info.app.commitidcommitid
endpoints.shutdown.enabledtrue 除了两个功能属性我们正在其上运行应用程序的端口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 --resourcedirectorysrc/main/resources/directoryfilteringtrue/filtering/resource
/resources
... 最后我们在项目中有了Jenkinsfile import groovy.json.JsonSlurper;properties([[$class: GitLabConnectionProperty, gitLabConnection: my-gitlab-connection]])node{stage Build, Test and Packageenv.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.txtwithMaven(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 Smoketestdef workspacePath pwd()sh curl --retry-delay 10 --retry 5 http://localhost:8888/info -o ${workspacePath}/info.jsonif (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.rejectNewStaticWhitelist.java:187 …。 要使此工作成功运行有一项最终设置。 默认情况下管道作业不允许某些操作因此我必须告诉Jenkins在这种情况下是允许的。 为此请转到“管理Jenkins”并转到“进程内脚本批准” 提到了可能的安全漏洞您必须先批准该漏洞然后作业才能执行操作 在单击“批准”按钮后重新运行该作业将需要批准另一个漏洞才能使作业成功完成。 现在构建将成功完成所有三个阶段如仪表板所示 到此结束了连续交付和流水线作为代码的示例。 如前所述这只是管道的一个非常简单的示例但是您可以使用它来入门该概念并从中获得更多收益。 翻译自: https://www.javacodegeeks.com/2017/03/pipeline-code-spring-boot-application.html