Centos7系统下配置Git服务器(7)-- CI/CD Jenkins代码自动上线

0x0 什么是CI/CD?

什么是CI:Continuous Integration,也就是持续集成

什么是CD:Continuous Delivery 和 Continuous Deployment,也就是持续交付、持续部署

持续集成:就是开发提交了新代码之后,立刻进行构建、(单元)测试。根据测试结果,测试过程可以确定新代码和原有代码能否正确地集成在一起,如果测试过程有问题,那么将返回代码给开发部门

持续交付:在持续集成的基础上,将集成后的代码部署到更贴近于真实的生产环境中。在贴近真实的生成环境中,可以提高数据支撑,且进一步做测试,如果进一步测试没有问题,那么可以部署到线上的生成环境中

持续部署:是在持续交付的基础上,把部署到生产环境的过程自动化,实现部署无需人工参与,如何在部署的过程中出现问题,将立即回滚代码

0x1 企业案例基于Gitee/GitLab/Git代码仓库的自动化部署

1)主机规划

名称 主机规划 配置 安装软件 系统
GitLab 192.168.0.119 4C 8G GitLab-CE Centos7.6 1810
jenkins 192.168.0.113 4C 4G Jenkins Centos7.6 1810
web01 192.168.0.108 2C 4G Nginx Centos7.6 1810
image.png

2)设置用户组添加用户

Step 1 GitLab创建用户组


image.png

Step 2 添加成员


image.png

Step3 创建新用户


image.png

image.png
image.png

image.png

创建完毕用户修改完毕密码,把用户添加到用户组给与开发权限

Step4 添加到用户群组


image.png

image.png

image.png

添加完毕!

3)创建项目仓库

创建项目仓库


image.png

image.png

image.png

【注意】我们直接从Gitee上找一个开源项目拉下来测试

实际开发中就是自己项目组的项目https://gitee.com/kangjie1209/monitor.git

image.png

image.png

image.png

项目迁移完毕

4)设置分支保护

正常情况下,我们是不允许在master分支上修改内容的,我们只能创建新的分支修改,修改完成后进行合并,这里我们做分支保护的目的是为了让其他用户如dev用户在修改完代码内容之后,发起合并请求,root用户进行审核完成才能进行合并
1、无分支保护情况
我们首先登陆dev用户,要了解dev是开发用户,root是老大,老大一般做审核,拥有最高权限退出root用户.


image.png

dev用户第一登录的时候会被要求修改密码。修改密码后重新登录。


image.png

5)首先创建一个新的分支,在新分支上修改内容(重要内容)

注意这里是在新的分支上操作的。


image.png

创建新的Dev分支

已经到Dev分支进行操作

6)在创建的Dev分支上进行开发,比如 修改代码或添加新的代码(重要内容)

点击项目中的404.html文件,打开现在的编辑器


image.png

修改了404.html文件

7)现在开发完毕请求上线代码,也就是合并分支(重要内容)

Dev用户提交合并分支请求(受保护的分支只能root授权通过才可以)


image.png

image.png

root账号通过合并请求以后合并到主分支

8) 取消受保护的分支设置,Dev开发人员可以自己合并分支

如果发现开发人员不能合并分支(是项目已经受限,需要root用户授权合并,我们也可以设置项目的分支权限)


image.png

image.png

image.png

修改index.html


image.png

image.png

image.png

0x2 Jenkins实现自动部署

Step 1

【实例】从GitLab代码仓库拉取项目到服务器自动构建


创建新任务

创建一个自由风格的项目


image.png

image.png

插件安装在可选插件搜索过滤gitlab 然后勾选插件 Blue Ocean Pipeline Editor以及Ant Plugin 还有Build Timeout等插件

Step 2 源码管理将gitlab仓库和jenkins关联

Jenkins主机的SSH KEY复制到GitLab


image.png

image.png

image.png

Jenkins插件目录
[root@jenkins jenkins]# ll /var/lib/jenkins/plugins

[root@jenkins home]# ll /var/lib/jenkins/plugins
total 120424
drwxr-xr-x. 7 jenkins jenkins      124 Jan 20 06:30 ace-editor
-rw-r--r--. 1 jenkins jenkins  4279042 Jan 20 06:29 ace-editor.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 ant
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 antisamy-markup-formatter
-rw-r--r--. 1 jenkins jenkins   107210 Jan 20 06:29 antisamy-markup-formatter.jpi
-rw-r--r--. 1 jenkins jenkins    82963 Jan 20 06:29 ant.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 apache-httpcomponents-client-4-api
-rw-r--r--. 1 jenkins jenkins  1761975 Jan 20 06:30 apache-httpcomponents-client-4-api.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 authentication-tokens
-rw-r--r--. 1 jenkins jenkins    14638 Jan 20 06:30 authentication-tokens.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-autofavorite
-rw-r--r--. 1 root    root       60849 Jan 20 09:23 blueocean-autofavorite.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-bitbucket-pipeline
-rw-r--r--. 1 root    root     2449153 Jan 20 09:23 blueocean-bitbucket-pipeline.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-commons
-rw-r--r--. 1 root    root       85384 Jan 20 09:23 blueocean-commons.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-config
-rw-r--r--. 1 root    root       61305 Jan 20 09:23 blueocean-config.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-core-js
-rw-r--r--. 1 root    root     1075851 Jan 20 09:23 blueocean-core-js.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-dashboard
-rw-r--r--. 1 root    root     2744727 Jan 20 09:23 blueocean-dashboard.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-display-url
-rw-r--r--. 1 root    root      648567 Jan 20 09:23 blueocean-display-url.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-events
-rw-r--r--. 1 root    root     5171819 Jan 20 09:23 blueocean-events.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-github-pipeline
-rw-r--r--. 1 root    root      116890 Jan 20 09:23 blueocean-github-pipeline.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-git-pipeline
-rw-r--r--. 1 root    root      835954 Jan 20 09:23 blueocean-git-pipeline.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-i18n
-rw-r--r--. 1 root    root       14963 Jan 20 09:23 blueocean-i18n.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-jira
-rw-r--r--. 1 root    root       28715 Jan 20 09:23 blueocean-jira.jpi
-rw-r--r--. 1 root    root       69904 Jan 20 09:23 blueocean.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-jwt
-rw-r--r--. 1 root    root      777943 Jan 20 09:23 blueocean-jwt.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-personalization
-rw-r--r--. 1 root    root      703909 Jan 20 09:23 blueocean-personalization.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-pipeline-api-impl
-rw-r--r--. 1 root    root      238422 Jan 20 09:23 blueocean-pipeline-api-impl.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-pipeline-editor
-rw-r--r--. 1 root    root     1641653 Jan 20 09:23 blueocean-pipeline-editor.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-pipeline-scm-api
-rw-r--r--. 1 root    root      301729 Jan 20 09:23 blueocean-pipeline-scm-api.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 blueocean-rest
drwxr-xr-x. 6 root    root          85 Jan 20 09:23 blueocean-rest-impl
-rw-r--r--. 1 root    root     1071360 Jan 20 09:23 blueocean-rest-impl.jpi
-rw-r--r--. 1 root    root      102278 Jan 20 09:23 blueocean-rest.jpi
drwxr-xr-x. 6 root    root          85 Jan 20 09:23 blueocean-web
-rw-r--r--. 1 root    root     1411835 Jan 20 09:23 blueocean-web.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 bouncycastle-api
-rw-r--r--. 1 jenkins jenkins  4885133 Jan 20 06:29 bouncycastle-api.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:30 branch-api
-rw-r--r--. 1 jenkins jenkins   308984 Jan 20 06:30 branch-api.jpi
drwxr-xr-x. 4 jenkins jenkins      204 Jan 20 06:29 build-timeout
-rw-r--r--. 1 jenkins jenkins    95728 Jan 20 06:29 build-timeout.jpi
drwxr-xr-x. 5 root    root          70 Jan 20 09:23 cloudbees-bitbucket-branch-source
-rw-r--r--. 1 root    root      403277 Jan 20 09:23 cloudbees-bitbucket-branch-source.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:29 cloudbees-folder
-rw-r--r--. 1 jenkins jenkins   219708 Jan 20 06:29 cloudbees-folder.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 command-launcher
-rw-r--r--. 1 jenkins jenkins    38820 Jan 20 06:29 command-launcher.jpi
drwxr-xr-x. 6 jenkins jenkins       82 Jan 20 06:29 credentials
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 credentials-binding
-rw-r--r--. 1 jenkins jenkins    80803 Jan 20 06:29 credentials-binding.jpi
-rw-r--r--. 1 jenkins jenkins   971342 Jan 20 06:29 credentials.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 display-url-api
-rw-r--r--. 1 jenkins jenkins    27121 Jan 20 06:30 display-url-api.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:30 docker-commons
-rw-r--r--. 1 jenkins jenkins    91018 Jan 20 06:30 docker-commons.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 docker-workflow
-rw-r--r--. 1 jenkins jenkins    82951 Jan 20 06:30 docker-workflow.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 durable-task
-rw-r--r--. 1 jenkins jenkins  5283231 Jan 20 06:29 durable-task.jpi
drwxr-xr-x. 7 jenkins jenkins       97 Jan 20 06:30 email-ext
-rw-r--r--. 1 jenkins jenkins   796637 Jan 20 06:30 email-ext.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 favorite
-rw-r--r--. 1 root    root       34106 Jan 20 09:23 favorite.jpi
drwxr-xr-x. 5 jenkins jenkins      176 Jan 20 06:30 git
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 git-client
-rw-r--r--. 1 jenkins jenkins  8503329 Jan 20 06:30 git-client.jpi
drwxr-xr-x. 7 jenkins jenkins      106 Jan 20 06:30 github
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 github-api
-rw-r--r--. 1 jenkins jenkins  1168178 Jan 20 06:30 github-api.jpi
drwxr-xr-x. 5 jenkins jenkins       98 Jan 20 06:30 github-branch-source
-rw-r--r--. 1 jenkins jenkins   280664 Jan 20 06:30 github-branch-source.jpi
-rw-r--r--. 1 jenkins jenkins  2124964 Jan 20 06:30 github.jpi
-rw-r--r--. 1 jenkins jenkins   902751 Jan 20 06:30 git.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:31 gitlab-hook
-rw-r--r--. 1 root    root     1114041 Jan 20 09:00 gitlab-hook.jpi
drwxr-xr-x. 5 root    root          86 Jan 20 09:31 gitlab-oauth
-rw-r--r--. 1 root    root     1615322 Jan 20 08:59 gitlab-oauth.jpi
drwxr-xr-x. 6 root    root          82 Jan 20 09:31 gitlab-plugin
-rw-r--r--. 1 root    root     8456411 Jan 20 08:59 gitlab-plugin.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:30 git-server
-rw-r--r--. 1 jenkins jenkins    30408 Jan 20 06:30 git-server.jpi
drwxr-xr-x. 5 jenkins jenkins      169 Jan 20 06:30 gradle
-rw-r--r--. 1 jenkins jenkins    58363 Jan 20 06:30 gradle.jpi
drwxr-xr-x. 5 jenkins jenkins       73 Jan 20 06:30 handlebars
-rw-r--r--. 1 jenkins jenkins    67784 Jan 20 06:30 handlebars.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 handy-uri-templates-2-api
-rw-r--r--. 1 root    root      594612 Jan 20 09:23 handy-uri-templates-2-api.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 htmlpublisher
-rw-r--r--. 1 root    root       71528 Jan 20 09:23 htmlpublisher.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 jackson2-api
-rw-r--r--. 1 jenkins jenkins  1972400 Jan 20 06:30 jackson2-api.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 jdk-tool
-rw-r--r--. 1 jenkins jenkins    69922 Jan 20 06:29 jdk-tool.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 jenkins-design-language
-rw-r--r--. 1 root    root     2363785 Jan 20 09:23 jenkins-design-language.jpi
drwxr-xr-x. 4 root    root         258 Jan 20 09:23 jira
-rw-r--r--. 1 root    root     4542959 Jan 20 09:23 jira.jpi
drwxr-xr-x. 5 jenkins jenkins       73 Jan 20 06:30 jquery-detached
-rw-r--r--. 1 jenkins jenkins   211945 Jan 20 06:30 jquery-detached.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 jsch
-rw-r--r--. 1 jenkins jenkins   279709 Jan 20 06:30 jsch.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 junit
-rw-r--r--. 1 jenkins jenkins   361632 Jan 20 06:29 junit.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 ldap
-rw-r--r--. 1 jenkins jenkins   130445 Jan 20 06:30 ldap.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 localization-support
-rw-r--r--. 1 jenkins jenkins    23848 Jan 20 06:30 localization-support.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:30 localization-zh-cn
-rw-r--r--. 1 jenkins jenkins   542952 Jan 20 06:30 localization-zh-cn.jpi
drwxr-xr-x. 5 jenkins jenkins       67 Jan 20 06:30 lockable-resources
-rw-r--r--. 1 jenkins jenkins    74635 Jan 20 06:30 lockable-resources.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 mailer
-rw-r--r--. 1 jenkins jenkins   126236 Jan 20 06:30 mailer.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 mapdb-api
-rw-r--r--. 1 jenkins jenkins   396306 Jan 20 06:30 mapdb-api.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:30 matrix-auth
-rw-r--r--. 1 jenkins jenkins   128779 Jan 20 06:30 matrix-auth.jpi
drwxr-xr-x. 6 jenkins jenkins       82 Jan 20 06:29 matrix-project
-rw-r--r--. 1 jenkins jenkins   230120 Jan 20 06:29 matrix-project.jpi
drwxr-xr-x. 5 root    root          70 Jan 20 09:23 mercurial
-rw-r--r--. 1 root    root      225189 Jan 20 09:23 mercurial.jpi
drwxr-xr-x. 5 jenkins jenkins       73 Jan 20 06:30 momentjs
-rw-r--r--. 1 jenkins jenkins    37051 Jan 20 06:30 momentjs.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pam-auth
-rw-r--r--. 1 jenkins jenkins  1504184 Jan 20 06:30 pam-auth.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-build-step
-rw-r--r--. 1 jenkins jenkins    34632 Jan 20 06:30 pipeline-build-step.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-github-lib
-rw-r--r--. 1 jenkins jenkins     8479 Jan 20 06:30 pipeline-github-lib.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-graph-analysis
-rw-r--r--. 1 jenkins jenkins    20613 Jan 20 06:30 pipeline-graph-analysis.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-input-step
-rw-r--r--. 1 jenkins jenkins    30501 Jan 20 06:30 pipeline-input-step.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-milestone-step
-rw-r--r--. 1 jenkins jenkins    18994 Jan 20 06:30 pipeline-milestone-step.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-model-api
-rw-r--r--. 1 jenkins jenkins  2436323 Jan 20 06:30 pipeline-model-api.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-model-declarative-agent
-rw-r--r--. 1 jenkins jenkins     6319 Jan 20 06:30 pipeline-model-declarative-agent.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:30 pipeline-model-definition
-rw-r--r--. 1 jenkins jenkins  1490265 Jan 20 06:30 pipeline-model-definition.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-model-extensions
-rw-r--r--. 1 jenkins jenkins    38764 Jan 20 06:30 pipeline-model-extensions.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-rest-api
-rw-r--r--. 1 jenkins jenkins   145776 Jan 20 06:30 pipeline-rest-api.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-stage-step
-rw-r--r--. 1 jenkins jenkins    19872 Jan 20 06:30 pipeline-stage-step.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 pipeline-stage-tags-metadata
-rw-r--r--. 1 jenkins jenkins    10708 Jan 20 06:30 pipeline-stage-tags-metadata.jpi
drwxr-xr-x. 7 jenkins jenkins      100 Jan 20 06:30 pipeline-stage-view
-rw-r--r--. 1 jenkins jenkins   201594 Jan 20 06:30 pipeline-stage-view.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 plain-credentials
-rw-r--r--. 1 jenkins jenkins    17459 Jan 20 06:29 plain-credentials.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 pubsub-light
-rw-r--r--. 1 root    root       33985 Jan 20 09:23 pubsub-light.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 resource-disposer
-rw-r--r--. 1 jenkins jenkins    21791 Jan 20 06:29 resource-disposer.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:31 ruby-runtime
-rw-r--r--. 1 root    root    19016644 Jan 20 08:59 ruby-runtime.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 scm-api
-rw-r--r--. 1 jenkins jenkins   218705 Jan 20 06:29 scm-api.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 script-security
-rw-r--r--. 1 jenkins jenkins  1004965 Jan 20 06:29 script-security.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 sse-gateway
-rw-r--r--. 1 root    root      213569 Jan 20 09:23 sse-gateway.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:29 ssh-credentials
-rw-r--r--. 1 jenkins jenkins    65885 Jan 20 06:29 ssh-credentials.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 ssh-slaves
-rw-r--r--. 1 jenkins jenkins   179773 Jan 20 06:30 ssh-slaves.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 structs
-rw-r--r--. 1 jenkins jenkins    44501 Jan 20 06:29 structs.jpi
drwxr-xr-x. 4 jenkins jenkins      275 Jan 20 06:30 subversion
-rw-r--r--. 1 jenkins jenkins  7350139 Jan 20 06:30 subversion.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 timestamper
-rw-r--r--. 1 jenkins jenkins    82494 Jan 20 06:29 timestamper.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 token-macro
-rw-r--r--. 1 jenkins jenkins   654142 Jan 20 06:29 token-macro.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 trilead-api
-rw-r--r--. 1 jenkins jenkins   384303 Jan 20 06:29 trilead-api.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:23 variant
-rw-r--r--. 1 root    root       10252 Jan 20 09:23 variant.jpi
drwxr-xr-x. 4 root    root          56 Jan 20 09:31 windows-slaves
-rw-r--r--. 1 root    root      470244 Jan 20 08:59 windows-slaves.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 workflow-aggregator
-rw-r--r--. 1 jenkins jenkins     8655 Jan 20 06:30 workflow-aggregator.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 workflow-api
-rw-r--r--. 1 jenkins jenkins   155980 Jan 20 06:29 workflow-api.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 workflow-basic-steps
-rw-r--r--. 1 jenkins jenkins   147305 Jan 20 06:30 workflow-basic-steps.jpi
drwxr-xr-x. 6 jenkins jenkins       86 Jan 20 06:30 workflow-cps
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 workflow-cps-global-lib
-rw-r--r--. 1 jenkins jenkins  3435811 Jan 20 06:30 workflow-cps-global-lib.jpi
-rw-r--r--. 1 jenkins jenkins   597525 Jan 20 06:30 workflow-cps.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 workflow-durable-task-step
-rw-r--r--. 1 jenkins jenkins    99500 Jan 20 06:29 workflow-durable-task-step.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:30 workflow-job
-rw-r--r--. 1 jenkins jenkins   111555 Jan 20 06:30 workflow-job.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:30 workflow-multibranch
-rw-r--r--. 1 jenkins jenkins    85719 Jan 20 06:30 workflow-multibranch.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:30 workflow-scm-step
-rw-r--r--. 1 jenkins jenkins    15370 Jan 20 06:30 workflow-scm-step.jpi
drwxr-xr-x. 4 jenkins jenkins       56 Jan 20 06:29 workflow-step-api
-rw-r--r--. 1 jenkins jenkins    75642 Jan 20 06:29 workflow-step-api.jpi
drwxr-xr-x. 5 jenkins jenkins       70 Jan 20 06:29 workflow-support
-rw-r--r--. 1 jenkins jenkins   380755 Jan 20 06:29 workflow-support.jpi
drwxr-xr-x. 5 jenkins jenkins       68 Jan 20 06:29 ws-cleanup
-rw-r--r--. 1 jenkins jenkins  6349503 Jan 20 06:29 ws-cleanup.jpi

Step3 继续设置构建环境Webhooks


image.png

image.png

image.png

设置配置到GitLab中


image.png

添加报错 是因为我们这里没有开启内网服务
image.png

image.png

添加成功!

Step4 添加构建在Jenkins服务器上构建脚本

cd /home/script/
vim do.sh
#!/bin/sh
DATE=$(date +%Y-%m-%d-%H-%M-%S)
CODE_DIR="/var/lib/jenkins/workspace/Jenkins-Test/"
WEB_DIR="/var/www/"
IP=192.168.0.108
get_code_tar(){
        cd $CODE_DIR && tar zcf /opt/web-$DATE.tar.gz ./*
}
scp_code_web(){
        scp /opt/web-$DATE.tar.gz $IP:$WEB_DIR
}
code_tarxf(){
        ssh $IP "cd $WEB_DIR &&mkdir web-$DATE && tar xf web-$DATE.tar.gz -C web-$DATE"
}
ln_html(){
         ssh $IP "cd $WEB_DIR && rm -rf html && ln -s web-$DATE html"
}
main(){
        get_code_tar;
        scp_code_web;
        code_tarxf;
        ln_html;
}
main

Step5 Jenkins要推送到Web服务器 设置免密登录

[root@jenkins .ssh]# ssh-copy-id -i [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.0.108 (192.168.0.108)' can't be established.
ECDSA key fingerprint is SHA256:1GMXKe1kJLq7jEyAGkQYWoVpa2/H53bfRLInTWPCcDY.
ECDSA key fingerprint is MD5:61:6e:10:be:6b:99:8e:02:f7:8a:07:94:1f:27:fa:0e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

[root@jenkins .ssh]# ssh '[email protected]'
Last login: Mon Jan 20 10:19:29 2020 from 192.168.0.103
[root@web01 ~]# exit
logout
Connection to 192.168.0.108 closed.

0x3 web服务器Apache2.4.6

[root@web01 ~]# yum -y install httpd
[root@web01 ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Aug  8 2019 11:41:18
[root@web01 ~]# sudo firewall-cmd --permanent --zone=public --add-service=http
success
[root@web01 ~]# sudo firewall-cmd --permanent --zone=public --add-service=https
success
[root@web01 ~]# sudo firewall-cmd --reload
success
[root@web01 ~]#

出现测试页面


image.png

0x4 在GitLab服务器上修改代码会触发Jenkins,推送到Web服务器

image.png
image.png
image.png

你可能感兴趣的:(Centos7系统下配置Git服务器(7)-- CI/CD Jenkins代码自动上线)