Jenkins

[Jenkins] Pipeline - agent Section

비번변경 2023. 11. 1. 18:50

Sections

Jenkins 파이프라인에서 Section이란 Declarative Pipeline의 구성 요소로, 하나 이상의 Directives(지시문)이나 Steps를 포함한다. agent, post, stages, steps에 해당된다.

  • agent : 전체 파이프라인 또는 특정 stage를 실행할 환경 지정
  • post : 파이프라인 또는 stage 실행이 완료될 때 실행되는 추가 steps 정의
  • stages : 하나 이상의 단계 지시문(stage directives) 정의
  • steps : 단계 지시문(stage directives)에서 실행할 하나 이상의 step 정의

이 글에서는 Sections 중 agent에 대해서 정리해보려고 한다.

 

 

agent

전체 파이프라인 또는 특정 stage를 실행한 환경을 지정한다. agent 섹션은 최상위 또는 stage 수준에서 정의될 수 있는데, 최상위 수준 지정은 필수값이며, stage 수준에서의 지정은 필요에 따라 선택적으로 지정할 수 있다.

 

최상위 수준 지정

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

stage 수준 지정

pipeline {
    agent none
    stages {
        stage('Example') {
            agent any
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

 

agent 설정

agent section에 지정할 수 있는 값은 다음과 같다.

 

any

Pipeline 또는 Stage에서 사용할 수 있는 agent에서 실행한다.

 

none

최상위 수준에 적용하면 파이프라인 전체에서 사용할 agent를 할당하지 않는다. 각 stage에서 사용할 agent는 stage 수준에서 정의되어야 한다.

 

label

제공한 레이블로 젠킨스 환경에서 사용할 수 있는 agent에서 실행한다.

pipeline {
    agent { label 'my-defined-label' }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

node

agent { node { label 'labelName' } }와 agent { label 'labelName' }는 동일하게 동작한다. 다만 node는 추가 옵션을 지정할 수 있다.

 

docker

도커 기반 파이프라인을 사용할 수 있도록 구성되어 있는 노드에서 동적으로 프로비저닝 되는 컨테이너를 사용해 파이프라인 또는 stage를 실행한다. docker run 명령어 실행 시 전달할 매개변수를 지정할 수 있다.

pipeline {
    agent { 
        docker {
            image 'maven:3.9.3-eclipse-temurin-17' // 이미지
            label 'my-defined-label' // 이미지 태그
            args  '-v /tmp:/tmp' // docker run 시 전달 매개변수
            registryUrl 'https://myregistry.com/' // 이미지가 저장된 레지스트리
            registryCredentialsId 'myPredefinedCredentialsInJenkins' // 레지스트리 자격 증명 정보
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

dockerfile

소스 레포지터리에 저장된 Dockerfile로 빌드한 컨테이너를 사용해 파이프라인이나 stage를 실행한다. 

pipeline {
    agent {
        // Equivalent to "docker build -f Dockerfile.build --build-arg version=1.0.2 ./build/"
        dockerfile {
            filename 'Dockerfile.build'
            dir 'build'
            label 'my-defined-label'
            additionalBuildArgs  '--build-arg version=1.0.2'
            args '-v /tmp:/tmp'
        }
    }
}
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

kubernetes

Kubernetes 클러스터에 배포된 포드 내부에서 파이프라인 또는 stage를 실행한다. 배포할 포드는 kubernetes 블럭 내에 정의한다.

pipeline {
    agent {
        kubernetes {
            defaultContainer 'kaniko'
            yaml '''
kind: Pod
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    imagePullPolicy: Always
    command:
    - sleep
    args:
    - 99d
    volumeMounts:
      - name: aws-secret
        mountPath: /root/.aws/
      - name: docker-registry-config
        mountPath: /kaniko/.docker
  volumes:
    - name: aws-secret
      secret:
        secretName: aws-secret
    - name: docker-registry-config
      configMap:
        name: docker-registry-config
'''
       }
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

 

참고 문서

https://www.jenkins.io/doc/book/pipeline/syntax/#agent

https://blog.voidmainvoid.net/104

https://waspro.tistory.com/554

728x90