Skip to content

Instantly share code, notes, and snippets.

@gloriaJun
Last active July 2, 2021 03:22
Show Gist options
  • Save gloriaJun/cf0ce7e7799e5891953ce17628e38a85 to your computer and use it in GitHub Desktop.
Save gloriaJun/cf0ce7e7799e5891953ce17628e38a85 to your computer and use it in GitHub Desktop.
Jenkins pipeline
#!/usr/bin/env groovy
void clean_dirs(String agentHome) {
targetDir = "${agentHome}/workspace"
days = 3
checkDiskUsageCommand = "du -sh ${targetDir}"
findCommand = "find ${targetDir} -maxdepth 1 -type d -ctime +${days}"
sh """#!/bin/bash
echo "o check disk usage"
echo "${checkDiskUsageCommand}"
${checkDiskUsageCommand}
echo ""
echo "o check to remove target dirs"
echo "${findCommand}"
${findCommand}
echo ""
echo "o excute remove"
echo "${findCommand} -exec rm -rf {} \\;"
${findCommand} -exec rm -rf {} \\;
echo ""
echo "o check disk usage after removing"
echo "${checkDiskUsageCommand}"
${checkDiskUsageCommand}
"""
}
pipeline {
agent any
triggers {
// 매일 오전 3시 실행
cron('H 03 * * *')
}
stages {
stage('Env') {
steps {
sh "printenv"
}
}
stage('Clean Dir') {
parallel {
stage('master') {
agent { label 'master' }
steps {
clean_dirs("${env.JENKINS_HOME}")
}
}
stage('slave01') {
agent { label 'slave01' }
steps {
clean_dirs("${env.HOME}/jenkins_home")
}
}
}
}
}
post {
cleanup {
cleanWs()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment