pipeline-流水線模板


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..."
                }
            }
        }
    }
}
模板二


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM