[젠킨스(Jenkins)] 기초 2. 파이프라인 문법 (쉘)
2024. 5. 27. 02:31
젠킨스(Jenkins)에서 sh 명령어를 사용하여 명령을 실행하고, 그 결과를 stdout으로 화면에 출력하려면 다음과 같이 파이프라인 스크립트를 작성할 수 있습니다.
파이프라인의 script 블록 안에 sh 명령어를 사용하고, returnStdout 옵션을 사용하면 됩니다.
다음은 예시입니다:
예시 1. sh(..) 명령어를 이용하여 쉘 스크립트 실행
pipeline {
agent any
stages {
stage('Run Shell Command') {
steps {
script {
// 쉘 명령을 실행하고, 결과를 변수에 저장
def result = sh(script: 'echo "Hello, Jenkins!"', returnStdout: true).trim()
// 결과를 화면에 출력
echo "Command output: ${result}"
}
}
}
}
}
이 스크립트는 다음과 같은 작업을 수행합니다:
1. sh 스텝을 사용하여 echo "Hello, Jenkins!" 명령을 실행합니다.
2. returnStdout: true 옵션을 사용하여 명령어의 출력 결과를 캡처합니다.
3. 캡처된 출력 결과를 result 변수에 저장하고, echo 스텝을 사용하여 화면에 출력합니다.
이 방식으로 원하는 쉘 명령어를 sh 스텝에 넣고 returnStdout 옵션을 사용하면 그 결과를 캡처하고 화면에 출력할 수 있습니다.
예시 2. 쉘 커맨드를 변수에 넣고 출력하는 방법
pipeline {
agent any
stages {
stage('Run Shell Command with Variable') {
steps {
script {
// 쉘 명령어를 변수에 저장
def shellCommand = 'echo "Hello, Jenkins!"'
// 변수에 저장된 쉘 명령어를 실행하고, 결과를 변수에 저장
def result = sh(script: shellCommand, returnStdout: true).trim()
// 결과를 화면에 출력
echo "Command output: ${result}"
}
}
}
}
}
예시 3. 쉘 스크립트 여러줄 실행하는 방법 (권장하지 않는 방법) ''' 사용
왜 권장하지 않나면,
작은 따옴표는 변수 보간(variable interpolation) (변수 삽입)을 지원하지 않고, 큰 따옴표만 변수 보간을 지원하기 때문이다.
큰 따옴표(Double Quotes)와 작은 따옴표(Single Quote)의 쓰임새가 다르기 때문이다.
큰 따옴표는 변수 보간을 지원한다.
▶ 작은 따옴표의 사용: 정말로 literal strings 을 표현하고 싶을 때 사용한다. ${} 키워드를 사용해도, ${} 그대로 출력이 된다.(변수 보간이 없음)
만약 변수나 escape sequences (e.g., \n or \t)를 처리하고 싶지 않을 때 사용하면 좋다.
▶ 큰 따옴표의 사용: 변수 보간을 사용하고 싶을 때 사용한다. ${} 문법을 통해 변수를 넣는다.
pipeline {
agent any
stages {
stage('Run Multiple Shell Commands with Variable') {
steps {
script {
// 여러 줄의 쉘 명령어를 변수에 저장
def shellCommand = '''
echo "Hello, Jenkins!"
echo "Running multiple commands"
ls -la
'''
// 변수에 저장된 쉘 명령어를 실행하고, 결과를 변수에 저장
def result = sh(script: shellCommand, returnStdout: true).trim()
// 결과를 화면에 출력
echo "Command output: ${result}"
}
}
}
}
}
예시 4. 쉘 스크립트 여러줄 실행하는 방법 (권장하는 방법) (변수 포함) """ 사용
소스코드
pipeline {
agent any
stages {
stage('Run Multiple Shell Commands with Variable') {
steps {
script {
def var = "Hong gil 1004"
// 여러 줄의 쉘 명령어를 변수에 저장
def shellCommand = """
echo "Hello, Jenkins!"
echo "Running multiple commands"
echo "${var}"
ls -la
"""
// 변수에 저장된 쉘 명령어를 실행하고, 결과를 변수에 저장
def result = sh(script: shellCommand, returnStdout: true).trim()
// 결과를 화면에 출력
echo "Command output: ${result}"
}
}
}
}
}
출력화면
[Pipeline] stage
[Pipeline] { (Run Multiple Shell Commands with Variable)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo Hello, Jenkins!
+ echo Running multiple commands
+ echo Hong gil 1004
+ ls -la
[Pipeline] echo
Command output: Hello, Jenkins!
Running multiple commands
Hong gil 1004
total 8
drwxr-xr-x 2 jenkins jenkins 4096 12월 17 22:41 .
drwxr-xr-x 4 jenkins jenkins 4096 12월 17 22:41 ..
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
예시 5. 쉘 스크립트 여러줄 실행하는 방법 2 (권장하는 방법)
소스코드
pipeline {
agent any
stages {
stage('Run Multiple Shell Commands with Variable') {
steps {
script {
def dir = "test_dir4"
def file = "a"
// 여러 줄의 쉘 명령어를 변수에 저장
def shellCommand = """
echo "Hello, Jenkins!"
mkdir "${dir}"
cd "${dir}"
touch a
touch b
touch c
ls -al "${file}"
"""
// 변수에 저장된 쉘 명령어를 실행하고, 결과를 변수에 저장
def result = sh(script: shellCommand, returnStdout: true).trim()
// 결과를 화면에 출력
echo "Command output: ${result}"
}
}
}
}
}
출력화면
...
[Pipeline] { (Run Multiple Shell Commands with Variable)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo Hello, Jenkins!
+ mkdir test_dir4
+ cd test_dir4
+ touch a
+ touch b
+ touch c
+ ls -al a
[Pipeline] echo
Command output: Hello, Jenkins!
-rw-r--r-- 1 jenkins jenkins 0 12월 17 23:09 a
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
...
'젠킨스(Jenkins) > 젠킨스(Jenkins)' 카테고리의 다른 글
[젠킨스(Jenkins)] 우분투에 젠킨스 설치하고 실행하기 (0) | 2024.05.27 |
---|---|
[젠킨스(Jenkins)] 젠킨스 버전 업데이트 (0) | 2024.02.14 |
[젠킨스(Jenkins)] 암호화된 credentials secret key 확인하는 방법 (0) | 2023.03.08 |
[젠킨스(Jenkins)] 윈도우에서 젠킨스 설치하기 + 관리하기 (0) | 2023.01.05 |
[젠킨스(Jenkins)] 용어 (0) | 2021.11.11 |