jenkins使用流水线部署go程序
# jenkins界面新建项目
# 编写脚本式流水线脚本
node("jenkins-slave4") {
properties([
parameters([
choice(name: 'serviceName', choices: ['goland-demo'], description: ''),
choice(name: 'targetHosts', choices: ['test001'], description: ''),
choice(name: 'targetDir', choices: ['/data/application'], description: ''),
gitParameter(branch:'', branchFilter: 'origin/(.*)', defaultValue: 'main', description: '选择将要构建的分支', name: 'TAG', quickFilterEnabled: true, selectedValue: 'TOP', sortMode: 'DESCENDING_SMART', tagFilter: '*', type: 'PT_BRANCH_TAG', useRepository: 'ssh://git@xxxxxx/goland-demo.git')
])
])
stage('Checkout') {
checkout(
[$class: 'GitSCM', doGenerateSubmoduleConfigurations: false, submoduleCfg: [], extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
branches: [[name: "main"]],userRemoteConfigs: [[url: "ssh://git@xxxxxx/goland-demo.git"]]]
)
}
stage('ansible') {
dir("/var/jenkins_home/ansible"){
checkout(
[$class: 'GitSCM', doGenerateSubmoduleConfigurations: false, submoduleCfg: [], extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
branches: [[name: "master"]],userRemoteConfigs: [[url: "ssh://git@xxxxxx/ansible.git"]]]
)
}
}
stage('编译') {
sh """
export GOPATH=/usr/local/go
export PATH=$PATH:\$GOPATH/bin
go build -o ${serviceName}/main ./main.go
cp -rf static service.sh ${serviceName}/
tar -zcf ${serviceName}.tar.gz ${serviceName}
"""
}
stage('部署') {
ansiblePlaybook become: true, becomeUser: null, credentialsId: 'deploy_local', extras: '--extra-vars "WORKSPACE=${WORKSPACE} serviceName=${serviceName} targetHosts=${targetHosts} targetDir=${targetDir}"', inventory: '/var/jenkins_home/ansible/local_hosts', playbook: '/var/jenkins_home/ansible/xxx/test.yml', sudoUser: null, disableHostKeyChecking: true
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
test.yml剧本文件如下
---
- hosts: "{{ targetHosts }}"
remote_user: admin
gather_facts: false
become: yes
tasks:
- name: "拷贝部署文件"
copy:
src: "{{item.src}}"
dest: "{{item.dest}}"
owner: admin
group: admin
mode: 0755
with_items:
- {src: "{{ WORKSPACE }}/{{ serviceName }}.tar.gz",dest: "/tmp/application/"}
- name: "Execute the script"
shell: |
rm -rf {{ targetDir }}/*
cd {{ targetDir }}
cp -rf /tmp/application/{{ serviceName }}.tar.gz {{ targetDir }}/ && tar -xf {{ serviceName }}.tar.gz
cd {{ serviceName }} && chmod +x service.sh && ./service.sh stop && ./service.sh start {{ targetDir }}/{{ serviceName }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
go服务启动停止service.sh脚本
#!/bin/bash
targetDir=$2
start(){
cd ${targetDir}
nohup ./main >>/dev/null 2>&1& echo $! > service.pid
cd -
}
stop(){
pid=`cat service.pid`
if [ -z $pid ]
then
echo "pid"
else
kill -9 ${pid}
kill -9 ${pid}
kill -9 ${pid}
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
*)
echo "[start|stop|restart]"
;;
esac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
将以上脚本文件push到运维git仓库管理
# 发布
上次更新: 2023/07/03, 16:29:05