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
<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"
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 ) } } }