Pipeline script from SCM 을 사용하여 배포하기
가. Pipeline 생성
새로운 Item을 클릭하여 Pipeline 로 생성
나. Pipeline 부분 설정
1. Definition :
Pipeline script from SCM 로 선택
2. SCM :
Git 선택
3. Repository URL
https://github.com/kkimsungchul/stock.git
4. Credentials
아래의 URL을 보고 생성한 키를 등록
https://github.com/kkimsungchul/study/blob/master/Jenkins/%5BJenkins%5D%20credentials%20%EB%93%B1%EB%A1%9D%20%EB%B0%8F%20%EC%82%AC%EC%9A%A9.txt
5. Branches to build :
브런치 명 입력
6. Script Path:
JenkinsFile 입력
다. 프로젝트 폴더에 JenkinsFile 파일 생성하여 아래의 스크립트 입력
경로 : https://github.com/kkimsungchul/stock/blob/master/JenkinsFile
- 아래의 경우 오류가 발생함
※ 톰캣이 실행중이지 않을 떄
※ 오류 발생으로 ROOT.war 파일을 백업파일로 만들어 놓았을때
※ ROOT.war 파일이 존재하지 않을때
==================================================================================================================================================
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git credentialsId: 'kimsc1218', url: 'https://github.com/kkimsungchul/stock.git', branch: 'master'
}
}
stage('build') {
steps {
bat './gradlew.bat clean build'
}
}
stage('service_stop') {
steps {
bat """
net stop Tomcat8
"""
}
}
stage('backup') {
steps {
bat """
cd C:/apache-tomcat-8.0.30/webapps
del ROOT
rename ROOT.war ROOTwar.backup.%date%
"""
}
}
stage('depoly') {
steps {
bat """
cd C:/ProgramData/Jenkins/.jenkins/workspace/JenkinsTest/gitTest/build/libs
move ROOT.war C:/apache-tomcat-8.0.30/webapps
"""
}
}
stage('service_start') {
steps {
bat """
net start Tomcat8
"""
}
}
}
}
==================================================================================================================================================
======================================================================================================
import java.text.SimpleDateFormat
def remote = [:]
def download_command = ""
def now = ""
def file_name = ""
def upload_url = ""
def tar_command =""
def tar_upload_command =""
def snapshot_upload_command =""
pipeline {
agent { label 'docker-maven' }
environment {
ARTIFACT_ID = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
PACKGING = readMavenPom().getPackaging()
GROUP_ID = readMavenPom().getGroupId()
NEXUS_ARTIFACT_URL = 'https://sungchul.xyz/upload/'
DELIVERY_FILE_NAME = 'stock'
DELIVERY_FILE_EXTENSION = '.tar'
}
parameters {
credentials(
credentialType: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl',
defaultValue: '',
description: 'The credentials needed to deploy.',
name: 'deployCredentialsId',
required: true
)
}
stages {
stage("Pre"){
steps {
script {
def dateFormat = new SimpleDateFormat("yyMMddHHmm")
def date = new Date()
now = dateFormat.format(date)
file_name = DELIVERY_FILE_NAME+"-"+now+DELIVERY_FILE_EXTENSION
upload_url = NEXUS_ARTIFACT_URL
tar_command = 'cd target && tar cvf '+ file_name + ' stock.war '
tar_upload_command = "cd target && curl -u ${NEXUS_ID}:${NEXUS_PW} --upload-file "+file_name+" "+upload_url
snapshot_upload_command = "cd target && curl -u ${NEXUS_ID}:${NEXUS_PW} --upload-file stock.war "+upload_url
cd_url1 = "curl -X POST https://${CD_ID}:${CD_TOKEN}@jenkinscd.sungchul.xyz/job/sungchulFolder/job/stock/buildWithParameters?token=run"
}
echo file_name
echo upload_url
}
}
stage("Build") {
steps {
withMaven(mavenSettingsConfig: 'Maven_config') {
sh "echo Build"
sh "mvn clean package -Dmaven.test.skip=true"
}
}
}
stage("Delivery To Nexus") {
steps {
sh "pwd"
sh tar_command
sh "cd target && ls -al"
echo tar_upload_command
//sh tar_upload_command
echo snapshot_upload_command
sh snapshot_upload_command
}
}
stage("Deploy") {
steps {
sh cd_url1
}
}
}
}
======================================================================================================