I have a Jenkinsfile
pipeline {
options {
ansiColor('xterm')
}
environment {
TF_FOLDER = ''
}
stages{
stage('Check for Changes') {
steps {
git(
url: 'https://github/org/repo.git',
credentialsId: 'repo-creds',
branch: "main"
)
script {
if (changeset("/dr/**")) {
TF_FOLDER = 'dr'
echo "Value of TF_FOLDER: ${TF_FOLDER}"
}
if (changeset("/nprod/**")) {
TF_FOLDER = 'nprod'
echo "Value of TF_FOLDER: ${TF_FOLDER}"
}
if (changeset("/prod/**")) {
TF_FOLDER = 'prod'
echo "Value of TF_FOLDER: ${TF_FOLDER}"
sh 'printenv'
}
else {
echo "No changes detected"
}
}
}
}
// Use TF_FOLDER in later stages
}
}
The root of my repo contains 3 folders
d----- 9/18/2024 4:33 PM dr
d----- 9/18/2024 4:33 PM nprod
d----- 9/18/2024 4:33 PM prod
When the pipeline runs, I want to first figure out which folder has changes and then assign that folder’s name to a variable called TF_FOLDER
When I made a change to any file under the prod
folder
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: prod/test-prod
The result of the pipeline is
[Pipeline] echo
Value of TF_FOLDER: dr
[Pipeline] echo
Value of TF_FOLDER: nprod
[Pipeline] echo
Value of TF_FOLDER: prod
Is there something Ive missed in my code ? Im expecting the pipeline to assign TF_FOLDER = prod
EDIT ::
I changed the ifs to if else
and now the result is
[Pipeline] echo
Value of TF_FOLDER: dr
[Pipeline] }
Although the change I made is in the prod folder. Not the dr folder