Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Jenkins] Scripted Pipeline lesson 4: Stage _ When

Posted on December 1, 2021December 16, 2021 By nim No Comments on [Jenkins] Scripted Pipeline lesson 4: Stage _ When

ở jenkins pipeline thì mình cũng có giới thiệu về when

[Jenkins] Lesson 5: Condition “When” in Pipeline Jenkins

Contents

Toggle
  • 1) Condition
  • 2) Branch
  • 3) building Tag and tag
  • 4) changelog – commit
  • 5) changeRequest – Pull request
  • 6) changeSet – Type file

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

https://github.com/mrnim94/scripted-when-branch.git

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")
		}
		
	}
}
sau khi save jenkín sẽ discover tag of github

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

Ở lần đầu tiên thì anh em chưa commit gì cả.

Giờ thực hiện commit.

giờ mình thực hiện commit
Giờ ấn build sem sao

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

TÌm hiểu trong Jenkinsfile có j?
Sau đó bạn nhân build ở branch master.
thì sẽ nhận được các thông như trên toàn null
Bạn edit file normal.txt ở branch dev
Xong tạo pull request để merge dev -> master với title là when-pr
trợ laị jenkins và ấn scan
GIờ thì anh em đã thấy hiện đầy đủ và đọc title(tiêu đề của pull request)

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'
    }
  }
}
không thấy j cả
change file js
nó đã found
vậy change file .js thì đã found

Còn các bạn thì change file còn lại sem có j đặc biệt hem nhé

Jenkins

Post navigation

Previous Post: [GRPC] BloomRPC aims to provide the simplest and most efficient developer
Next Post: [istio] Discover route TCP in Cluster Istio

More Related Articles

[Jenkins/Git] Why do My Jenkins get env.BRANCH_NAME of Git that is Null Git
[Jenkins] Scripted Pipeline lesson 6: Stage _ Input Jenkins
[Jenkins] Share Libraries 8: Loading Resources Jenkins
[jenkins] Scripted Pipeline lesson 2: Docker Jenkins
[Jenkins] Lesson 17: create Cron Job via Jenkins Pipeline Jenkins
[Jenkins]Scripted Pipeline lesson 15: Parameters. Jenkins

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Laravel] Laravel Helpful June 26, 2025
  • [VScode] Hướng dẫn điều chỉnh font cho terminal June 20, 2025
  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025
  • [Teamcity] How to transfer the value from pipeline A to pipeline B June 9, 2025

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.