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'
相关推荐
fanruitian4 分钟前
docker 为单个容器设置代理
运维·docker·容器
梁萌7 分钟前
Docker快速安装Tomcat
docker·容器·tomcat·镜像
Doker 多克2 小时前
IntelliJ IDEA Docker集成
spring cloud·docker·intellij-idea
运维&陈同学4 小时前
【Elasticsearch05】企业级日志分析系统ELK之集群工作原理
运维·开发语言·后端·python·elasticsearch·自动化·jenkins·哈希算法
筏镜8 小时前
调整docker bridge地址冲突,通过bip调整 bridge地址
java·docker·eureka
韩俊强13 小时前
使用Docker部署一个Node.js项目
docker·容器·node.js
csdn_金手指14 小时前
Jenkins持续交付web应用,通过docker制作相关的镜像进行发布部署
运维·jenkins
Steven_Mmm14 小时前
初试Docker
运维·docker·容器
Gauss松鼠会15 小时前
GaussDB 企业版轻量化部署探索(二)
数据库·人工智能·docker·华为云·gaussdb
过过过呀Glik20 小时前
在 Ubuntu 上安装与配置 Docker 的完整指南
linux·ubuntu·docker