pipeline {
agent { label 'master' }
environment {
// CI 변수
GIT_CREDENTIALS_ID = "test1"
GIT_URL = "gitlab.paas.smes-tipa.go.kr/test1/helpdesk.git" // http:// 또는 https:// 제거
BRANCH="master"
// Path Directory 변수 및 웹 도메인 변수 지정
MAINNAME = "helpdesk"
DOMAIN = "www.smes.go.kr"
DIRPATH = "helpdesk"
// CF LOGIN 변수
PAASTA_CREDENTIAL_ID = "paasta-admin"
CF_ORG_NAME = "중소벤처24시스템_운영"
CF_SPACE_NAME = "헬프데스크"
//CF PUSH를 위한 변수
CF_API_URL = "https://api.paas.smes-tipa.go.kr"
CF_MANIFEST = "manifest.yml"
}
tools { nodejs 'node-v16.13.1' }
stages {
stage("01. git clone") {
steps {
script {
try {
git credentialsId: "${GIT_CREDENTIALS_ID}", url: "http://${GIT_URL}", branch: "${BRANCH}", poll: true, changelog: true
}
catch(Exception e) {
cleanWs()
PrintErrorMessage(e)
currentBuild.result = 'FAILURE'
}
}
}
}
stage("02. yarn build") {
steps {
script {
try {
dir ('front-end') {
sh ("sed -i 's%https://registry.npmjs.org/%http://10.0.201.173:8081/repository/npm-group/%g' package-lock.json")
//sh "npm install"
//sh "rm -rf yarn.lock .yarn.rc"
//sh "yarn cache clean && yarn config set nodedir nodefile"
//sh "yarn install --force"
//sh "yarn build"
sh 'npm install --sass-binary-path="/opt/linux/linux-x64-93_binding.node"'
sh "npm run build"
}
}
catch(Exception e) {
cleanWs()
PrintErrorMessage(e)
currentBuild.result = 'FAILURE'
}
}
}
}
stage("03. Create Path Directory") {
steps {
script {
try {
dir ('front-end') {
sh "mkdir -p push"
sh "mkdir -p push/${DIRPATH}"
sh "rm -rf push/${DIRPATH}/*"
sh "cp -rp build/* push/${DIRPATH}/"
}
}
catch(Exception e) {
cleanWs()
PrintErrorMessage(e)
currentBuild.result = 'FAILURE'
}
}
}
}
stage ('04. Create manifest.yml for cf app') {
steps{
script {
try {
dir ('front-end') {
sh("""
cat > push/manifest.yml <<EOF
---
applications:
- name: ${MAINNAME}
routes:
- route: ${DOMAIN}/${DIRPATH}
- route: helpdesk.paas.smes-tipa.go.kr
memory: 1024M
instances: 1
buildpacks:
- staticfile_buildpack_offline
path: .
EOF
""")
}
}
catch(Exception e) {
cleanWs()
PrintErrorMessage(e)
currentBuild.result = 'FAILURE'
}
}
}
}
stage ('05. Create nginx.conf for cf app') {
steps{
script {
try {
dir ('front-end') {
sh('''
cat > push/nginx.conf <<EOF
worker_processes 1;
daemon off;
error_log /home/vcap/app/nginx/logs/error.log;
events { worker_connections 1024; }
http {
charset utf-8;
log_format cloudfoundry '\\$http_x_forwarded_for - \\$http_referer - [\\$time_local] "\\$request\\" \\$status \\$body_bytes_sent';
access_log /home/vcap/app/nginx/logs/access.log cloudfoundry;
default_type application/octet-stream;
include mime.types;
sendfile on;
client_max_body_size 2048m;
gzip on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gunzip on;
gzip_static always;
gzip_types text/plain text/css text/js text/xml text/javascript application/javascript application/x-javascript application/json application/xml application/xml+rss;
gzip_vary on;
tcp_nopush on;
keepalive_timeout 30;
port_in_redirect off; # Ensure that redirects don't include the internal container PORT - 8080
server_tokens off;
server {
listen 8080;
server_name localhost;
root /home/vcap/app/public;
location / {
try_files \\$uri \\$uri/ /${DIRPATH}/index.html;
index index.html index.htm Default.htm;
}
location ~ /\\. {
deny all;
return 404;
}
location /helpdesk/api/v1 {
proxy_pass https://helpdesk-api.paas.smes-tipa.go.kr/api/v1;
}
}
}
EOF
''')
}
}
catch(Exception e) {
cleanWs()
PrintErrorMessage(e)
currentBuild.result = 'FAILURE'
}
}
}
}
stage('06-CF app deploy') {
steps {
echo '06-CF app deploy..'
script {
try {
withCredentials([usernamePassword(credentialsId: "${PAASTA_CREDENTIAL_ID}", passwordVariable: 'passta_password', usernameVariable: 'passta_username')]) {
def lastSuccessfulBuildID = 0
def build = currentBuild.previousBuild
while (build != null) {
if (build.result == "SUCCESS")
{
lastSuccessfulBuildID = build.id //as Integer
break
}
build = build.previousBuild
}
dir ('front-end') {
sh "cf login -a ${CF_API_URL} -u ${passta_username} -p ${passta_password} -o ${CF_ORG_NAME} -s ${CF_SPACE_NAME} --skip-ssl-validation"
sh "cf push -f push/${CF_MANIFEST}"
//sh "cf delete-route -f ${CF_DOMAIN} --hostname ${CF_SUBDOMAIN}"
//sh "cf map-route ${PROJECT}${BUILD_NUMBER} ${CF_DOMAIN} --hostname ${CF_SUBDOMAIN}"
//sh "cf unmap-route ${PROJECT}${lastSuccessfulBuildID} ${CF_DOMAIN} --hostname ${CF_SUBDOMAIN}"
//sh "cf logout"
}
}
}
catch(Exception e) {
addComment("CF app deploy 스테이지에서 실패했습니다.")
cleanWs()
PrintErrorMessage(e)
currentBuild.result = 'FAILURE'
}
}
}
}
/*
stage('07. clean') {
steps {
script {
dir ('front-end') {
sh 'rm -rf push'
sh 'rm -rf build'
}
}
}
}
*/
}
}
Shell
복사