Skip to content

Instantly share code, notes, and snippets.

@anilnautiyal
Created May 5, 2020 06:08
Show Gist options
  • Save anilnautiyal/7c03a8889e04f8b7aee6858022bca80a to your computer and use it in GitHub Desktop.
Save anilnautiyal/7c03a8889e04f8b7aee6858022bca80a to your computer and use it in GitHub Desktop.
Jenkins YAML file
pipeline {
agent 'any'
options {
skipStagesAfterUnstable()
}
environment {
exampleEnvType = 'dev'
//you can decalare more global env variable here as above, if not needed remove the environment block
}
stages {
// Build and test Stage
// using * run test for all
stage('BuildAndTest') {
when {
branch '*'
}
steps {
echo 'write your test case here'
}
}
// Build Development
stage('BuildDevelopment') {
when {
branch 'dev'
}
environment {
//you can declare local env variable here
username = 'test'
ip = '10.0.0.1'
// also can be decalaed via jenkins dashboard under global variable
}
steps {
sh """
ssh ${env.username}@${env.username}
ls -al
# can mentioned whatever you need
"""
}
}
// Build Staging
stage('BuildStging') {
when {
branch 'staging'
}
environment {
username = 'test1'
ip '10.0.0.2'
}
steps {
// Staging Build
sh """
ssh ${env.username}@${env.ip}
# more steps what is needed can be given
"""
}
}
// Production build
stage('BuildProduction') {
when {
branch 'master'
}
environment {
username = 'prod'
ip = '10.0.0.3'
}
steps {
sh """
ssh ${env.username}@${env.ip}
"""
}
}
// Deploy Development
stage('DeployDev') {
when {
branch 'dev'
}
environment {
//decalare env here
}
steps {
echo 'Deploy steps goes here'
sh """
# can write rsync command
"""
}
}
// Deploy Staging
stage('DeployStaging') {
when {
branch 'staging'
}
environment {
//decalare non sensitive env if needed
}
steps {
// Deploy staging
sh """
//deploy staging steps goes here
"""
}
}
// Production Deployment
stage('DeployProduction') {
when {
branch 'master'
}
environment {
//declare envs for production if needed any
}
steps {
sh """
// production deploy steps goes here
"""
}
}
}
// post section send email to all whoever is commiting to git
// it uses email extended plugin, which requires to configure smtp also
// post {
// failure {
// emailext attachLog: true,
// body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}",
// //to: "${Jenkins_Admin}",
// //recipientProviders: [developers(), requestor()],
// recipientProviders: [[$class: 'DevelopersRecipientProvider']],
// subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
// }
// success {
// emailext attachLog: false,
// body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}",
// //to: "${Jenkins_Admin}",
// //recipientProviders: [developers(), requestor()],
// recipientProviders: [[$class: 'DevelopersRecipientProvider']],
// subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
// }
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment