从0开始构建一条完成的CI CD流水线
tools { maven 'apache-maven-3.8.6' }
docker pull jenkins/agent:latest
wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
- central:中央仓库,是由Maven社区提供的资源仓库,它包含了大量的常用程序库组件(jar包)。默认Maven的中央仓库地址为:http://repo1.maven.org/maven2/
- local:本地仓库,是存放maven环境本地的一个文件夹,此文件夹在第一次运行Maven命令时就创建了。Maven在执行构建任务时,根据依赖关系从中心仓库、或远程仓库下载依赖组件到本地仓库,然后本地仓库的内容供项目引用。
- remote:远程仓库,例如项目需要指定外部其他公司、或开源组织的jar包,这些依赖组件通用性等原因,未纳入Maven中央仓库,这个时候就要手动指定一个私有的远程仓库来拉取依赖。
<mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
<repositories> <repository> <id>springsource-repos</id> <name>SpringSource Repository</name> <url>http://repo.spring.io/release/</url> </repository> </repositories>
mvn clean install -Dmaven.repo.local=/home/maven/local_repo/
mvn clean install -s /home/maven/settings.xml
[root@node01 agent-jenkins]# ls apache-maven-3.8.6.tar.gz Dockerfile jenkins-agent kubectl.tar.gz
[root@node01 agent-jenkins]# cat Dockerfile FROM jenkins/agent:latest USER root ADD apache-maven-3.8.6.tar.gz /opt/ ADD kubectl.tar.gz /usr/local/bin/ ENV PATH $PATH:/opt/apache-maven-3.8.6/bin/ COPY jenkins-agent /usr/local/bin/ CMD ["/bin/sh","-c","/usr/local/bin/jenkins-agent"]
# docker build -t registry.example.com:5000/jenkins/agent:v1 . # docker push registry.example.com:5000/jenkins/agent:v1
apiVersion: "v1"
kind: "Pod"
metadata:
name: jenkins-agent
namespace: "default"
spec:
containers:
- env:
- name: "MAVEN_HOME"
value: "/opt/apache-maven-3.8.6/"
image: "registry.example.com:5000/jenkins/agent:v1"
imagePullPolicy: "IfNotPresent"
name: "jnlp"
resources:
limits:
memory: "2G"
cpu: "1500m"
requests:
memory: "1G"
cpu: "100m"
volumeMounts:
- mountPath: "/root/.m2"
name: "m2"
readOnly: false
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
- mountPath: "/usr/bin/docker"
name: "docker-client"
readOnly: true
- mountPath: "/var/run/docker.sock"
name: "docker-engine"
readOnly: true
volumes:
- hostPath:
path: "/root/.m2"
type: "DirectoryOrCreate"
name: "m2"
- hostPath:
path: "/home/jenkins"
name: "workspace-volume"
- hostPath:
path: "/usr/bin/docker"
type: File
name: "docker-client"
- hostPath:
path: "/var/run/docker.sock"
type: Socket
name: "docker-engine"
- m2:这个是用作maven的本地仓库路径,使用hostpath挂载到了本地目录,当然也可以存储到某些共享存储中,目的就是让依赖包只下载一次。
- workspace-volume:这个是将jenkins的工作目录也使用hostpath挂载。
- docker-client:docker命令的挂载,用于build、push等命令
- docker-engine:docker engine的挂载,用于build、push等
- Git
- Git Parameter
- DingTalk
- build user vars plugin
pipeline { agent { kubernetes { cloud 'kubernetes-internal' //指定cloud name inheritFrom 'jenkins-agent' //指定podTemplate,新版本已经不再用label指定 namespace 'default' } } environment { GIT_CERT = credentials('vfan-gitlab') //gitlab用户凭证 HARBOR_HOST = 'registry.example.com:5000' SERVER_NAME = 'simple-java-maven-app' } /* tools { maven 'apache-maven-3.8.6' 镜像有maven环境了,可以不指定 } */ options { buildDiscarder(logRotator(numToKeepStr: '10')) //保持历史构建的最大个数 timeout(20) //默认单位分钟,20分钟 timestamps() //Pipeline开始时间以及每个step执行开始时间 } parameters { choice( name: 'GIT_REPO_URL', choices: 'http://10.85.122.128:880/vfan/simple-java-maven-app.git', description: 'Git Repo example environment' ) choice( name: 'GIT_TYPE', choices: ['branch', 'tag'], description: 'Git Repo example brance' ) choice( name: 'GIT_REPO_BRANCE', choices: ['master', 'dev', 'test'], description: 'Git Repo example brance' ) gitParameter name: 'GIT_TAG', type: 'PT_TAG', branch: 'master', branchFilter: '.*', defaultValue: '', selectedValue: 'TOP', sortMode: 'DESCENDING_SMART', listSize: '1', description: 'Select you git tag.' choice( name: 'ENVIRONMENT', choices: ['INT', 'DEV', 'PROD'], description: 'Select deployment environment' ) } stages { stage('git clone branch') { when { expression { params.GIT_TYPE == "branch" } } steps { git( branch: params.GIT_REPO_BRANCE, credentialsId: env.GIT_CERT, url: params.GIT_REPO_URL ) } post { success { sh ''' echo "use branch build" git status ''' } } } stage('git clone tag') { when { expression { params.GIT_TYPE == "tag" } } steps { checkout([$class: 'GitSCM', branches: [[name: "${GIT_TAG}"]], userRemoteConfigs: [[credentialsId: env.GIT_CERT, url: params.GIT_REPO_URL]]]) } post { success { sh ''' echo "use tag build" git status ''' } } } stage('Maven Build') { steps { sh 'mvn -B -DskipTests clean package' } } stage('Test') { steps { sh 'mvn test' } post { always { junit 'target/surefire-reports/*.xml' } } } stage('Deliver') { steps { sh './jenkins/scripts/deliver.sh' } } stage('Docker build && push') { steps { withCredentials([usernamePassword(credentialsId: 'harbor-auth', passwordVariable: 'HARBOR_PASSWD', usernameVariable: 'HARBOR_USER')]) { sh ''' echo "Other operations..." echo "Start building..." date -d "+8 hour" +%Y%m%d_%H%M%S > /tmp/date BUILD_TIME=`cat /tmp/date` docker build --build-arg APP_NAME=simple-java-maven-app -t ${HARBOR_HOST}/${SERVER_NAME}:${GIT_REPO_BRANCE}_${BUILD_TIME} . echo "Build complete." docker login $HARBOR_HOST -u $HARBOR_USER -p $HARBOR_PASSWD docker push ${HARBOR_HOST}/${SERVER_NAME}:${GIT_REPO_BRANCE}_${BUILD_TIME} docker rmi ${HARBOR_HOST}/${SERVER_NAME}:${GIT_REPO_BRANCE}_${BUILD_TIME} ''' } } } stage('Deploy to k8s'){ input{ message "Should we continue deploy?" ok "Yes, we should." } environment { // 提前创建好secret file类型的凭据 KUBE_CONFIG_INT = credentials('mycluster_int') // KUBE_CONFIG_DEV = credentials('mycluster_dev') // KUBE_CONFIG_PROD = credentials('mycluster_prod') } steps{ sh''' BUILD_TIME=`cat /tmp/date` case $ENVIRONMENT in "INT") kubectl set image deployment ${SERVER_NAME} --kubeconfig=${KUBE_CONFIG_INT} app=${HARBOR_HOST}/${SERVER_NAME}:${GIT_REPO_BRANCE}_${BUILD_TIME} kubectl rollout status deployment ${SERVER_NAME} --kubeconfig=${KUBE_CONFIG_INT} ;; "DEV") kubectl set image deployment ${SERVER_NAME} --kubeconfig=${KUBE_CONFIG_DEV} app=${HARBOR_HOST}/${SERVER_NAME}:${GIT_REPO_BRANCE}_${BUILD_TIME} kubectl rollout status deployment ${SERVER_NAME} --kubeconfig=${KUBE_CONFIG_DEV} ;; esac echo "Deployment complete." ''' } } } post { success{ echo 'Deployment succeeded.' dingtalk ( robot: 'myapp-dingding-robot', type: 'MARKDOWN', // 发什么类型的消息,有TEXT、LINK、MARKDOWN、和ACTION_CARD,参考https://jenkinsci.github.io/dingtalk-plugin/guide/pipeline.html at: [], atAll: false, title: 'Jenkins发版成功', text: [ "## 构建结果:**${currentBuild.result}**", '---', "## 构建信息", '---', "- 项目名称:${SERVER_NAME}", "- 构建环境:${ENVIRONMENT}", "- 构建分支:${GIT_REPO_BRANCE}", "- 构建标签:${GIT_TAG}", "- 项目地址:${GIT_REPO_URL}", "- 构建用户:${env.BUILD_USER}" ], // messageUrl: '', // picUrl: '', // singleTitle: '', // btns: [], // btnLayout: '', // hideAvatar: false ) } failure{ echo "Deployment failed." dingtalk ( robot: 'myapp-dingding-robot', type: 'MARKDOWN', // 发什么类型的消息,有TEXT、LINK、MARKDOWN、和ACTION_CARD,参考https://jenkinsci.github.io/dingtalk-plugin/guide/pipeline.html at: [], atAll: false, title: 'Jenkins发版失败', text: [ "## 构建结果:**${currentBuild.result}**", '---', "## 构建信息", '---', "- 项目名称:${SERVER_NAME}", "- 构建环境:${ENVIRONMENT}", "- 构建分支:${GIT_REPO_BRANCE}", "- 构建标签:${GIT_TAG}", "- 项目地址:${GIT_REPO_URL}", "- 构建用户:${env.BUILD_USER}" ], // messageUrl: '', // picUrl: '', // singleTitle: '', // btns: [], // btnLayout: '', // hideAvatar: false ) } } }
文章来源地址https://www.toymoban.com/news/detail-419448.html
文章来源:https://www.toymoban.com/news/detail-419448.html
到了这里,关于devops-5:从0开始构建一条完成的CI CD流水线的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!