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'
相关推荐
wanmei0028 分钟前
Dockerfile复制目录进入镜像里
docker
inter_peng10 分钟前
[Docker-显示所有容器IP] 显示docker-compose.yml中所有容器IP的方法
tcp/ip·docker·eureka
Linux运维日记23 分钟前
k8s1.31版本最新版本集群使用容器镜像仓库Harbor
linux·docker·云原生·容器·kubernetes
一名路过的小码农2 小时前
ceph 18.2.4二次开发,docker镜像制作
ceph·docker·容器
ernesto_ji4 小时前
Jenkins下载安装、构建部署到linux远程启动运行
linux·servlet·jenkins
xiangshangdemayi5 小时前
Windows环境GeoServer打包Docker极速入门
windows·docker·容器·geoserver·打包·数据挂载
程序员JerrySUN5 小时前
熟悉的 Docker,陌生的 Podman
linux·docker·容器·系统架构·podman
gobeyye6 小时前
Docker 用法详解
运维·docker·容器
小灰灰__6 小时前
Linux离线安装Docker命令,简单镜像操作
linux·docker·eureka
雪碧聊技术8 小时前
Docker3:docker基础1
运维·docker·容器·docker常见命令