章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • ansible系列文章

  • Kubernetes笔记

  • elk

  • jenkins

    • jenkins容器安装
    • jenkins流水线语法介绍
    • jenkins常用插件汇总及介绍
    • jenkins配置连接slave从节点
    • jenkins配置android发布
    • jenkins配置ios发布
    • jenkins使用rpm方式安装
    • jenkins部署java程序
    • jenkins部署vue程序
    • jenkins使用流水线部署go程序
    • GitLabCI_CD

    • 专题
    • jenkins
    章工运维
    2023-07-03
    目录

    jenkins使用流水线部署go程序

    # jenkins界面新建项目

    b711dbb9c60fa891.jpg

    # 编写脚本式流水线脚本

    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

    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

    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

    将以上脚本文件push到运维git仓库管理

    # 发布

    5a42bb4e4b807012.jpg

    586f99b0a024a383.jpg

    微信 支付宝
    上次更新: 2025/03/02, 17:48:52

    ← jenkins部署vue程序 Gitlab ci与Jenkins对比→

    最近更新
    01
    不花一分钱从0到1建站教程
    04-22
    02
    批量拿取多台服务器的日志文件
    04-21
    03
    高德MCP智能体搞定旅游计划
    04-19
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式