<aside> 💡 **실습 목표
</aside>
기본 코드 구조
pipeline : 반드시 맨 위에 있어야 한다.
agent : 어디에서 실행할 것인지 정의한다.
any, none, label, node, docker, dockerfile, kubernetes
agent 가 none 인 경우 stage 에 포함시켜야 함
pipeline {
agent none
stages {
stage('Example Build') {
agent { docker 'maven:3-alpine' }
steps {
echo 'Hello, Maven'
sh 'mvn --version'
}
}
stage('Example Test') {
agent { docker 'openjdk:8-jre' }
steps {
echo 'Hello, JDK'
sh 'java -version'
}
}
}
}
stages : 하나 이상의 stage 에 대한 모음
pipeline 블록 안에서 한 번만 실행 가능함
pipeline {
agent any
stages {
stage("build") {
steps {
echo 'building the applicaiton...'
}
}
stage("test") {
steps {
echo 'testing the applicaiton...'
}
}
stage("deploy") {
steps {
echo 'deploying the applicaiton...'
}
}
}
}
본인 Github 에 새 Repository [ js-pipeline-project ] 생성
Local 에서 'Jenkinsfile' 파일 생성하여 위 stages 코드 복사한 후 Github 업로드
post 조건
pipeline {
agent any
stages {
stage("build") {
steps {
echo 'building the applicaiton...'
}
}
stage("test") {
steps {
echo 'testing the applicaiton...'
}
}
stage("deploy") {
steps {
echo 'deploying the applicaiton...'
}
}
}
post {
always {
echo 'building..'
}
success {
echo 'success'
}
failure {
echo 'failure'
}
}
}
when 조건 추가
pipeline {
agent any
stages {
stage("build") {
when {
expression {
env.GIT_BRANCH == 'origin/master'
}
}
steps {
echo 'building the applicaiton...'
}
}
stage("test") {
when {
expression {
env.GIT_BRANCH == 'origin/test' || env.GIT_BRANCH == ''
}
}
steps {
echo 'testing the applicaiton...'
}
}
stage("deploy") {
steps {
echo 'deploying the applicaiton...'
}
}
}
}
Jenkinsfile 자체 환경변수 목록 보기
Custom 환경변수 사용하기
pipeline {
agent any
environment {
NEW_VERSION = '1.0.0'
}
stages {
stage("build") {
steps {
echo 'building the applicaiton...'
echo "building version ${NEW_VERSION}"
}
}
stage("test") {
steps {
echo 'testing the applicaiton...'
}
}
stage("deploy") {
steps {
echo 'deploying the applicaiton...'
}
}
}
}
Credentials 자격 증명 환경 변수로 사용하기
Jenkins credential 추가
Jenkinsfile 에서 환경변수로 사용
pipeline {
agent any
environment {
NEW_VERSION = '1.0.0'
ADMIN_CREDENTIALS = credentials('admin_user_credentials')
}
stages {
stage("build") {
steps {
echo 'building the applicaiton...'
echo "building version ${NEW_VERSION}"
}
}
stage("test") {
steps {
echo 'testing the applicaiton...'
}
}
stage("deploy") {
steps {
echo 'deploying the applicaiton...'
echo "deploying with ${ADMIN_CREDENTIALS}"
sh 'printf ${ADMIN_CREDENTIALS}'
}
}
}
}

Jenkins 플러그인 중 Credentials Plugin 확인

pipeline {
agent any
environment {
NEW_VERSION = '1.0.0'
}
stages {
stage("build") {
steps {
echo 'building the applicaiton...'
echo "building version ${NEW_VERSION}"
}
}
stage("test") {
steps {
echo 'testing the applicaiton...'
}
}
stage("deploy") {
steps {
echo 'deploying the applicaiton...'
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'admin_user_credentials',
usernameVariable: 'USER',
passwordVariable: 'PWD'
]]) {
sh 'printf ${USER}'
}
}
}
}
}
Jenkinsfile 에 parameter 추가
pipeline {
agent any
parameters {
string(name: 'VERSION', defaultValue: '', description: 'deployment version')
choice(name: 'VERSION', choices: ['1.1.0','1.2.0','1.3.0'], description: '')
booleanParam(name: 'executeTests', defaultValue: true, description: '')
}
stages {
stage("build") {
steps {
echo 'building the applicaiton...'
}
}
stage("test") {
steps {
echo 'testing the applicaiton...'
}
}
stage("deploy") {
steps {
echo 'deploying the applicaiton...'
}
}
}
}
executeTests 가 true 인 경우의 조건 추가해보기
pipeline {
agent any
parameters {
choice(name: 'VERSION', choices: ['1.1.0','1.2.0','1.3.0'], description: '')
booleanParam(name: 'executeTests', defaultValue: true, description: '')
}
stages {
stage("build") {
steps {
echo 'building the applicaiton...'
}
}
stage("test") {
when {
expression {
params.executeTests
}
}
steps {
echo 'testing the applicaiton...'
}
}
stage("deploy") {
steps {
echo 'deploying the applicaiton...'
echo "deploying version ${params.VERSION}"
}
}
}
}