Parameters, post and cron lab

Lab1: Create pipeline and build

pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')

booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')

choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')

password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"

echo "Biography: ${params.BIOGRAPHY}"

echo "Toggle: ${params.TOGGLE}"

echo "Choice: ${params.CHOICE}"

echo "Password: ${params.PASSWORD}"
}
}
}
}

Lab2:

 

// Declarative Pipeline
pipeline {
  agent any
 triggers {
 cron('* * * * *')
}
  stages {
   stage('Example') {
  steps {
     echo 'Hello World'
   }
  }
 }
}


Lab3: Post actions: The post section defines one or more additional steps that are run upon the completion of a Pipeline’s or stage’s run (depending on the location of the post section within the Pipeline). post can support any of the following postcondition blocks: alwayschangedfixedregressionabortedfailuresuccessunstableunsuccessful, and cleanup

pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
post {
always {
echo 'I will always say Hello again!'
}
}
}

Try: replace post– > always

post –>failure

post –> success  and provide echo statement

Try this code and provide observation:

pipeline {
agent any

 parameters {
  choice(
   name: 'ENVIRONMENT',
   choices: ['dev', 'qa', 'prod'],
   description: 'Select the deployment environment'
 )
}
stages {
 stage('Build') {
  steps {
// Add your build steps here
   echo "Building for ${params.ENVIRONMENT} environment"
  }
}

 stage('Deploy') {
  when {
   expression {
// You can use the 'params.ENVIRONMENT' variable to make decisions in your pipeline
   return params.ENVIRONMENT == 'qa' || params.ENVIRONMENT == 'prod'
  }
}
steps {
// Add your deployment steps here
  echo "Deploying to ${params.ENVIRONMENT} environment"
  }
}
stage('Test') {
  steps {
// Add your testing steps here
  echo "Testing in ${params.ENVIRONMENT} environment"
 }
}
}
post {
always {
// Add any cleanup or post-build steps here
echo "Pipeline completed for ${params.ENVIRONMENT} environment"
 }
 }
}

Lab5:

Parameterization: Create jenkins file in scm

pipeline {
    agent any
    parameters {
        string(name: ‘environment’, defaultValue: ‘Dev’, description: ‘Environment to build for (Valid values: Dev, Test, Prod)’)
string(name: ‘version’, defaultValue: ‘1.0’, description: ‘Version number to build for’)
booleanParam(name: ‘to_deploy_to_environment’, defaultValue: true, description: ”)
choice(choices: ‘US-EAST-1\nUS-WEST-2’, description: ‘What AWS region?’, name: ‘region’)
text(name:’myText’, defaultValue:’myTextValue’, description:’myText’)
password(name:’myPassword’, defaultValue:’myPasswordValue’, description:’myDescription’)
file(name:’myFile’, description:’fileDescription’)
credentials(name:’myCredentials’, description:’myCredentailsDesc’, required:true)
    }
    stages {
        stage(‘Example’) {
            steps {
                echo “We are building for ${params.environment}, ${params.version}, and we are deploying to environment: ${params.to_deploy_to_environment}”
echo “region:${params.region}, myText: ${params.myText}, myPassword: ${params.myPassword}, and myFile: ${params.myFile}”
echo “selected credentials is: ${params.myCredentials}”
            }
        }
    }
}