Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (39 sloc) 1.69 KB
// 定期的にbranchと対応してない(すでに削除された)Dockerコンテナとnginx設定ファイルを削除する。
def APP_NAME = 'sample-app'
def WORK_DIR = "/var/tmp/${APP_NAME}"
def NGINX_SETTING_DIR = '/etc/nginx/default.d'
node {
def activeBranchNames // 現在リモートに存在するブランチ名
stage('Checkout scm') {
checkout scm
}
stage('List remote branch name.') {
def cmd = "git branch -a | grep 'origin' | sed -e 's/.*origin\\///g'"
activeBranchNames = sh(script: cmd, returnStdout: true).split(/\n/) as ArrayList // コマンド結果をListにして取得
}
stage('Remove inactive branch container.') {
def cmd = "docker ps --format '{{.Names}}' | grep ${APP_NAME}_ "
def applicationContainerNames = sh(script: cmd, returnStdout: true).split(/\n/)
applicationContainerNames.each{
def branchName = it.replace("${APP_NAME}_", '').trim()
if (!activeBranchNames.contains(branchName)) sh "docker rm -f ${it}"
}
}
stage('Remove inactive branch work directory.') {
def cmd = "ls ${WORK_DIR}"
def workDirNames = sh(script: cmd, returnStdout: true).split(/\n/)
workDirNames.each{
def branchName = it.trim()
if (!activeBranchNames.contains(branchName)) sh "rm -rf ${WORK_DIR}/${it}"
}
}
stage('Remove inactive branch Nginx setting files.') {
def cmd = "ls ${NGINX_SETTING_DIR}"
def nginxSettingFileNames = sh(script: cmd, returnStdout: true).split(/\n/)
nginxSettingFileNames.each{
def branchName = it.replace("${APP_NAME}_", '').replace('.conf', '').trim()
if (!activeBranchNames.contains(branchName)) sh "rm -rf ${NGINX_SETTING_DIR}/${it.trim()}"
}
sh 'nginx -s reload'
}
}