Jenkins Docker Pipeline Clone Build Deploy mysqldump

本文首发在这里

先决条件

  • 装好 Docker 的 Ubuntu
  • 钉钉机器人 Webhook
bash 复制代码
curl -H 'Content-Type:application/json' -d '{"msgtype":"text","text":{"content":"hello world"}}' 'https://oapi.dingtalk.com/robot/send?access_token=secret'

安装 Jenkins

以下命令基本来自官方,列出仅做参考

bash 复制代码
sudo docker network create jenkins
mkdir -p docker/jenkins/certs docker/jenkins/data
sudo docker run --name jenkins-docker --rm --detach --privileged --network jenkins --network-alias docker --env DOCKER_TLS_CERTDIR=/certs --volume ~/docker/jenkins/certs:/certs/client --volume ~/docker/jenkins/data:/var/jenkins_home --publish 2376:2376 docker:dind --storage-driver overlay2
vi docker/jenkins/Dockerfile # 请从官方文档复制需要粘贴进去的内容
cd docker/jenkins && sudo docker build -t myjenkins-blueocean:2.426.1-1 .
sudo docker run --name jenkins-blueocean --restart=on-failure --detach --network jenkins --env DOCKER_HOST=tcp://docker:2376 --env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 --publish 8080:8080 --publish 50000:50000 --volume ~/docker/jenkins/data:/var/jenkins_home --volume ~/docker/jenkins/certs:/certs/client:ro myjenkins-blueocean:2.426.1-1
sudo docker exec -it jenkins-blueocean bash
sudo docker logs jenkins-blueocean

安装后设置向导请遵循官方

配置 Jenkins

  • 下载插件 SSH Agent、DingTalk
  • 添加凭据 系统管理 -> 凭据管理 -> 全局 -> 添加凭据 -> 范围选择全局
  1. Username with password
  2. SSH Username with private key
bash 复制代码
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@192.168.1.188
cat ~/.ssh/id_rsa # 私钥
  • 添加钉钉机器人 id、名称、webhook

新建流水线

建议勾选丢弃旧的构建

Clone Build Deploy
groovy 复制代码
pipeline {
    agent {
        docker {
            image 'golang:1.21.4-bookworm'
            args  '-u 0:0'
        }
    }
    parameters {
        choice(name: 'IP', choices: ['188'], description: 'pick one')
    }
    stages {
        stage('start') {
            steps {
                dingtalk(robot: 'bot', type: 'TEXT', text: ['jenkins ${IP} start'])
            }
        }
        stage('clone') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'git', usernameVariable: 'username', passwordVariable: 'password')]) {
                    sh '''
                    rm -rf framework
                    git clone https://$username:$password@github.com/panshiqu/framework.git
                    '''
                }
            }
            post {
                failure {
                    dingtalk(robot: 'bot', type: 'TEXT', text: ['jenkins ${IP} clone failure'])
                }
            }
        }
        stage('build') {
            steps {
                sh '''
                echo choice: ${IP}
                cd framework
                pwd
                go version
                go env -w GOPROXY=https://goproxy.cn,direct
                go env -w GOCACHE=/tmp/.cache/go-build
                go env -w GOFLAGS=-buildvcs=false
                go mod tidy
                go build -o db_server db.go
                go build -o manager_server manager.go
                go build -o proxy_server proxy.go
                go build -o login_server login.go
                go build -o game_server game.go
                '''
            }
            post {
                failure {
                    dingtalk(robot: 'bot', type: 'TEXT', text: ['jenkins ${IP} build failure'])
                }
            }
        }
        stage('deploy') {
            steps {
                sshagent(credentials: ["${IP}"]) {
                    sh '''
                    ssh -o StrictHostKeyChecking=no -l ubuntu 192.168.1.${IP} uname -a
                    cd framework && scp *_server ubuntu@192.168.1.${IP}:~/server/update
                    '''
                }
            }
            post {
                failure {
                    dingtalk(robot: 'bot', type: 'TEXT', text: ['jenkins ${IP} deploy failure'])
                }
                success {
                    dingtalk(robot: 'bot', type: 'TEXT', text: ['jenkins ${IP} clone && build && deploy success'])
                }
            }
        }
    }
}
mysqldump
groovy 复制代码
pipeline {
    agent {
        docker {
            image 'mysql:5.7.44'
        }
    }
    parameters {
        string(name: 'fromIP', defaultValue: '192.168.1.177', description: 'ip')
        string(name: 'fromPWD', defaultValue: '123456', description: 'pwd')
        string(name: 'toIP', defaultValue: '192.168.1.188', description: 'ip')
        string(name: 'toPWD', defaultValue: '123456', description: 'pwd')
    }
    stages {
        stage('start') {
            steps {
                dingtalk(robot: 'bot', type: 'TEXT', text: ['jenkins mysqldump ${fromIP}->${toIP} start'])
            }
        }
        stage('dump') {
            steps {
                sh '''
                mysqldump -h${fromIP} -uroot -p${fromPWD} --events --routines --add-drop-database --databases game log > dump.sql
                mysql -h${toIP} -uroot -p${toPWD} < dump.sql
                rm -f dump.sql
                '''
            }
            post {
                failure {
                    dingtalk(robot: 'bot', type: 'TEXT', text: ['jenkins mysqldump ${fromIP}->${toIP} failure'])
                }
                success {
                    dingtalk(robot: 'bot', type: 'TEXT', text: ['jenkins mysqldump ${fromIP}->${toIP} success'])
                }
            }
        }
    }
}

远程构建

  • 流水线配置勾选触发远程构建,身份验证令牌填入abc
  • 全局安全配置勾选匿名用户具有可读权限
bash 复制代码
curl 'http://192.168.1.188:8080/job/framework/buildWithParameters?IP=188&token=abc'
curl 'http://192.168.1.188:8080/job/mysqlgame/buildWithParameters?fromIP=192.168.1.177&fromPWD=123456&toIP=192.168.1.188&toPWD=123456&token=abc'
相关推荐
追风林4 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
城南vision6 小时前
Docker学习—Docker核心概念总结
java·学习·docker
wclass-zhengge6 小时前
Docker篇(Docker Compose)
运维·docker·容器
梦魇梦狸º10 小时前
腾讯轻量云服务器docker拉取不到镜像的问题:拉取超时
docker·容器·github
鬼才血脉10 小时前
docker+mysql配置
mysql·adb·docker
詹姆斯爱研究Java11 小时前
Docker 的基本概念和优势
docker
南猿北者12 小时前
docker镜像仓库常用命令
运维·docker·容器
杨浦老苏13 小时前
Docker可视化管理面板DPanel的安装
运维·docker·群晖
航月14 小时前
FTP、ISCSI、CHRONY、DNS、NFS、DOCKER、MARIADB、NGINX、PHP、CA各服务开启方法
nginx·docker·mariadb
微刻时光14 小时前
Docker镜像分成
java·运维·开发语言·docker·容器·镜像