当前位置:网站首页>pipeline groovy

pipeline groovy

2022-06-24 12:23:00 Chen Bucheng I

One . Variable

1. Direct definition

def x="abc"

2. Assign a variable to the result from the script execution branch = “/jen_script/return-branch.sh $group $job”.execute().text

# Separate the results by commas , Write to array branch = “one, two, three” branch_list = branch[1..-2].tokenize(‘,’)

  1. 3. quotes
  2. ```python
  3. def x="abc"
  4. print '${x}' // Output ${x}, Variable not supported
  5. print "${x}" // Output abc, Support variables
  6. print ''' // Output ${X}, Variable not supported
  7. ${X}
  8. '''
  9. print """ // Output abc, Support variables
  10. ${X}
  11. """

Two . Process judgment

1. Judgment variable

  1. if(x='abc'){
  2. echo "abc"
  3. }else(x='bcd'){
  4. echo "bcd"
  5. }

3、 ... and . Method

1. Define the method and call

  1. /String Is to declare that this variable should be of type string , It can be omitted , The type changes according to the incoming type
  2. def createName(String givenName,String familyName){
  3. return givenName +""+ familyName
  4. }
  5. // call , Parentheses may be omitted
  6. createName(familyName ="Lee", givenName ="Bruce")

2. Method to add default parameters

  1. def sayHello(String name ="zhangsan"){
  2. print"hello ${name}"
  3. }
  4. // Parentheses cannot be omitted when parameters are not passed
  5. sayHello()

3. Closure

  1. // Define closure
  2. def codeBlock ={print"hello closure"}
  3. // Closures can also be used directly as function calls
  4. codeBlock()// Output hello closure

4. The closure is passed as a parameter to another method

  1. // Define closure
  2. def codeBlock ={print"hello closure"}
  3. // Define a method , It takes a closure parameter
  4. def sayHello(closure){
  5. closure()
  6. }
  7. // Calling sayHello Method can be as follows
  8. sayHello(codeBlock)
  9. // If you remove the statement defined by the closure
  10. sayHello({print"hello closure"})
  11. // Because parentheses are not necessary , therefore
  12. sayHello {
  13. print"hello closure"
  14. }
  15. // If sayHello Change the name to pipeine Namely , Is it very similar jenkins Of pipeline
  16. pipeline {
  17. print"hello closure"
  18. }

5. Alternative use of closures , Define a stage Method

  1. // Define methods , Pass a normal variable and a closure
  2. def stage(String name, closue){
  3. print name
  4. closue()
  5. }
  6. // Under normal circumstances , Use this way stage function
  7. stage("stage name",{print"closure"})
  8. // Execution printing
  9. //stage name
  10. //closure
  11. // It can be written in another way
  12. stage("stage name"){
  13. print"closure"
  14. }

Four . Array

1. Define an array , Then determine whether it is in the array . For example, judgment two Whether in one In this array , You need to define the string first , Back cut .

  1. ipeline {
  2. agent any
  3. environment {
  4. one ="xxx,ddd,lll"
  5. two ="ddd"
  6. }
  7. stages {
  8. stage('pull'){
  9. steps {
  10. script {
  11. list = one.split(',')
  12. for( i in list ){
  13. echo "$i"
  14. echo "$two"
  15. if(i == two){
  16. echo "ok two"
  17. }else{
  18. echo "no two"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
原网站

版权声明
本文为[Chen Bucheng I]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210602191027606c.html