String workspace = "/opt/jenkins/workspace/${JOB_NAME}" def v = 'v' env.e1 = 'v1' pipeline { agent { node { label 'master' customWorkspace "${workspace}" } } tools { git 'git' jdk 'jdk-19' maven 'maven' } options { ansiColor('xterm') buildDiscarder logRotator(artifactDaysToKeepStr: '60', artifactNumToKeepStr: '5', daysToKeepStr: '60', numToKeepStr: '5') checkoutToSubdirectory '' disableConcurrentBuilds abortPrevious: true disableResume() parallelsAlwaysFailFast() preserveStashes buildCount: 3 quietPeriod 2 // unit: second retry 0 skipDefaultCheckout(true) skipStagesAfterUnstable() timestamps()pod timeout(time: 1, unit: 'HOURS') warnError('warnError') } parameters { booleanParam defaultValue: true, description: 'description for boolean parameter', name: 'booleanParameter' choice choices: ['defaultChoice', 'choice1', 'choice2'], description: 'description for choice parameter', name: 'choiceParameter' file description: 'description for file parameter', name: 'data.zip' string defaultValue: 'defaultStringParameter', description: 'description', name: 'stringParameter', trim: true } // triggers { // cron '''TZ=Europe/London // H 0-9/2,18-23/2 1-30 1,2,3,9-10 1-5''' // pollSCM '''TZ=Europe/London // H 0-9/2,18-23/2 1-30 1,2,3,9-10 1-5''' // upstream 'folder-a/esoteric' // upstream threshold: 'FAILURE', upstreamProjects: 'job-1, folder/job-2' // default to only stable // } environment { // 1. inside pipeline block e2 = 'v2' usernameWithPassword = credentials('08d73561-fb85-4796-b832-7a420b40f19c') // he environment variable specified will be set to username:password and two additional environment variables will be automatically defined: MYVARNAME_USR and MYVARNAME_PSW respectively. sshUsernameWithPrivatekey = credentials('f15a044c-c823-4119-8373-dc0c0d86963d') // the environment variable specified will be set to the location of the SSH key file that is temporarily created and two additional environment variables will be automatically defined: MYVARNAME_USR and MYVARNAME_PSW (holding the passphrase). secretText = credentials('baef090a-a4c3-4dae-a826-a5fef073a8fc') // the environment variable specified will be set to the Secret Text content secretFile = credentials('61467172-bb10-4ee4-a9a5-eb64d171f2ad') // the environment variable specified will be set to the location of the File file that is temporarily created } stages { stage('----------------------------------------Environment----------------------------------------') { input { message 'Input Message' ok 'Yes, we should.' submitterParameter 'submitter' parameters { string defaultValue: 'defaultInputStringParameter', description: 'description for input string parameter', name: 'inputStringParameter', trim: true } } environment { // 2. inside stage directive e3 = 'v3' } steps { cleanWs(notFailBuild: true) checkout scmGit(branches: [[name: 'refs/heads/mainn']], browser: github('https://github.com/annexationer/eschew'), extensions: [checkoutOption(5), cloneOption(depth: 1, honorRefspec: true, noTags: false, reference: '', shallow: true, timeout: 5), [$class: 'ScmName', name: 'IKEA'], buildSingleRevisionOnly()], userRemoteConfigs: [[credentialsId: 'f15a044c-c823-4119-8373-dc0c0d86963d', name: 'upstream', refspec: '+refs/heads/main:refs/remotes/upstream/mainn +refs/heads/dev:refs/remotes/upstream/devv', url: '[email protected]:annexationer/eschew.git']]) withEnv(["e1=${JENKINS_URL}", "e2=${JOB_URL}", "e3=${BUILD_URL}"]) { // can override any env variables sh ''' env | sort echo ${usernameWithPassword} > usernameWithPassword echo ${usernameWithPassword_USR} >> usernameWithPassword echo ${usernameWithPassword_PSW} >> usernameWithPassword echo ${sshUsernameWithPrivatekey} > sshUsernameWithPrivatekey echo ${sshUsernameWithPrivatekey_USR} >> sshUsernameWithPrivatekey echo ${sshUsernameWithPrivatekey_PSW} >> sshUsernameWithPrivatekey echo ${secretText} > secretText echo ${secretFile} > secretFile ls -ahl mvn --version java -version javac -version ''' } script { // should only in the steps env.e1 = "${JOB_NAME}" // folder-a/esoteric, override scriptted env env.e2 = "${JOB_BASE_NAME}" // esoteric, cannot override declarative env env.e3 = "${BUILD_ID}" // cannot override declarative env, but since e3 declare in this stage, following stage can access e3 def var1 = sh encoding: 'UTF-8', returnStdout: true, script: '''#!/usr/bin/env -vS bash ls -ahl''' var1 = var1.trim() echo ">> var1 = ${var1} <<" String var2 = "${env.WORKSPACE}" // ${JENKINS_HOME}/workspace/folder-a/esoteric var3 = "${env.GIT_LOCAL_BRANCH}" sh ''' env | sort ''' } } } stage('----------------------------------------Check----------------------------------------') { steps { timeout(time: 5, unit: 'MINUTES') { script { echo ">> var3 = ${var3} <<" sh ''' env | sort ls -ahl ''' config = readFile(encoding: 'Base64', file: '.git/config').trim() } } } } stage('----------------------------------------Build----------------------------------------') { steps { timeout(time: 20, unit: 'MINUTES') { script { if (env.GIT_COMMIT) { echo "GIT_COMMIT = ${GIT_COMMIT}" } echo "${config}" def exitStatus = sh encoding: 'UTF-8', returnStatus: true, script: 'command_not_exist' if (exitStatus != 0) { println '\033[7mcommand failed\033[0m' } echo "${currentBuild.getBuildCauses()}" currentBuild.getBuildCauses().each { println it } } } } } stage('----------------------------------------Deploy----------------------------------------') { agent { label "master" customWorkspace "${workspace}" } when { beforeAgent true beforeInput true beforeOptions true triggeredBy cause: "UserIdCause", detail: "jenkins" expression { env.JOB_BASE_NAME ==~ /(production|staging|esoteric)/ } expression { env.NODE_NAME != null } anyOf { branch 'main' environment name: 'CI', value: 'true' environment name: 'DEPLOY_TO', value: 'production' environment name: 'DEPLOY_TO', value: 'staging' } } steps { timeout(time: 20, unit: 'MINUTES') { script { if (env.GIT_BRANCH) { echo "GIT_BRANCH = ${GIT_BRANCH}" } } } } } stage('----------------------------------------Parallel----------------------------------------') { parallel { stage('----------------------------------------Parallel-1----------------------------------------') { steps { script { def browsers = ['chrome', 'firefox'] for ( int i = 0; i < browsers.size(); ++i) { println browsers[i] } browsers.add('edge') browsers.each { print it } // browsers.removeElement('edge') browsers -= 'edge' println browsers.join(', ') } } } stage('----------------------------------------Parallel-2----------------------------------------') { steps { // error(message: 'Error Signal') // no strack trace throw new Exception("Error Signal") script { def list1 = [2,1,3,4,5,6,76] list1.findAll { it > 3 } def list2 = ["Hello", "World"] } } } } } } post { always { script { println('post: always') if (env.WORKSPACE != null) { println "WORKSPACE = ${WORKSPACE}" } else { println 'WORKSPACE is null' } } } success { script { if (currentBuild.description) { currentBuild.description += 'post: success' } else { currentBuild.description = 'post: success' } } } failure { script { if (currentBuild.description) { currentBuild.description += 'post: failure' } else { currentBuild.description = 'post: failure' } } } aborted { script { if (currentBuild.description) { currentBuild.description += 'post: aborted' } else { currentBuild.description = 'post: aborted' } } } } }
标签:description,env,currentBuild,echo,environment,template,Jenkins,true From: https://www.cnblogs.com/dissipate/p/17350702.html