Dockerfile+Jenkinsfile+GitLab轻松实现.NetCore程序的CICD
一.相关介绍
Dockerfile:关于Dockerfile的使用说明,我在文章《让.NetCore程序跑在任何有docker的地方》中有说到,这里不在赘述,需要的可以先看下,本文主要介绍Jenkinsfile结合dockerfile配合使用,自动构建.NetCore应用程序。 Jenkinsfile :Jenkinsfile 是 Jenkins 2.x 或更高版本核心特性 Pipeline(流水线) 的脚本,或者说对于Jenkins 流水线的定义被写在一个叫Jenkinsfile的文本文件中,该文件可以被提交到项目的源代码的控制仓库。这是"流水线即代码"的基础; 将CD 流水线作为应用程序的一部分,像其他代码一样进行版本化和审查。 创建 `Jenkinsfile`并提交它到源代码控制中提供了以下几个好处:- 自动地为所有分支创建流水线构建过程并拉取请求。
- 在流水线上代码复查/迭代 (以及剩余的源代码)。
- 对流水线进行审计跟踪。
- 该流水线的真正的源代码 , 可以被项目的多个成员查看和编辑。
二.Jenkins和GitLab的安装
工欲善其事,必先利其器Jenkins的安装可以看我之前的一篇文章:https://www.cnblogs.com/peyshine/p/12891935.htmlGitlab的安装推荐看下这篇文章:https://segmentfault.com/a/1190000021593151三.打通GitLab Webhooks与Jenkins流程
1.这里先新建一个.net core应用程序,除了新增加了一个Jenkinsfile文件以外,其他没有任何代码的修改![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172115897-1626472093.png)
pipeline{ agent any stages { stage('Build') { steps{ echo 'This is a build step' } } stage('Test') { steps{ echo 'This is a test step' } } stage('Deploy') { steps{ echo 'This is a deploy step' } } }}
然后将程序文件push到Gitlab上
2.在Jenkins系统管理,系统配置中,在Gitlab处添加相关信息![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172129295-1881913870.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172146146-200210996.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172158118-1386838445.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172216629-295045152.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172225873-1759431838.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172255611-1503245636.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172326909-1951570449.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172343590-996903074.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172352078-1325638572.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172401283-510751558.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172410622-557122095.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172421094-567360672.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172451186-1005315674.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172517730-1663743659.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172504610-380040609.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172533173-268158859.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172546782-379257972.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172557196-1675689977.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172608272-1861073842.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172618309-2111309376.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172634024-689896989.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172719905-1762282456.png)
四.接入Jenkinsfile,Dockerfile实现自动发布
编写Dockerfile如下:FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS buildCOPY *.csproj ./app/WORKDIR /appRUN dotnet restoreCOPY . ./RUN dotnet publish -o out /p:PublishWithAspNetCoreTargetManifest="false"FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS runtimeENV ASPNETCORE_URLS http://+:80WORKDIR /appCOPY --from=build /app/out ./ENTRYPOINT ["dotnet", "WebApplication_Jenkinsfile.dll"]编写Jenkinsfile如下:
pipeline{ agent any stages { stage('Checkout') { steps{ git credentialsId: '85ca7e47-532e-4901-9828-50a8da071d16', url: '', branch:'master' echo '---This is a Checkout step---' } } stage('Build') { steps{ sh '''cd WebApplication_Jenkinsfile docker rmi -f docker_webapplication_test:1.0 docker build -t docker_webapplication_test:1.0 .''' echo '---This is a Build step---' } } stage('Run') { steps{ sh '''docker rm -f docker_webapplication_test docker run --name docker_webapplication_test -d -p 7489:80 docker_webapplication_test:1.0 ''' echo '---This is a run step---' } } }}
说明:
stages 必须,包括顺序执行的一个或多个stage命令,在pipeline内仅能使用一次,通常位于agent/options后面steps 必须,steps位于stage指令块内部,包括一个或多个step。仅有一个step的情况下可以忽略关键字step及其{}这里为了演示,只添加了几个核心的步骤,可以根据需要自行添加自动测试,邮件提醒等额外步骤 流水线语法,可以参照下图中的示例步骤点击后,可以生成脚本信息,上面Jenkinsfile中的git credentialsId信息就是在这里生成的![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172801892-2032185793.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172816375-536979815.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172829075-1922203931.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172837755-1004345609.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172846738-311883527.png)
![](https://img2020.cnblogs.com/blog/645287/202005/645287-20200526172853777-216384361.png)
No comments:
Post a Comment