Last active
November 9, 2024 11:56
-
-
Save davesave/5613dd33cf86ac1a6356d2dd4ccf6d2a to your computer and use it in GitHub Desktop.
Mulitbranch pipeline example with loading external groovy pipelines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deployProjectToEnvStage(String env_name) { | |
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { | |
stage("Deploy to ${env_name}") { | |
echo 'Running Deploy stage in cd.groovy...' | |
sh "echo 'Deploying project to ${env_name}'" | |
script { | |
if ("${env_name}" == 'dev1') { | |
sh 'sleep 1' | |
} | |
if ("${env_name}" == 'dev2') { | |
sh 'sleep 2' | |
error 'chaos' | |
} | |
if ("${env_name}" == 'dev3') { | |
sh 'sleep 3' | |
} | |
} | |
} | |
} | |
} | |
// A function to run all stages together | |
def runPipeline() { | |
deployProjectToEnvStage('dev1') | |
deployProjectToEnvStage('dev2') | |
deployProjectToEnvStage('dev3') | |
} | |
return this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def buildStage() { | |
stage('Build') { | |
container('maven') { | |
echo 'Running Maven Build stage in ci.groovy...' | |
sh 'echo "Building project..."' | |
sh 'mvn clean install' | |
} | |
} | |
} | |
def testStage() { | |
stage('Unit Tests') { | |
echo 'Running Unit Test stage in ci.groovy...' | |
sh 'echo "Running tests..."' | |
} | |
} | |
def dockerStage() { | |
stage('Docker build') { | |
container('dind') { //todo: add to podTemplate (replace with kaniko later) | |
echo 'Running Docker build stage in ci.groovy...' | |
sh 'echo "Running docker..."' | |
} | |
} | |
} | |
// A function to run all stages together | |
def runPipeline() { | |
buildStage() | |
testStage() | |
dockerStage() | |
} | |
return this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent { | |
kubernetes { | |
idleMinutes 2 | |
yaml ''' | |
metadata: | |
labels: | |
some-label: some-label-value | |
spec: | |
containers: | |
- name: jnlp | |
env: | |
- name: CONTAINER_ENV_VAR | |
value: jnlp | |
- name: maven | |
image: maven:3.9.9-eclipse-temurin-21-alpine | |
command: | |
- cat | |
tty: true | |
''' | |
} | |
} | |
options { | |
buildDiscarder(logRotator(numToKeepStr: '10')) | |
timeout(time: 1, unit: 'HOURS') | |
skipDefaultCheckout() | |
timestamps() | |
} | |
stages { | |
stage('SCM') { | |
steps { | |
checkout scm // multibranch pipelines | |
} | |
} | |
stage('Setup') { | |
steps { | |
script { | |
def ci | |
def cd | |
// CHECKOUT JENKINS GROOVY FILES: | |
dir('.jenkins') { | |
checkout([ | |
$class: 'GitSCM', | |
branches: [[name: 'main']], | |
userRemoteConfigs: [[ | |
url: '........................................', | |
credentialsId: '...............................' | |
]] | |
]) | |
ci = load 'ci.groovy' | |
cd = load 'cd.groovy' | |
} | |
ci.runPipeline() | |
cd.runPipeline() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment