jenkinsfile
1
- pipeline {
- agent any
- environment{
- def build_env="dev"
- def mailFrom="xxxxx@163.com"
- def mailRecipients="xxxxx@xxx.com"
- }
- stages {
- stage('Pull Code') {
- steps {
- //
- checkout([$class: 'GitSCM',branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: '511c4c13-45bc-4c9f-ba38-3b37d05e4a9a', url: 'git@github.com:xxxx/CICD.git']]])
- }
- post {
- success {
- echo 'pull code success.'
- }
- }
- }
- stage('Deploy'){
- steps {
- script {
- echo "Start build python-api"
- def count = sh(script: "docker ps|grep python-api |wc -l", returnStdout: true).trim()
- if (count!='0'){
- echo "docker stop python-api"
- sh "docker stop python-api"
- }
- def count2 = sh(script: "docker ps -a|grep python-api |wc -l", returnStdout: true).trim()
- if (count2!='0'){
- echo "docker rm python-api"
- sh "docker rm python-api"
- }
- echo "docker rmi python-api"
- sh "docker rmi python-api:${build_env}"
- echo "docker build"
- sh "docker build -t python-api:${build_env} ."
- echo "clear old images(status=none)"
- sh "docker image prune -f"
- }
- }
- post {
- success {
- echo 'build docker success.'
- }
- }
- }
- stage('Run'){
- steps {
- echo "docker run python-api"
- sh "docker run --name=python-api -d -p 8088:8088 python-api:${build_env}"
- }
- }
- stage('Test'){
- steps {
- script{
- echo "test status...."
- def response = httpRequest "http://139.x.x.x:8088/"
- println('Status: '+response.status)
- println('Response: '+response.content)
- def res = response.content.replace("\"", "")
- if(res == "ok"){
- //send email
- echo "Deploy success"
- }else{
- error "Deploy failed. API can not access."
- }
- }
- }
- }
- }
- post {
- always {
- echo 'Build completed...'
- }
- success {
- echo 'build success!'
- mail subject: "[Jenkins] ${env.JOB_NAME} [Build#${env.BUILD_NUMBER}] - Success!",
- body: "Check console output at ${env.BUILD_URL} to view the results.",
- charset: 'utf-8',
- from: "${mailFrom}",
- mimeType: 'text/html',
- to : "${mailRecipients}"
- }
- failure {
- echo 'build failure!'
- mail subject: "[Jenkins] ${env.JOB_NAME} [Build#${env.BUILD_NUMBER}] - Failure!",
- body: "Check console output at ${env.BUILD_URL} to view the results.",
- charset: 'utf-8',
- from: "${mailFrom}",
- mimeType: 'text/html',
- to: "${mailRecipients}"
- }
- unstable {
- echo 'the job is marked as unstable.'
- }
- changed {
- echo ''
- }
- }
- }
2
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · Amazon Linux 2023 安装Docker和Docker Compose
- · 修改Docker的默认网段
- · docker定时任务Mysql脚本
- · 解决Linux实例磁盘空间满问题
- · failed to fetch metadata: fork/exec /home/appuser/.docker/cli-plugins/docker-buildx: exec format error
- · Docker登录login报错Error saving credentials
- · docker安装mysql8注意事项
- · 黑群晖docker无法pull镜像,x509错误解决方法
- · k3s+kuboard快速搭建K8s生产环境集群
- · 解决es报错:blocked by: [FORBIDDEN/12/index read-only / allow delete (api) ]
- · filebeat按docker容器名创建不同的索引
- · traefik负载均衡/滚动升级