Goals to be achieved: submit code to the master branch github triggered webhook, jenkins pull back from the github code => compile => packaged as a docker mirror => Re-release
Machine: centos 7.4
1. Install git, jenkins, docker-ce
yum install git directly:
yum install git -y
Reference may also install a new version of the article: https://linuxize.com/post/how-to-install-git-on-centos-7/
jenkins install the referenced article https://www.cnblogs.com/stulzq/p/9291237.html
Installation Reference docker-ce official website: https://docs.docker.com/install/linux/docker-ce/centos/
2. Create a net core demo upload to github
New time Click Enable docker support, it will generate a file Dockerfile of docker
This Dockerfile move to the solution (.sln) with directory
This Dockerfile basic is to copy the files in the current directory to aspnetcore-build the mirror, after which good and then compiled and then published to aspnetcore: 3.1 mirror,
and finally designated to run your dotnet core program
After creating the project to create a warehouse with desktop spread github
3. Test is working under dockerfile
#这个是jenkins默认的存放代码位置 cd /var/lib/jenkins/workspace/; git clone https://github.com/bbenph/WebApplication1.git cd WebApplication1 image_version=`date +%Y%m%d%H%M`; echo $image_version; # 停止之前的docker container docker stop test || true; # 删除这个container docker rm test || true; # build镜像并且打上tag docker build -t test:$image_version .; docker images; # 把刚刚build出来的镜像跑起来 docker run -p 8810:80 --restart=always --name test -d test:$image_version; docker logs test;
可以看到 image container 都有了 程序也跑起来了
4. 配置jenkins拉取github代码
通过上面这个dockfile脚本,我们已经把dotnet core程序编译好了,并且打包成了docker images,还直接跑起来了.
但是我们想要的应该是自动化编译部署,而且上面我们都把jenkins跑起来了,所以….
jenkins job配置
新建Job
打开jenkins首页,左侧选择”新建任务”(newJob) => Freestyle project
添加源码仓库
确认之后进入Job配置页面, 先只配置 源码管理和构建模块
上面的shell脚本说明:
/var/lib/jenkins/workspace/ 是jenkins的默认工作路径 后面的/test 是新建jenkins 任务的名称
|| true 是即使这个container不存在 报错也不影响下面shell脚本执行 因为有时候container不存在/没运行 也是可能的
docker run 命令可以 通过 docker run --help 查看各参数啥意思
点击应用 => 保存
测试下jenkins配置是否正常
现在修改代码,提交github 后 再次jenkins中的立刻构建 发现使用新的image重新创建了一个容器
修改的内容也看到了
5. 配置jenkins 代码提交github master分支后 自动化编译部署