just wanna know how is Jenkinsfile used among multiple branches in a company , single Jenkinsfile for multiple branches with conditionals or any other way or plugins? i dont mind using multiple jenkinsfile but implementing it and merge would cause a overwrite i think
I have 3 main, dev , release and a feature branch which should be triggered based on pull-requests to dev.
pipeline {
agent any
environment {
BRANCH_NAME = env.BRANCH_NAME ?: 'unknown'
CHANGE_ID = env.CHANGE_ID ?: ''
}
stages {
stage('Initialize') {
steps {
echo "Building branch: ${BRANCH_NAME}"
echo "Pull Request ID: ${CHANGE_ID}"
}
}
stage('Build') {
when {
anyOf {
branch 'main'
branch 'dev'
expression { return env.CHANGE_ID != '' } // For pull requests
}
}
steps {
script {
if (BRANCH_NAME == 'main') {
echo "Building main branch"
// Main branch-specific build steps
sh 'echo Main branch build process'
} else if (BRANCH_NAME == 'dev') {
echo "Building dev branch"
// Dev branch-specific build steps
sh 'echo Dev branch build process'
} else if (env.CHANGE_ID != '') {
echo "Building feature branch for PR to dev"
// Feature branch-specific build steps
sh 'echo Feature branch build process'
}
}
}
}
stage('Test') {
when {
anyOf {
branch 'main'
branch 'dev'
expression { return env.CHANGE_ID != '' } // For pull requests
}
}
steps {
script {
if (BRANCH_NAME == 'main') {
echo "Testing main branch"
// Main branch-specific test steps
sh 'echo Main branch testing process'
} else if (BRANCH_NAME == 'dev') {
echo "Testing dev branch"
// Dev branch-specific test steps
sh 'echo Dev branch testing process'
} else if (env.CHANGE_ID != '') {
echo "Testing feature branch for PR to dev"
// Feature branch-specific test steps
sh 'echo Feature branch testing process'
}
}
}
}
stage('Deploy') {
when {
anyOf {
branch 'main'
branch 'dev'
expression { return env.CHANGE_ID != '' } // For pull requests
}
}
steps {
script {
if (BRANCH_NAME == 'main') {
echo "Deploying main branch"
// Main branch-specific deployment steps
sh 'echo Main branch deployment process'
} else if (BRANCH_NAME == 'dev') {
echo "Deploying dev branch"
// Dev branch-specific deployment steps
sh 'echo Dev branch deployment process'
} else if (env.CHANGE_ID != '') {
echo "Deploying feature branch for PR to dev"
// Feature branch-specific deployment steps
sh 'echo Feature branch deployment process'
}
}
}
}
}
post {
always {
echo 'Cleaning up...'
// Clean up actions, e.g., deleting temporary files
}
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed!'
}
}
}