node ("docker") 自定义节点 系统管理--> 节点管理
一、模板
# credentialsId 账号密码id 凭据中查看添加 # url gitlab地址 node ("docker") { // 指定Slave标签 // 拉取代码 stage('Git Checkout') { checkout([$class: 'GitSCM', branches: [[name: '$Branch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId:"d1968253-a99c-4b4c-b877-1617aa164181",url: 'http://192.168.56.15/root/java-demo.git']]]) } // 代码编译 stage('Maven Build') { sh ''' export JAVA_HOME=/var/lib/jdk1.8.0_60 /var/lib/apache-maven-3.5.0/bin/mvn clean package -Dmaven.test.skip=true ''' } // 项目打包到镜像并推送到镜像仓库 stage('Build and Push Image') { sh ''' REPOSITORY=192.168.56.11/project/java-demo:${Branch} cat > Dockerfile << EOF FROM 192.168.56.11/library/tomcat:v1 RUN rm -rf /usr/local/tomcat/webapps/ROOT COPY target/*.war /usr/local/tomcat/webapps/ROOT.war CMD ["catalina.sh", "run"] EOF docker build -t $REPOSITORY . docker login -u zjy -p 123456A.com 192.168.56.11 docker push $REPOSITORY ''' } // 部署到Docker主机 stage('Deploy to Docker') { sh ''' REPOSITORY=192.168.56.11/project/java-demo:${Branch} docker rm -f java-demo |true docker image rm $REPOSITORY |true docker login -u zjy -p 123456A.com 192.168.56.11 docker run -d --name java-demo -p 88:8080 $REPOSITORY ''' } }模板一
二、模板
#!groovy pipeline { agent {node {label 'master'}} environment { PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin" } stages { stage("Checkout test repo") { steps{ sh 'git config --global http.sslVerify false' dir ("${env.WORKSPACE}") { git branch: 'master', credentialsId:"d1968253-a99c-4b4c-b877-1617aa164181", url: 'http://192.168.56.11/root/test-repo.git' } } } stage("Print env variable") { steps { dir ("${env.WORKSPACE}") { sh """ echo "[INFO] Print env variable" echo "Current deployment environment is $deploy_env" >> test.properties echo "The build is $version" >> test.properties echo "[INFO] Done..." """ } } } stage("Check test properties") { steps{ dir ("${env.WORKSPACE}") { sh """ echo "[INFO] Check test properties" if [ -s test.properties ] then cat test.properties echo "[INFO] Done..." else echo "test.properties is empty" fi """ echo "[INFO] Build finished..." } } } } }模板二