共计 2540 个字符,预计需要花费 7 分钟才能阅读完成。

克隆代码片段
stage('Clone') {
    echo "1.Clone Stage"
    git url: "https://github.com/cnych/jenkins-demo.git"
}
构建产物
stage('Build') {
    echo "3.Build Docker Image Stage"
    sh "docker build -t xadocker.cn/jenkins-demo:${build_tag} ."
}
构建产物并附加仓库提交版本号
stage('Clone') {
    echo "1.Clone Stage"
    git url: "https://github.com/cnych/jenkins-demo.git"
    script {
        build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
    }
}
stage('Build') {
    echo "3.Build Docker Image Stage"
    sh "docker build -t qienda/jenkins-demo:${build_tag} ."
}
打包推送产物
stage('Push') {
    echo "4.Push Docker Image Stage"
    sh "docker login -u xadocker -p xxxxx"
    sh "docker push xadocker.cn/jenkins-demo:${build_tag}"
}
// 或者推荐以下方式
stage('Push') {
    echo "4.Push Docker Image Stage"
    withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPassword', usernameVariable: 'harborUser')]) {
        sh "docker login -u ${harborUser} -p ${harborPassword}"
        sh "docker push xadocker.cn/jenkins-demo:${build_tag}"
    }
}
等待输入
stage('Deploy') {
    input {
        message "Should we continue?"
    }
    steps {
        echo "Continuing with deployment"
    }
}
对输入增加超时
stage('Deploy') {
    options {
        timeout(time: 30, unit: 'SECONDS') 
    }
    input {
        message "Should we continue?"
    }
    steps {
        echo "Continuing with deployment"
    }
}
输入参数选项
stage('select') {
    echo "5. Change YAML File Stage"
    def userInput = input(
        id: 'userInput',
        message: 'Choose a deploy environment',
        parameters: [
            [
                $class: 'ChoiceParameterDefinition',
                choices: "Dev\nQA\nProd",
                name: 'Env'
            ]
        ]
    )
    echo "This is a deploy step to ${userInput.Env}"
    sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s.yaml"
    sh "sed -i 's/<BRANCH_NAME>/${env.BRANCH_NAME}/' k8s.yaml"
}
逻辑判断
stage('Deploy') {
    echo "6. Deploy Stage"
    if (userInput.Env == "Dev") {
      // deploy dev stuff
    } else if (userInput.Env == "QA"){
      // deploy qa stuff
    } else {
      // deploy prod stuff
    }
    sh "kubectl apply -f k8s.yaml"
}
script块获取命令输出
对于声明式pipeline,有一些逻辑处理语句可能不是很完善,没有脚本式那么丰富的处理方式,但是可以用script块来使用脚本式的语句
stage('Get JDK Version') {
    steps {
        script {
            try {
                JDK_VERSION = sh (script: "java --version | awk 'FNR==1'", returnStdout: true)
            } catch(err) {
                echo "CAUGHT ERROR: ${err}"
                throw err
            }
        }
    }
}
stage('Say JDK Version ') {
    steps {
        echo "${JDK_VERSION}"
    }
}
并发parallel
博主用得少,因为资源不足以支撑,资源充足得小伙伴可以用用,这种经常在多端多平台时编译或多cpu架构时用到,
1.同agent下的任务并发
stage('Build') {
    failFast true
    parallel {
        stage(’Branch UAT') {
            git url: "https://xxx.git",branch: "uat"
            steps {
                sh 'npm build:uat'
                sleep time: 10, unit: 'SECONDS'
            }
        }
        stage('Branch DEV') {
            git url: "https://xxx.git",branch: "dev"
            steps {
                sh 'cnpm i && cnpm build:dev'
                sleep time: 20, unit: 'SECONDS'
            }
        }
    }
}
2.不同agent下的并发
stage('Build') {
    failFast true
    parallel {
        stage('ARM') {
            agent { lable 'arm' }
            git url: "https://xxx.git",branch: "uat"
            steps {
                sh 'npm build:uat'
                sleep time: 10, unit: 'SECONDS'
            }
        }
        stage('x86') {
            agent { lable 'x86' }
            git url: "https://xxx.git",branch: "uat"
            steps {
                sh 'cnpm i && cnpm build:uat'
                sleep time: 20, unit: 'SECONDS'
            }
        }
    }
}
正文完
  
 
 隐私政策
 留言板
 金色传说
 kubernetes
 terraform
 云生原
 helm
 代码编程
 Java
 Python
 Shell
 DevOps
 Ansible
 Gitlab
 Jenkins
 运维
 老司机
 Linux 杂锦
 Nginx
 数据库
 elasticsearch
 监控
 上帝视角
 DJI FPV
 DJI mini 3 pro
 关于本站