Declarative Pipeline


<aside>

pipeline { //pipeline 선언
    agent any  // 실행할 에이전트 (any: 아무 에이전트에서 실행)

    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
                sh './gradlew build'  // Gradle 빌드 실행
            }
        }

        stage('Test') {
            steps {
                echo 'Running tests...'
                sh './gradlew test'
            }
        }

        stage('Deploy') {
            steps {
                echo 'Deploying application...'
                sh 'scp build/libs/app.jar user@server:/deploy/path/'
            }
        }
    }
}

agent 블록


pipeline {
	1. agent any //아무 에이전트
	2. agent { label 'docker' } // 'docker' 라벨이 있는 노드
	3. agent {
			 docker {
				 image 'openjdk:11'
}

</aside>

문법


<aside>

stages & stage


stages {
	stage('Build') {
		step {
			echo 'Building the project'
			sh '.lgradlew build'
			}
		}
	}

enviroment


pipeline {
    environment {
        APP_NAME = 'my-app'
        APP_VERSION = '1.0.0'
    }
    stages {
        stage('Build') {
            steps {
                sh 'echo Building $APP_NAME version $APP_VERSION'
            }
        }
    }
}

post


post {
    success {
        echo 'Build succeeded!'
    }
    failure {
        echo 'Build failed!'
    }
    always {
        echo 'Cleaning up...'
    }
}

parallel


stages {
    stage('Parallel Jobs') {
        parallel {
            stage('Test1') {
                steps {
                    sh 'echo Running Test 1'
                }
            }
            stage('Test2') {
                steps {
                    sh 'echo Running Test 2'
                }
            }
        }
    }
}

</aside>

키워드 설명
pipeline {} Jenkinsfile의 기본 블록
agent any 어느 노드에서 실행할지 지정
stages {} 여러 단계(스테이지) 그룹
stage('이름') {} 개별 작업 단계
steps {} 해당 단계에서 실행할 작업
sh '명령어' Shell 명령 실행 (Linux)
echo '출력할 메시지' 콘솔에 메시지 출력
environment {} 환경 변수 설정
post {} 빌드 후 작업 (성공, 실패 시)
when {} 특정 조건에서 실행
input '메시지' 수동 승인 요청
parallel {} 병렬 실행