写点什么

jenkins pipline 基本语法

作者:忙着长大#
  • 2022-11-20
    北京
  • 本文字数:1582 字

    阅读完需:约 5 分钟

#pipline 固定开头格式

pipeline {

//agent any  //全局必须带有 agent,表明此 pipeline 执行节点,指定执行任务的节点,any 标识随机分配

#agent { label 'jenkins-node1' } //基于 label 指定具体执行的步骤节点,非必须

agent any

environment { //全局的变量,在当前 pipline 所有的 stage 中都会生效

NAME='user1'

PASSWD='123456'

}

stages {

stage('环境变量 stage1') {

environment { //定义在 stage 中的变量只会在当前 stage 生效,其他的 stage 不会生效

GIT_SERVER = 'git@192.168.131.130:my-app1/app1-1.git'

}

steps {

sh """

echo '$NAME'

echo '$PASSWD'

echo '$GIT_SERVER'

"""

}

}

stage('环境变量 stage2') {

steps {

sh """

echo '${NAME}'

echo '$PASSWD'

"""

}

}

}

}


pipeline {

  agent any

  stages {

    stage('交互测试') {

      input {

        message "是否继续部署?"

        ok "继续部署"

        submitter "jenkinsadmin" # 指定用户才可以点击继续部署

      }

      steps {

        echo "Hello jenkins!"

      }

    }

  }

}


pipeline {  agent any  stages {    stage('post测试-代码clone阶段') {        steps {            sh 'echo git clone'            sh 'cd /data/xxx'  //此步骤会执行失败,用于验证构建失败的邮件通知        }        post {            cleanup {	        		script {	              mail to: 'xxxxxxxxxx@qq.com',                  subject: "Pipeline Name: ${currentBuild.fullDisplayName}",                  body: " ${env.JOB_NAME} -Build Number-${env.BUILD_NUMBER} -cleanup 构建失败!\n 点击链接 ${env.BUILD_URL} 查看详情"	            }            }	        always {	        		script {	              mail to: 'xxxxxxxxxx@qq.com',                  subject: "Pipeline Name: ${currentBuild.fullDisplayName}",                  body: " ${env.JOB_NAME} -Build Number-${env.BUILD_NUMBER} -always 构建失败!\n 点击链接 ${env.BUILD_URL} 查看详情"	            }            }	        aborted {	        		echo "post aborted"	            }	        success {	        	script {	            mail to: 'xxxxxxxxxx@qq.com',                    subject: "Pipeline Name: ${currentBuild.fullDisplayName}",                    body: " ${env.JOB_NAME} -Build Number-${env.BUILD_NUMBER} - 构建成功!\n 点击链接 ${env.BUILD_URL} 查看详情"	            }            }        	failure {	        	script {	            mail to: 'xxxxxxxxxx@qq.com',                    subject: "Pipeline Name: ${currentBuild.fullDisplayName}",                    body: " ${env.JOB_NAME} -Build Number-${env.BUILD_NUMBER} -failure 构建失败!\n 点击链接 ${env.BUILD_URL} 查看详情"	            }            }
} } }}
复制代码


####

pipeline {

  agent any

  parameters {

    string(name: 'BRANCH', defaultValue:  'develop', description: '分支选择')   //字符串参数,会配置在 jenkins 的参数化构建过程中

    choice(name: 'DEPLOY_ENV', choices: ['develop', 'production'], description: '部署环境选择')  //选项参数,会配置在 jenkins 的参数化构建过程中

  }

  stages {

    stage('测试参数 1') {

      steps {

        sh "echo $BRANCH"

      }

    }

    stage('测试参数 2') {

      steps {

        sh "echo $DEPLOY_ENV"

      }

    }

  }

}

用户头像

忙着长大#

关注

还未添加个人签名 2022-02-09 加入

还未添加个人简介

评论

发布
暂无评论
jenkins pipline 基本语法_忙着长大#_InfoQ写作社区