pipeline{ agent{ node { label ‘master‘ } } triggers { cron ‘H 1 * * *‘ } environment{ user="xxxx" pswd="xxxx" url="xxxxxx" proids=‘1 2 4 5 6 7 8 9 10‘ } stages{ stage(‘deleteImage‘){ steps{ script{ sh returnStdout: true, label: ‘deleteImage‘, script: ‘‘‘ set +x for proid in ${proids} do #echo ${proid} num=0 REPOS=$(curl -s -u "${user}:${pswd}" -X GET --header ‘Accept: application/json‘ "${url}/api/repositories?project_id=${proid}"|grep "name"|awk -F ‘"‘ ‘{print $4}‘) #echo ${REPOS} deleted_images="" for rp in ${REPOS} do TAGS=$(curl -s -u "${user}:${pswd}" -X GET --header ‘Accept: application/json‘ "${url}/api/repositories/${rp}/tags"|grep \"name\"|awk -F ‘"‘ ‘{print $4}‘) for t in ${TAGS} do #echo ${t} createTime=$(curl -s -u "${user}:${pswd}" -X GET --header ‘Accept: application/json‘ "${url}/api/repositories/${rp}/tags/${t}"|grep \"created\"|awk -F ‘"‘ ‘{print $4}‘) createTime=${createTime/T/ } createTime=${createTime/Z/} cTs=`date -d "${createTime}" +%s` nTs=`date +%s` interval=`expr $nTs - $cTs` dayCount=`expr $interval / 3600 / 24 + 1` if [ ${dayCount} -gt 8 ]; then curl -s -u "${user}:${pswd}" -X DELETE -H "Content-Type: application/json" "${url}/api/repositories/${rp}/tags/${t}" echo "Deleted ===> ${URL}/${rp}:${t}">>${BUILD_ID}.log deleted_images="${deleted_images} ${URL}/${rp}:${t}" num=`expr $num + 1` fi done done echo "all deleted images number is: $num ">>${BUILD_ID}.log if [ -z ${deleted_images} ]; then echo ">>>>no delete image">>${BUILD_ID}.log else echo "delete images are:">>${BUILD_ID}.log echo "${deleted_images}">>${BUILD_ID}.log fi done ‘‘‘ def msgs=readFile file: "${BUILD_ID}.log" print(msgs) } } } } }
可以用crontab定时任务去做,不过不好检查。用jenkins好看一点。这个没有发邮件,还可以发邮件或者其他的通知