本文首发在这里
先决条件
- 装好 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
- 添加凭据
系统管理 -> 凭据管理 -> 全局 -> 添加凭据 -> 范围选择全局
- Username with password
- 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'