11 基于 Docker 构建 Jenkins CI 平台

11.1 CI/CD概述

持续集成(Continuous Integration,Cl):代码合并、构建、部署、测试都在一起,不断地执行这个过程,并对结果反馈。

持续部署(Continuous Deployment,CD):部署到测试环境、预生产环境、生产环境。

持续交付(Continuous Delivery,CD):将最终产品发布到生产环境,给用户使用。

03 Docker CICD 部分 - 图1

11.2 CI工作流程设计

03 Docker CICD 部分 - 图2

CI 工作流程

拓展:

Git 代码版本管理系统

GitLab 基于Git做图形管理页面

GitHub 公共的代码管理仓库

IP地址 主机名 角色
10.0.0.100 harbor-ssh-server Harbor 镜像仓库
10.0.0.101 jenkins-ssh-server Jenkins CICD
10.0.0.102 gitlab-ssh-server GitLab 代码仓库

11.3 部署 GitLab

11.3.1 部署 GitLab

  1. mkdir gitlab && cd gitlab
  2. docker run -d \
  3. --name gitlab \
  4. -p 8443:443 \
  5. -p 9999:80 \
  6. -p 9998:22 \
  7. -v $PWD/config:/etc/gitlab \
  8. -v $PWD/logs:/var/log/gitlab \
  9. -v $PWD/data:/var/opt/gitlab \
  10. -v /etc/localtime:/etc/localtime \
  11. --restart=always \
  12. lizhenliang/gitlab-ce-zh:latest
  13. # 查看GitLab的容器日志
  14. $ docker logs -f gitlab

访问地址::http://<IP地址>:9999

03 Docker CICD 部分 - 图3

初次会先设置管理员密码,然后登陆,默认管理员用户名root,密码就是刚设置的。

03 Docker CICD 部分 - 图4

11.3.2 创建项目,提交测试代码

进入后先创建项目,提交代码,以便后面测试。

项目名称自定义:java-devops-demo

03 Docker CICD 部分 - 图5

devops-java-demo.zip

  1. unzip devops-java-demo.zip
  2. cd devops-java-demo
  3. git init
  4. # 若无法正常推送到远程仓库,则将项目代码中的隐藏目录 .git 删除即可
  5. git remote add origin http://10.0.0.102:9999/root/java-devops-demo.git
  6. git add .
  7. # git config --global user.email "Your@example.com"
  8. # git config --global user.name "Your Name"
  9. git config --global user.email "zhongzhiwei@kubesphere.io"
  10. git config --global user.name "zhongzhiwei"
  11. git commit -m "all"
  12. $ git push origin master
  13. Username for 'http://10.0.0.102:9999': root
  14. Password for 'http://root@10.0.0.102:9999': [Gitlab root密码]
  • GitLab 代码仓库中就有相应的代码

使用 https://github.com/Dragon-zw/tomcat-java-demo.git 代码

  1. unzip devops-java-demo.zip
  2. cd devops-java-demo
  3. git init
  4. # 若无法正常推送到远程仓库,则将项目代码中的隐藏目录 .git 删除即可
  5. git remote add origin http://10.0.0.102:9999/root/java-devops-demo.git
  6. git add .
  7. # git config --global user.email "Your@example.com"
  8. # git config --global user.name "Your Name"
  9. git config --global user.email "zhongzhiwei@kubesphere.io"
  10. git config --global user.name "zhongzhiwei"
  11. git commit -m "all"
  12. $ git push origin master
  13. Username for 'http://10.0.0.102:9999': root
  14. Password for 'http://root@10.0.0.102:9999': [Gitlab root密码]

03 Docker CICD 部分 - 图6

GitLab 代码项目仓库中就拥有该代码

03 Docker CICD 部分 - 图7

11.4 部署 Harbor 镜像仓库

11.4.1 安装 Docker 和 Docker-Compose

范例:Ubuntu 20.04 部署 Docker 和 Docker-Compose 脚本

  1. #!/bin/bash
  2. #Description: ubuntu20.04系统安装docker-compose编排工具
  3. COLOR="echo -e \\033[1;31m"
  4. END="\033[m"
  5. DOCKER_VERSION="5:20.10.21~3-0~ubuntu-focal"
  6. install_docker(){
  7. ${COLOR}"开始安装 Docker....."${END}
  8. sleep 1
  9. apt update
  10. apt -y install apt-transport-https ca-certificates curl software-properties-common
  11. curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
  12. add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
  13. apt update
  14. ${COLOR}"Docker有以下版本:"${END}
  15. sleep 2
  16. apt-cache madison docker-ce
  17. ${COLOR}"5秒后即将安装: docker-"${DOCKER_VERSION}" 版本....."${END}
  18. ${COLOR}"如果想安装其它Docker版本,请按ctrl+c键退出,修改版本再执行"${END}
  19. sleep 5
  20. apt -y install docker-ce=${DOCKER_VERSION} docker-ce-cli=${DOCKER_VERSION}
  21. mkdir -p /etc/docker
  22. tee /etc/docker/daemon.json <<-'EOF'
  23. {
  24. "registry-mirrors": ["https://po13h3y1.mirror.aliyuncs.com","http://hub-mirror.c.163.com","https://mirror.ccs.tencentyun.com","http://f1361db2.m.daocloud.io"],
  25. "exec-opts": ["native.cgroupdriver=systemd"],
  26. "log-driver": "json-file",
  27. "log-opts": {
  28. "max-size": "100m"
  29. },
  30. "storage-driver": "overlay2"
  31. }
  32. EOF
  33. systemctl daemon-reload
  34. systemctl restart docker
  35. docker version && ${COLOR}"Docker 安装完成"${END} || ${COLOR}"Docker 安装失败"${END}
  36. }
  37. install_docker_compose(){
  38. ${COLOR}"开始安装 Docker compose....."${END}
  39. sleep 1
  40. # curl -L https://github.com/docker/compose/releases/download/1.25.3/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  41. # chmod +x /usr/local/bin/docker-compose
  42. curl -L https://get.daocloud.io/docker/compose/releases/download/v2.13.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  43. chmod +x /usr/local/bin/docker-compose
  44. docker-compose --version && ${COLOR}"Docker Compose 安装完成"${END} || ${COLOR}"Docker compose 安装失败"${END}
  45. }
  46. dpkg -s docker-ce &> /dev/null && ${COLOR}"Docker已安装"${END} || install_docker
  47. docker-compose --version &> /dev/null && ${COLOR}"Docker Compose已安装"${END} || install_docker_compose

范例:CentOS & Ubuntu 全系列部署 Docker 和 Docker-Compose 脚本

  1. #!/bin/bash
  2. #Description:全系列系统安装 docker harbor镜像仓库
  3. DOCKER_VERSION="20.10.10"
  4. #DOCKER_VERSION="19.03.14"
  5. DOCKER_COMPOSE_VERSION=2.6.1
  6. #DOCKER_COMPOSE_VERSION=1.29.2
  7. DOCKER_COMPOSE_FILE=docker-compose-Linux-x86_64
  8. #HARBOR_NAME=`hostname -I|awk '{print $1}'`
  9. HARBOR_ADMIN_PASSWORD=123456
  10. HARBOR_IP=`hostname -I|awk '{print $1}'`
  11. COLOR_SUCCESS="echo -e \\033[1;32m"
  12. COLOR_FAILURE="echo -e \\033[1;31m"
  13. END="\033[m"
  14. . /etc/os-release
  15. UBUNTU_DOCKER_VERSION="5:${DOCKER_VERSION}~3-0~${ID}-${UBUNTU_CODENAME}"
  16. color () {
  17. RES_COL=60
  18. MOVE_TO_COL="echo -en \\033[${RES_COL}G"
  19. SETCOLOR_SUCCESS="echo -en \\033[1;32m"
  20. SETCOLOR_FAILURE="echo -en \\033[1;31m"
  21. SETCOLOR_WARNING="echo -en \\033[1;33m"
  22. SETCOLOR_NORMAL="echo -en \E[0m"
  23. echo -n "$1" && $MOVE_TO_COL
  24. echo -n "["
  25. if [ $2 = "success" -o $2 = "0" ] ;then
  26. ${SETCOLOR_SUCCESS}
  27. echo -n $" OK "
  28. elif [ $2 = "failure" -o $2 = "1" ] ;then
  29. ${SETCOLOR_FAILURE}
  30. echo -n $"FAILED"
  31. else
  32. ${SETCOLOR_WARNING}
  33. echo -n $"WARNING"
  34. fi
  35. ${SETCOLOR_NORMAL}
  36. echo -n "]"
  37. echo
  38. }
  39. install_docker(){
  40. if [ $ID = "centos" -o $ID = "rocky" ];then
  41. if [ $VERSION_ID = "7" ];then
  42. cat > /etc/yum.repos.d/docker.repo <<EOF
  43. [docker]
  44. name=docker
  45. gpgcheck=0
  46. #baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
  47. baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/
  48. EOF
  49. else
  50. cat > /etc/yum.repos.d/docker.repo <<EOF
  51. [docker]
  52. name=docker
  53. gpgcheck=0
  54. #baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
  55. baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/8/x86_64/stable/
  56. EOF
  57. fi
  58. yum clean all
  59. ${COLOR_FAILURE} "Docker有以下版本"${END}
  60. yum list docker-ce --showduplicates
  61. ${COLOR_FAILURE}"5秒后即将安装: docker-"${DOCKER_VERSION}" 版本....."${END}
  62. ${COLOR_FAILURE}"如果想安装其它Docker版本,请按ctrl+c键退出,修改版本再执行"${END}
  63. sleep 5
  64. yum -y install docker-ce-$DOCKER_VERSION docker-ce-cli-$DOCKER_VERSION \
  65. || { color "Base,Extras的yum源失败,请检查yum源配置" 1;exit; }
  66. else
  67. dpkg -s docker-ce &> /dev/null && $COLOR"Docker已安装,退出" 1 && exit
  68. apt update || { color "更新包索引失败" 1 ; exit 1; }
  69. apt -y install apt-transport-https ca-certificates curl software-properties-common || \
  70. { color "安装相关包失败" 1 ; exit 2; }
  71. curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
  72. add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
  73. apt update
  74. ${COLOR_FAILURE} "Docker有以下版本"${END}
  75. apt-cache madison docker-ce
  76. ${COLOR_FAILURE}"5秒后即将安装: docker-"${UBUNTU_DOCKER_VERSION}" 版本....."${END}
  77. ${COLOR_FAILURE}"如果想安装其它Docker版本,请按ctrl+c键退出,修改版本再执行"${END}
  78. sleep 5
  79. apt -y install docker-ce=${UBUNTU_DOCKER_VERSION} docker-ce-cli=${UBUNTU_DOCKER_VERSION}
  80. fi
  81. if [ $? -eq 0 ];then
  82. color "安装软件包成功" 0
  83. else
  84. color "安装软件包失败,请检查网络配置" 1
  85. exit
  86. fi
  87. mkdir -p /etc/docker
  88. tee /etc/docker/daemon.json <<-'EOF'
  89. {
  90. "registry-mirrors": ["https://po13h3y1.mirror.aliyuncs.com","http://hub-mirror.c.163.com","https://mirror.ccs.tencentyun.com","http://f1361db2.m.daocloud.io"],
  91. "exec-opts": ["native.cgroupdriver=systemd"],
  92. "log-driver": "json-file",
  93. "log-opts": {
  94. "max-size": "100m"
  95. },
  96. "storage-driver": "overlay2"
  97. }
  98. EOF
  99. systemctl daemon-reload
  100. systemctl enable docker
  101. systemctl restart docker
  102. docker version && color "Docker 安装成功" 0 || color "Docker 安装失败" 1
  103. echo 'alias rmi="docker images -qa|xargs docker rmi -f"' >> ~/.bashrc
  104. echo 'alias rmc="docker ps -qa|xargs docker rm -f"' >> ~/.bashrc
  105. }
  106. install_docker_compose(){
  107. if [ $ID = "centos" -o $ID = "rocky" ];then
  108. ${COLOR_SUCCESS}"开始安装 Docker compose....."${END}
  109. sleep 1
  110. if [ ! -e ${DOCKER_COMPOSE_FILE} ];then
  111. #curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/${DOCKER_COMPOSE_FILE} -o /usr/bin/docker-compose
  112. curl -L https://get.daocloud.io/docker/compose/releases/download/v${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m) -o /usr/bin/docker-compose
  113. else
  114. mv ${DOCKER_COMPOSE_FILE} /usr/bin/docker-compose
  115. fi
  116. chmod +x /usr/bin/docker-compose
  117. else
  118. apt -y install docker-compose
  119. fi
  120. if docker-compose --version ;then
  121. ${COLOR_SUCCESS}"Docker Compose 安装完成"${END}
  122. else
  123. ${COLOR_FAILURE}"Docker compose 安装失败"${END}
  124. exit
  125. fi
  126. }
  127. docker info &> /dev/null && ${COLOR_FAILURE}"Docker已安装"${END} || install_docker
  128. docker-compose --version &> /dev/null && ${COLOR_FAILURE}"Docker Compose已安装"${END} || install_docker_compose

11.4.2 解压离线包部署

  1. # https://github.com/goharbor/harbor/releases/download/v2.6.2/harbor-offline-installer-v2.6.2.tgz
  2. $ tar -zxvf harbor-offline-installer-v2.6.2.tgz
  3. $ cd harbor
  4. $ cp harbor.yml.tmpl harbor.yml
  5. $ vim harbor.yml
  6. hostname: reg.kubesphere.com
  7. https: #先注释https相关配置
  8. harbor_admin_password: Harbor12345
  9. # 部署Harbor HTTP
  10. $ ./prepare
  11. $ ./install.sh

03 Docker CICD 部分 - 图8

  1. $ docker-compose ps
  2. Name Command State Ports
  3. -----------------------------------------------------------------------------------------------------------------
  4. harbor-core /harbor/entrypoint.sh Up (health: starting)
  5. harbor-db /docker-entrypoint.sh 96 13 Up (health: starting)
  6. harbor-jobservice /harbor/entrypoint.sh Up (health: starting)
  7. harbor-log /bin/sh -c /usr/local/bin/ ... Up (health: starting) 127.0.0.1:1514->10514/tcp
  8. harbor-portal nginx -g daemon off; Up (health: starting)
  9. nginx nginx -g daemon off; Up (health: starting) 0.0.0.0:80->8080/tcp,:::80->8080/tcp
  10. redis redis-server /etc/redis.conf Up (health: starting)
  11. registry /home/harbor/entrypoint.sh Up (health: starting)
  12. registryctl /home/harbor/start.sh Up (health: starting)

浏览器访问http://<IP地址>:80也可以修改主机的host文件将IP地址 域名进行绑定

默认用户名:admin

默认密码:Harbor12345

03 Docker CICD 部分 - 图9

Harbor 的主界面

03 Docker CICD 部分 - 图10

  1. cat >> /etc/hosts <<EOF
  2. # KubeSphere Harbor Host BEGIN
  3. 10.0.0.100 reg.kubesphere.com
  4. # KubeSphere Harbor Host END
  5. EOF

11.4.3 在 Jenkins 主机配置Docker可信任,如果是HTTPS需要拷贝证书

由于Harbor未配置https,还需要在docker配置可信任。

  1. $ cat /etc/docker/daemon.json
  2. {
  3. "registry-mirrors": ["https://po13h3y1.mirror.aliyuncs.com","http://hub-mirror.c.163.com","https://mirror.ccs.tencentyun.com","http://f1361db2.m.daocloud.io"],
  4. "exec-opts": ["native.cgroupdriver=systemd"],
  5. "log-driver": "json-file",
  6. "log-opts": {
  7. "max-size": "100m"
  8. },
  9. "storage-driver": "overlay2",
  10. "insecure-registries": ["reg.kubesphere.com"]
  11. }

11.5 部署 Jenkins

11.5.1 准备JDK 和 Maven 环境

将二进制包上传到服务器并解压到工作目录,用于让Jenkins容器挂载使用。

  1. # JDK Download:https://lupf.cn/articles/2022/02/19/1645283667689.html
  2. # Maven Download:https://dlcdn.apache.org/maven/maven-3/3.5.4/binaries/
  3. # wget https://dlcdn.apache.org/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz --no-check-certificate
  4. tar -zxvf jdk-8u45-linux-x64.tar.gz
  5. mv jdk1.8.0_45/ /usr/local/jdk
  6. tar -zxvf apache-maven-3.5.4-bin.tar.gz
  7. mv apache-maven-3.5.4/ /usr/local/maven

修改Maven源:

  1. # docker pull jenkins/jenkins:lts
  2. docker pull jenkins/jenkins:2.375.1-lts
  3. docker run -d --name jenkins -p 80:8080 -p 50000:50000 -u root \
  4. -v /opt/jenkins_home:/var/jenkins_home \
  5. -v /var/run/docker.sock:/var/run/docker.sock \
  6. -v /usr/bin/docker:/usr/bin/docker \
  7. -v /usr/local/maven:/usr/local/maven \
  8. -v /usr/local/jdk:/usr/local/jdk \
  9. -v /etc/localtime:/etc/localtime \
  10. --restart=always \
  11. --name jenkins jenkins/jenkins:2.375.1-lts
  12. # 查看Jenkins日志
  13. $ docker logs -f jenkins

访问地址:http://IP

03 Docker CICD 部分 - 图11

:::warning Jenkins 是类似于 Docker in Docker 的模式

:::

  1. # 获取Jenkins的管理员密码
  2. $ docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword
  3. 2f9c8ca73c98418182bd1af7a19a37ca

Jenkins 使用安装推荐的插件

03 Docker CICD 部分 - 图12

11.5.2 安装插件

管理Jenkins→系统配置→管理插件→搜索git/pipeline/blue ocean,选中点击安装。默认从国外网络下载插件,会比较慢,建议修改国内源:

  1. $ docker exec -it jenkins /bin/bash
  2. cd /opt/jenkins_home/updates
  3. # sed -i 's/https:\/\/updates.jenkins.io\/download/http:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' /var/lib/jenkins/updates/default.json
  4. # sed -i 's/https:\/\/www.google.com/https:\/\/www.baidu.com/g' /var/lib/jenkins/updates/default.json
  5. sed -i 's/https:\/\/updates.jenkins.io\/download/http:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' /opt/jenkins_home/updates/default.json
  6. sed -i 's/https:\/\/www.google.com/https:\/\/www.baidu.com/g' /opt/jenkins_home/updates/default.json
  7. $ docker restart jenkins

11.6 发布测试

11.6.1 创建项目并配置

New ltem → Pipeline → This project is parameterized → String Parameter

  • Name: Branch #变量名,下面脚本中调用
  • Default Value: master #默认分支
  • Description: 发布的代码分支 #描述

03 Docker CICD 部分 - 图13

11.6.2 Pipeline 脚本

初始化Pipeline 脚本范例:

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('1.拉取代码') {
  5. steps {
  6. echo '拉取代码'
  7. }
  8. }
  9. stage('2.代码编译') {
  10. steps {
  11. echo '代码编译'
  12. }
  13. }
  14. stage('3.部署代码') {
  15. steps {
  16. echo '部署代码'
  17. }
  18. }
  19. }
  20. }

03 Docker CICD 部分 - 图14

结果看板

03 Docker CICD 部分 - 图15

最终的Jenkinsfile Pipeline示例

  1. #!/usr/bin/env groovy
  2. // 定义镜像仓库地址
  3. def registry = "reg.kubesphere.com"
  4. def project = "welcome"
  5. def app_name = "demo"
  6. // 需要 Groovy Postbuild Jenkins 插件
  7. def image_name = "${registry}/${project}/${app_name}:${Branch}-${BUILD_NUMBER}"
  8. // GitLab的地址
  9. def git_address = "http://10.0.0.102:9999/root/java-devops-demo.git"
  10. // 连接Harbor的用户名和密码
  11. def docker_registry_auth = "d5c0e5c2-5a39-496e-86ae-697a448f5b84"
  12. // 连接GitLab的用户名和密码
  13. def git_auth = "6d59e843-034f-4531-8705-d7f6c1f40b23"
  14. pipeline {
  15. agent any
  16. stages {
  17. stage('拉取代码') {
  18. steps {
  19. checkout ([$class: 'GitSCM', branches: [[name: "${Branch}"]], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
  20. }
  21. }
  22. stage('代码编译') {
  23. steps {
  24. sh """
  25. pwd
  26. JAVA_HOME=/usr/local/jdk
  27. PATH=$JAVA_HOME/bin:/usr/local/maven/bin:$PATH
  28. java -version
  29. mvn clean package -Dmaven.test.skip=true
  30. ls -alhR
  31. """
  32. }
  33. }
  34. stage('构建镜像') {
  35. steps {
  36. withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}",passwordVariable: "password",usernameVariable: "username")]) {
  37. sh """
  38. echo '
  39. FROM lizhenliang/tomcat:latest
  40. LABEL maintainer zhongzhiwei <zhongzhiwei@kubesphere.io>
  41. RUN rm -rf /usr/local/tomcat/webapps/*
  42. ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
  43. ' > Dockerfile
  44. docker build -t ${image_name} .
  45. docker login -u ${username} -p '${password}' ${registry}
  46. docker push ${image_name}
  47. """
  48. }
  49. }
  50. }
  51. stage('部署到Docker') {
  52. steps {
  53. sh """
  54. docker rm -f tomcat-java-demo | true
  55. docker container run -d --name tomcat-java-demo -p 18080:8080 ${image_name}
  56. """
  57. }
  58. }
  59. }
  60. }

使用阿里云Maven镜像加速 **/usr/local/maven/conf/settings.xml**

  1. cat settings.xml
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <!--
  4. Licensed to the Apache Software Foundation (ASF) under one
  5. or more contributor license agreements. See the NOTICE file
  6. distributed with this work for additional information
  7. regarding copyright ownership. The ASF licenses this file
  8. to you under the Apache License, Version 2.0 (the
  9. "License"); you may not use this file except in compliance
  10. with the License. You may obtain a copy of the License at
  11. http://www.apache.org/licenses/LICENSE-2.0
  12. Unless required by applicable law or agreed to in writing,
  13. software distributed under the License is distributed on an
  14. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. KIND, either express or implied. See the License for the
  16. specific language governing permissions and limitations
  17. under the License.
  18. -->
  19. <!--
  20. | This is the configuration file for Maven. It can be specified at two levels:
  21. |
  22. | 1. User Level. This settings.xml file provides configuration for a single user,
  23. | and is normally provided in ${user.home}/.m2/settings.xml.
  24. |
  25. | NOTE: This location can be overridden with the CLI option:
  26. |
  27. | -s /path/to/user/settings.xml
  28. |
  29. | 2. Global Level. This settings.xml file provides configuration for all Maven
  30. | users on a machine (assuming they're all using the same Maven
  31. | installation). It's normally provided in
  32. | ${maven.conf}/settings.xml.
  33. |
  34. | NOTE: This location can be overridden with the CLI option:
  35. |
  36. | -gs /path/to/global/settings.xml
  37. |
  38. | The sections in this sample file are intended to give you a running start at
  39. | getting the most out of your Maven installation. Where appropriate, the default
  40. | values (values used when the setting is not specified) are provided.
  41. |
  42. |-->
  43. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  44. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  45. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  46. <!-- localRepository
  47. | The path to the local repository maven will use to store artifacts.
  48. |
  49. | Default: ${user.home}/.m2/repository
  50. <localRepository>/path/to/local/repo</localRepository>
  51. -->
  52. <!-- interactiveMode
  53. | This will determine whether maven prompts you when it needs input. If set to false,
  54. | maven will use a sensible default value, perhaps based on some other setting, for
  55. | the parameter in question.
  56. |
  57. | Default: true
  58. <interactiveMode>true</interactiveMode>
  59. -->
  60. <!-- offline
  61. | Determines whether maven should attempt to connect to the network when executing a build.
  62. | This will have an effect on artifact downloads, artifact deployment, and others.
  63. |
  64. | Default: false
  65. <offline>false</offline>
  66. -->
  67. <!-- pluginGroups
  68. | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
  69. | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
  70. | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
  71. |-->
  72. <pluginGroups>
  73. <!-- pluginGroup
  74. | Specifies a further group identifier to use for plugin lookup.
  75. <pluginGroup>com.your.plugins</pluginGroup>
  76. -->
  77. </pluginGroups>
  78. <!-- proxies
  79. | This is a list of proxies which can be used on this machine to connect to the network.
  80. | Unless otherwise specified (by system property or command-line switch), the first proxy
  81. | specification in this list marked as active will be used.
  82. |-->
  83. <proxies>
  84. <!-- proxy
  85. | Specification for one proxy, to be used in connecting to the network.
  86. |
  87. <proxy>
  88. <id>optional</id>
  89. <active>true</active>
  90. <protocol>http</protocol>
  91. <username>proxyuser</username>
  92. <password>proxypass</password>
  93. <host>proxy.host.net</host>
  94. <port>80</port>
  95. <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  96. </proxy>
  97. -->
  98. </proxies>
  99. <!-- servers
  100. | This is a list of authentication profiles, keyed by the server-id used within the system.
  101. | Authentication profiles can be used whenever maven must make a connection to a remote server.
  102. |-->
  103. <servers>
  104. <!-- server
  105. | Specifies the authentication information to use when connecting to a particular server, identified by
  106. | a unique name within the system (referred to by the 'id' attribute below).
  107. |
  108. | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
  109. | used together.
  110. |
  111. <server>
  112. <id>deploymentRepo</id>
  113. <username>repouser</username>
  114. <password>repopwd</password>
  115. </server>
  116. -->
  117. <!-- Another sample, using keys to authenticate.
  118. <server>
  119. <id>siteServer</id>
  120. <privateKey>/path/to/private/key</privateKey>
  121. <passphrase>optional; leave empty if not used.</passphrase>
  122. </server>
  123. -->
  124. </servers>
  125. <!-- mirrors
  126. | This is a list of mirrors to be used in downloading artifacts from remote repositories.
  127. |
  128. | It works like this: a POM may declare a repository to use in resolving certain artifacts.
  129. | However, this repository may have problems with heavy traffic at times, so people have mirrored
  130. | it to several places.
  131. |
  132. | That repository definition will have a unique id, so we can create a mirror reference for that
  133. | repository, to be used as an alternate download site. The mirror site will be the preferred
  134. | server for that repository.
  135. |-->
  136. <mirrors>
  137. <!-- mirror
  138. | Specifies a repository mirror site to use instead of a given repository. The repository that
  139. | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  140. | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  141. |
  142. <mirror>
  143. <id>mirrorId</id>
  144. <mirrorOf>repositoryId</mirrorOf>
  145. <name>Human Readable Name for this Mirror.</name>
  146. <url>http://my.repository.com/repo/path</url>
  147. </mirror>
  148. -->
  149. <!--
  150. <mirror>
  151. <id>nexus</id>
  152. <mirrorOf>*</mirrorOf>
  153. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  154. </mirror>
  155. <mirror>
  156. <id>nexus-public-snapshots</id>
  157. <mirrorOf>public-snapshots</mirrorOf>
  158. <url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
  159. </mirror>
  160. <mirror>
  161. <id>nexus-public-snapshots1</id>
  162. <mirrorOf>public-snapshots1</mirrorOf>
  163. <url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
  164. </mirror>
  165. -->
  166. <mirror>
  167. <id>central</id>
  168. <mirrorOf>central</mirrorOf>
  169. <name>aliyun maven</name>
  170. <url>https://maven.aliyun.com/repository/public</url>
  171. </mirror>
  172. </mirrors>
  173. <!-- profiles
  174. | This is a list of profiles which can be activated in a variety of ways, and which can modify
  175. | the build process. Profiles provided in the settings.xml are intended to provide local machine-
  176. | specific paths and repository locations which allow the build to work in the local environment.
  177. |
  178. | For example, if you have an integration testing plugin - like cactus - that needs to know where
  179. | your Tomcat instance is installed, you can provide a variable here such that the variable is
  180. | dereferenced during the build process to configure the cactus plugin.
  181. |
  182. | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
  183. | section of this document (settings.xml) - will be discussed later. Another way essentially
  184. | relies on the detection of a system property, either matching a particular value for the property,
  185. | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
  186. | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
  187. | Finally, the list of active profiles can be specified directly from the command line.
  188. |
  189. | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
  190. | repositories, plugin repositories, and free-form properties to be used as configuration
  191. | variables for plugins in the POM.
  192. |
  193. |-->
  194. <profiles>
  195. <!-- profile
  196. | Specifies a set of introductions to the build process, to be activated using one or more of the
  197. | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
  198. | or the command line, profiles have to have an ID that is unique.
  199. |
  200. | An encouraged best practice for profile identification is to use a consistent naming convention
  201. | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
  202. | This will make it more intuitive to understand what the set of introduced profiles is attempting
  203. | to accomplish, particularly when you only have a list of profile id's for debug.
  204. |
  205. | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
  206. <profile>
  207. <id>jdk-1.4</id>
  208. <activation>
  209. <jdk>1.4</jdk>
  210. </activation>
  211. <repositories>
  212. <repository>
  213. <id>jdk14</id>
  214. <name>Repository for JDK 1.4 builds</name>
  215. <url>http://www.myhost.com/maven/jdk14</url>
  216. <layout>default</layout>
  217. <snapshotPolicy>always</snapshotPolicy>
  218. </repository>
  219. </repositories>
  220. </profile>
  221. -->
  222. <!--
  223. | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
  224. | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
  225. | might hypothetically look like:
  226. |
  227. | ...
  228. | <plugin>
  229. | <groupId>org.myco.myplugins</groupId>
  230. | <artifactId>myplugin</artifactId>
  231. |
  232. | <configuration>
  233. | <tomcatLocation>${tomcatPath}</tomcatLocation>
  234. | </configuration>
  235. | </plugin>
  236. | ...
  237. |
  238. | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
  239. | anything, you could just leave off the <value/> inside the activation-property.
  240. |
  241. <profile>
  242. <id>env-dev</id>
  243. <activation>
  244. <property>
  245. <name>target-env</name>
  246. <value>dev</value>
  247. </property>
  248. </activation>
  249. <properties>
  250. <tomcatPath>/path/to/tomcat/instance</tomcatPath>
  251. </properties>
  252. </profile>
  253. -->
  254. </profiles>
  255. <!-- activeProfiles
  256. | List of profiles that are active for all builds.
  257. |
  258. <activeProfiles>
  259. <activeProfile>alwaysActiveProfile</activeProfile>
  260. <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  261. </activeProfiles>
  262. -->
  263. </settings>

11.6.3 添加凭证

  • 添加字符参数

03 Docker CICD 部分 - 图16

  • 添加 Harbor 私有镜像的凭证( 需要确保凭证的信息正确 )

03 Docker CICD 部分 - 图17

  • 添加 GitLab 私有代码仓库的凭证( 需要确保凭证的信息正确 )

03 Docker CICD 部分 - 图18

  • 使用 Jenkins 生成的ID号。

03 Docker CICD 部分 - 图19

11.6.4 运行构建

  • Jenkins 查看流水线的状态

03 Docker CICD 部分 - 图20

03 Docker CICD 部分 - 图21

  • Harbor 镜像仓库的镜像上传状态

03 Docker CICD 部分 - 图22

  • 访问部署页面

03 Docker CICD 部分 - 图23

11.7 Jenkins Pipeline

Jenkins Pipeline是一套插件,支持在Jenkins中实现集成和持续交付管道;

Pipeline通过特定语法对简单到复杂的传输管道进行建模;

  • 声明式:遵循与<font style="color:#E8323C;">Groovy</font>相同语法。<font style="color:#E8323C;">pipeline { }</font>
  • 脚本式:支持Groovy大部分功能,也是非常表达和灵活的工具。node { }

Jenkins Pipeline的定义被写入一个文本文件,称为Jenkinsfile。

  1. Jenkinsfile (Declarative Pipeline)
  2. pipeline {
  3. agent any
  4. stages {
  5. stage('Build') {
  6. steps {
  7. //
  8. }
  9. }
  10. stage('Test') {
  11. steps {
  12. //
  13. }
  14. }
  15. stage('Deploy') {
  16. steps {
  17. //
  18. }
  19. }
  20. }
  21. }

03 Docker CICD 部分 - 图24

:::warning

  1. 在任何可用的代理上,执行流水线或它的任何阶段。
  2. 定义“Build”阶段。
  3. 执行与”Build”阶段相关的步骤。
  4. 定义”Test”阶段。
  5. 执行与”Test”阶段相关的步骤。
  6. 定义“Deploy”阶段。
  7. 执行与”Deploy”阶段相关的步骤。

:::

03 Docker CICD 部分 - 图25

11.8 CI/CD收益

高效的CI/CD环境可以获得:

  • 及时发现问题
  • 大幅度减少故障率
  • 加快迭代速度
  • 减少时间成本