在《快速搭建Jenkins集羣》一文中,咱們藉助docker快速搭建了Jenkins集羣,今天就在這個集羣環境中建立pipeline任務,體驗Jenkins集羣下的並行任務;java
整個集羣環境一共有三臺電腦,信息以下:git
主機名 | IP地址 | 做用 |
---|---|---|
master | 192.168.133.131 | Jenkins集羣的master節點,提供web服務 |
agent1 | 192.168.133.132 | Jenkins集羣的一號工做接節點,標籤是<font color="blue">maven</font> |
agent2 | 192.168.133.133 | Jenkins集羣的二號工做接節點,標籤是<font color="blue">gradle</font> |
本次實戰要體驗Jenkins集羣同時執行兩個任務,都是編譯構建GitHub上的熱門開源項目:程序員
要作如下準備工做才能順利執行後續的任務:github
docker exec -it agent /bin/bash
[root@agent1 16]# docker exec -it agent /bin/bash root@82eb8cfec0a6:/# ssh root@192.168.133.131 The authenticity of host '192.168.133.131 (192.168.133.131)' can't be established. ECDSA key fingerprint is SHA256:DPE2nldWHiOhC4DB9doy7jPWNZVup6XFZ+sR2i1gqz8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.133.131' (ECDSA) to the list of known hosts. root@192.168.133.131's password: Last login: Sat Nov 16 19:59:42 2019 from 192.168.133.132
至此,準備完畢,能夠建立任務了;web
spring-cloud-alibaba是個maven工程,要用maven來編譯構建:spring
pipeline { agent { label 'maven' } tools { maven 'mvn-3.6.2' } stages { stage('Checkout') { steps { echo '從GitHub下載spring-cloud-alibaba工程的源碼(2.1.1.RELEASE歸檔包)' sh 'wget https://github.com/alibaba/spring-cloud-alibaba/archive/v2.1.1.RELEASE.tar.gz' echo '下載結束,解壓歸檔包' sh 'tar -zxf v2.1.1.RELEASE.tar.gz' } } stage('Build') { steps { echo '開始編譯構建' sh 'cd spring-cloud-alibaba-2.1.1.RELEASE && mvn clean package -U -DskipTests' } } stage('Save') { steps { echo '將構建結果傳送到存儲服務器' sh 'cd spring-cloud-alibaba-2.1.1.RELEASE/spring-cloud-alibaba-nacos-discovery/target && sshpass -p 888888 scp ./*.jar root@192.168.133.131:/usr/local/build_result' echo '傳送完畢' } } stage('Clean') { steps { echo '清理Maven工程' sh 'cd spring-cloud-alibaba-2.1.1.RELEASE && mvn clean' echo '清理完畢' } } } }
編譯構建spring-cloud-alibaba工程是個費時的操做,咱們如今去建立另外一個任務:編譯構建spring-frameworkdocker
spring-framework是個gradle工程,不須要準備gradle環境,在執行編譯命令時會自動下載gradle工具:shell
pipeline { agent { label 'gradle' } stages { stage('Checkout') { steps { echo '從GitHub下載spring-framework工程的源碼(master分支)' checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/spring-projects/spring-framework.git']]]) } } stage('Build') { steps { echo '開始編譯構建' sh './gradlew build' } } stage('傳送構建結果') { steps { echo '將構建結果傳送到存儲服務器' sh 'cd spring-core/build/libs && sshpass -p 888888 scp ./*.jar root@192.168.133.131:/usr/local/build_result && cd ../../..' echo '傳送完畢' } } stage('Clean') { steps { echo '清理gradle工程' sh './gradlew clean' echo '清理完畢' } } } }
上述兩個任務在構建成功後,都會經過scp命令將構建結果傳送到master的<font color="blue">/usr/local/build_result</font>目錄下:segmentfault
[root@master build_result]# ls agent.jar spring-cloud-alibaba-nacos-discovery-2.1.1.RELEASE.jar spring-cloud-alibaba-nacos-discovery-2.1.1.RELEASE-sources.jar spring-objenesis-repack-3.1.jar spring-cglib-repack-3.3.0.jar spring-cloud-alibaba-nacos-discovery-2.1.1.RELEASE-javadoc.jar spring-core-5.2.2.BUILD-SNAPSHOT.jar
至此,Jenkins集羣下的pipeline實戰就完成了,藉助pipeline的標籤參數能夠控制任務的調度節點,實現多任務同時在多節點執行;bash