Jenkins Decrarative Pipeline에서 stage를 배열 정의
                                            
                                                
                                                
                                                
                                                
                                                
                                                 7940 단어  JenkinsJenkinsPipeline
                    
환경마다 하나씩 처리를 실행하는 것은 번거롭다.
Pipeline에서 stage별 처리를 반복해서 쓰는 것은 귀찮습니다.
루프 처리로 실시해 버리고 싶네요.
하고 싶은 일
이런 처리입니다.
예로서 복수 리전에서 처리를 하는 케이스를 들고 있습니다.
pipeline {
    agent any
    stages{
        stage('ap-northeast-1') {
            steps{
                script{
                    echo "ap-northeast-1 の処理"
                }
            }
        }
        stage('us-east-1') {
            steps{
                script{
                    echo "us-east-1 の処理"
                }
            }
        }
    }
}
script의 내용에, 리전명만 바꾸어 몇번이나 같은 것을 쓰는 것은 귀찮거나 합니다.
배열의 Loop
다음과 같이 작성하여 변경할 부분을 변수화할 수 있습니다.
pipeline {
    agent any
    stages{
        stage('Prepare'){
            steps{
                script{
                    env_list = ["ap-northeast-1","us-east-1"]
                    env_list.each { region -> 
                        stage(region) {
                            echo "${region} の処理"
                        }
                    }
                }
            }
        }
    }
}
실행 화면은 다음과 같습니다.

Prepare Stage에서 다른 stage를 감싸고 있습니다.이것에 의해, 배열을 전개하는 Groovy 처리를 기술할 수 있게 되어 있습니다.
어레인지 버전
map 이용
맵을 이용할 수도 있습니다.
stage마다 변동시키고 싶은 값이 2개 있을 때 편리합니다.
script{
    env_map = [
        "ap-northeast-1": "tokyo",
        "us-east-1": "virsinia"
    ]
    env_map.each { region -> 
        stage(region.key) {
            echo "${region.value} の処理"
        }
    }
}
실행 화면은 다음과 같습니다.

Parameters에서 파라미터 전환
stage에 들어가기 전에 script를 사이에 두는 것으로,
if 분기 등으로 Stage 내의 내용을 조정하는 것도 가능합니다.
pipeline {
  agent any
  parameters {
      choice(name: 'Cloud', choices: 'aws\ngcp', description: '説明')
  }
  stages{
    stage('Prepare'){
      steps{
        script{
          if (params.Cloud == 'aws'){
            env_map = [
              "tokyo": "ap-northeast-1",
              "virsinia": "us-east-1"
            ]                    
          } else if (params.Cloud == 'gcp'){
            env_map = [
              "tokyo": "asia-northeast1",
              "virsinia": "us-east4"
            ]
          }
          env_map.each { region -> 
            stage(region.key) {
              echo "${region.value} の処理"
            }
          }
        }
      }
    }
  }
}
마지막으로
Jenkins Decrarative Pipeline에서 stae를 배열로 정의하는 방법을 소개했습니다.
여러 환경으로부터의 정보 수집, 복수 환경에의 배포 등으로 활용할 수 있다고 생각합니다.
확인을 위해 Jenkins는 2.183, 플러그인은 처음 시작할 때 권장되는 것만 사용했습니다.
Reference
이 문제에 관하여(Jenkins Decrarative Pipeline에서 stage를 배열 정의), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/98lerr/items/3051f824a1085c36b109텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)