ở jenkins pipeline thì mình cũng có giới thiệu về when
1) Condition
node{
def name = "nimtechnology"
def isGroovyCool = false
withEnv(['DEPLOY_TO=production']){
stage('build'){
//when-environment condition
if(env.DEPLOY_TO == 'production')
println "Deploying"
//when-equals condition
if(name == 'nimtechnology')
println "name is ${name}"
//when-expression condition
if(isGroovyCool == false)
println "name is ${name} and groovy is cool"
//when-not condition
if(name == 'john')
println "name isn't ${john}"
//when-allOf condition
if(name == 'nimtechnology' && !isGroovyCool)
println "name is ${name} and groovy is cool"
//when-anyOf condition
if(name == 'nimtechnology' && !isGroovyCool)
println "name is ${name} and groovy is cool"
}
}
}
2) Branch
3) building Tag and tag
node{
stage("Build"){
if(env.TAG_NAME != null){
println(" we are building a tag and tag is ${env.TAG_NAME}")
}
else{
println(" we are building a branch")
}
if(env.TAG_NAME == "release-1.0"){
println(" we are building specifically release-1.0 tag")
}
}
}
4) changelog – commit
không giống như bài changelog ở Jenkins Pipeline
https://nimtechnology.com/2021/10/30/jenkins-lesson-5-condition-when-in-pipeline-jenkins/#changelog_8211_commit
Để lấy được changeLog chú ta cần dùng changeSets.
changeSets
a list of changesets coming from distinct SCM checkouts; each has a kind
and is a list of commits; each commit has a commitId
, timestamp
, msg
, author
, and affectedFiles
each of which has an editType
and path
; the value will not generally be Serializable
so you may only access it inside a method marked @NonCPS
changeSets
là tập hợp của 1 list changsets
==> mỗi changeset có 1 kind và 1 list commits
==> mỗi commit thì chúng ta sẽ có nhiều thông tin về: commitId
, timestamp
, msg
, author
, and affectedFiles
,…
https://stackoverflow.com/questions/64044654/how-to-get-changeset-list-of-current-build-in-jenkins
Theo như link trên:
For your use case, you can use Jenkins global variables. To retrieve the changed files from current build you can use this one:
currentBuild.changeSets
To do the same but for the previous build:
currentBuild.previousBuild.changeSets
The full reference is here.
node{
checkout([
$class: 'GitSCM',
branches: [[name: "origin/master"]],
userRemoteConfigs: [[
url: 'https://github.com/mrnim94/scripted-when-changelog.git'
]]
])
def changeLogFound = false
def changeLogSet = currentBuild.changeSets
println "changeLogSet.size(): ${changeLogSet.size()}"
for(int i = 0; i < changeLogSet.size(); i++) {
println changeLogSet[i].getClass().getName()
def entries = changeLogSet[i].items
for(int j = 0; j < entries.length; j++) {
def entry = entries[j]
println "${entry.msg}"
if(entry.msg =~ /.*some_text.*/){
changeLogFound = true
break;
}
}
if(changeLogFound)
break
}
changeLogSet = null
if(changeLogFound){
stage('changeLogFound'){
echo 'change log found'
}
}
else{
stage('changeLogNotFound'){
echo 'change log not found'
}
}
}
Ở trên thì mình thấy được build hiện tại. build hiện tại này có sự thay đổi so với build trước hay ko?def changeLogSet = currentBuild.changeSets
Nếu currentBuild.changeSets
có sự thay đổi nghĩ là chúng ta đã commit thì changeLogSet.size()
sẽ lớn hơn 0
==> từ đây mình đọc nội dung commit
Giờ thực hiện commit.
Giờ chúng ta ko commit j cả và ấn luôn.
5) changeRequest – Pull request
https://github.com/mrnim94/scripted-when-changeRequest.git
Bạn sẽ tạo 1 pipeline multi-branch
6) changeSet – Type file
node{
checkout([
$class: 'GitSCM',
branches: [[name: "origin/master"]],
userRemoteConfigs: [[
url: 'https://github.com/mrnim94/scripted-when-changeset.git'
]]
])
def changeSetFound = false
def changeLogSet = currentBuild.changeSets
println "changeLogSet.size(): ${changeLogSet.size()}"
for(int i = 0; i < changeLogSet.size(); i++) {
println changeLogSet[i].getClass().getName()
def entries = changeLogSet[i].items
for(int j = 0; j < entries.length; j++) {
def entry = entries[j]
def files = new ArrayList(entry.affectedFiles)
for(int k = 0; k < files.size(); k ++){
def file = files[k]
if(file.path.endsWith("js")){
changeSetFound = true
break;
}
}
if(changeSetFound)
break;
}
}
changeLogSet = null
echo "changeSetFound: ${changeSetFound}"
if(changeSetFound){
stage('changeSetFound'){
echo 'change set found'
}
}
else{
stage('changeSetNotFound'){
echo 'change set not found'
}
}
}
Còn các bạn thì change file còn lại sem có j đặc biệt hem nhé