pipeline script for SCM 构建go项目 并推送harbor

pipeline script 和 pipeline script for SCM    推荐使用第二种 

首先需要再项目的根目录下新建Jenkinsfile 文件 

pipeline for SCM   拉取github 代码 

自动生成Jenkinsfile 的语法 

 生成jenkinsfile 的拉取脚本 

项目中编写Jenkinsfile文件 

pipeline {
    agent any
    stages {
        stage('拉取代码') {
            steps {
               checkout scmGit(branches: [[name: '*/master']], browser: github('https://github.com/moreyLee/game-api.git'), extensions: [], userRemoteConfigs: [[credentialsId: '5edb9434-b237-4bd7-a166-5d2c91d0ec8f', url: 'https://github.com/moreyLee/game-api.git']])
            }
        }
    }
}

替换项目中的配置文件 config.yaml

生成流水线脚本 

Jenkinsfile 文件

pipeline {
    agent any
    stages {
        stage('拉取代码') {
            steps {
               checkout scmGit(branches: [[name: '*/master']], browser: github('https://github.com/moreyLee/game-api.git'), extensions: [], userRemoteConfigs: [[credentialsId: '5edb9434-b237-4bd7-a166-5d2c91d0ec8f', url: 'https://github.com/moreyLee/game-api.git']])
            }
        }
        stage('替换config.yaml配置文件为测试环境'){
            steps{
               configFileProvider([configFile(fileId: '41906e5e-6fcb-4825-b174-d855f0a900ab', targetLocation: 'conf/config.yaml')]) {
               }
            }
        }

替换dockerfile文件

pipeline {
    agent any
    stages {
        stage('拉取代码') {
            steps {
               checkout scmGit(branches: [[name: '*/master']], browser: github('https://github.com/moreyLee/game-api.git'), extensions: [], userRemoteConfigs: [[credentialsId: '5edb9434-b237-4bd7-a166-5d2c91d0ec8f', url: 'https://github.com/moreyLee/game-api.git']])
            }
        }
        stage('config.yaml配置文件替换为测试环境'){
            steps{
               configFileProvider([configFile(fileId: '41906e5e-6fcb-4825-b174-d855f0a900ab', targetLocation: 'conf/config.yaml')]) {
                    echo 'config.yaml 配置文件替换成功'
               }
            }
        }
        stage('dockerfile替换为测试环境'){
            steps{
              configFileProvider([configFile(fileId: 'ed3935e4-2fb5-4c34-87ea-74bef589d946', targetLocation: 'dockerfile')]) {
                    echo 'dockerfile 配置文件替换成功'
              }
            }
        }

构建打包成二进制文件

pipeline {
    agent any
    environment {
         PATH =  "PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:$PATH"
    }
    stages {
        stage('打印环境变量'){
                steps{
                    script {
                        sh 'printenv'
                    }
                }
        }
        stage('拉取代码') {
            steps {
               checkout scmGit(branches: [[name: '*/master']], browser: github('https://github.com/moreyLee/game-api.git'), extensions: [], userRemoteConfigs: [[credentialsId: '5edb9434-b237-4bd7-a166-5d2c91d0ec8f', url: 'https://github.com/moreyLee/game-api.git']])
            }
        }
        stage('config.yaml配置文件替换为测试环境'){
            steps{
               configFileProvider([configFile(fileId: '41906e5e-6fcb-4825-b174-d855f0a900ab', targetLocation: 'conf/config.yaml')]) {
                    echo 'config.yaml 配置文件替换成功'
               }
            }
        }
        stage('dockerfile替换为测试环境'){
            steps{
              configFileProvider([configFile(fileId: 'ed3935e4-2fb5-4c34-87ea-74bef589d946', targetLocation: 'dockerfile')]) {
                    echo 'dockerfile 配置文件替换成功'
              }
            }
        }
        stage('构建代码'){
            steps{
                sh '''
                docker version
                ls -lh /bin/sh
                source /etc/profile
                go version
                export GO111MODULE=on
                go build -o k8-game-api
                '''
            }
        }
    }
}

构建镜像 

将程序打包成镜像 隔绝应用程序对系统环境的依赖。。。

pipeline {
    agent any
    environment {
         PATH =  "PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:$PATH"
    }
    stages {
        stage('打印环境变量'){
                steps{
                    script {
                        sh 'printenv'
                    }
                }
        }
        stage('拉取代码') {
            steps {
               checkout scmGit(branches: [[name: '*/master']], browser: github('https://github.com/moreyLee/game-api.git'), extensions: [], userRemoteConfigs: [[credentialsId: '5edb9434-b237-4bd7-a166-5d2c91d0ec8f', url: 'https://github.com/moreyLee/game-api.git']])
            }
        }
        stage('config.yaml配置文件替换为测试环境'){
            steps{
               configFileProvider([configFile(fileId: '41906e5e-6fcb-4825-b174-d855f0a900ab', targetLocation: 'conf/config.yaml')]) {
                    echo 'config.yaml 配置文件替换成功'
               }
            }
        }
        stage('dockerfile替换为测试环境'){
            steps{
              configFileProvider([configFile(fileId: 'ed3935e4-2fb5-4c34-87ea-74bef589d946', targetLocation: 'dockerfile')]) {
                    echo 'dockerfile 配置文件替换成功'
              }
            }
        }
        stage('构建代码'){
            steps{
                sh '''
                docker version
                echo "脚本日志解析器"
                ls -lh /bin/sh
                echo "容器中环境变量是否生效"
                source /etc/profile
                go version
                export GO111MODULE=on
                go build -o k8-game-api
                '''
            }
        }
        stage('构建镜像'){
            steps{
                sh '''
                docker build -t k8-game-api .
                '''
            }
        }
       
    }
}

登录harbor  推送镜像至harbor 并添加时间戳

def Version(){
    return new Date().format('yyyy-MM-dd HH:mm:ss')
}
pipeline {
    agent any
    environment {
        _version = Version()
    }
    stages {
        stage('打印环境变量'){
                steps{
                    script {
                        sh 'printenv'
                    }
                }
        }
        stage('拉取代码') {
            steps {
               checkout scmGit(branches: [[name: '*/master']], browser: github('https://github.com/moreyLee/game-api.git'), extensions: [], userRemoteConfigs: [[credentialsId: '5edb9434-b237-4bd7-a166-5d2c91d0ec8f', url: 'https://github.com/moreyLee/game-api.git']])
            }
        }
        stage('config.yaml配置文件替换为测试环境'){
            steps{
               configFileProvider([configFile(fileId: '41906e5e-6fcb-4825-b174-d855f0a900ab', targetLocation: 'conf/config.yaml')]) {
                    echo 'config.yaml 配置文件替换成功'
               }
            }
        }
        stage('dockerfile替换为测试环境'){
            steps{
              configFileProvider([configFile(fileId: 'ed3935e4-2fb5-4c34-87ea-74bef589d946', targetLocation: 'dockerfile')]) {
                    echo 'dockerfile 配置文件替换成功'
              }
            }
        }
        stage('构建代码'){
            steps{
                sh '''
                docker version
                echo "脚本日志解析器"
                ls -lh /bin/sh
                echo "容器中环境变量是否生效"
                source /etc/profile
                go version
                export GO111MODULE=on
                go build -o k8-game-api
                '''
            }
        }
        stage('构建镜像'){
            steps{
                sh '''
                docker build -t k8-game-api .
                '''
            }
        }
        stage('推送镜像'){
            steps{
                echo "时间戳\n+${_version}"
                echo "推送镜像到镜像仓库-登录"
                sh 'docker login -uadmin -pDevops%8 https://harbor.dev.com/'
                echo "镜像打标签 这步是必须的 推送至harbor "
                sh 'docker tag k8-game-api:latest harbor.dev.com/k8s/k8-game-api:latest'
                echo "推送到harbor"
                sh 'docker push harbor.dev.com/k8s/k8-game-api:latest'
                echo '清除镜像标签'
                sh 'docker rmi -f harbor.dev.com/k8s/k8-game-api:latest'
                echo '清理none 镜像'
            }
        }
    }
}

报错:pipeline无法识别sh 命令 无法执行任何系统命令

原因:因为是容器 jenkins的默认系统变量都在容器中 没有在宿主机上面。

因此修改 docker-compose.yaml文件  将go的变量挂载到容器上面 

 volumes:                                   # 卷挂载路径
      - /var/vol_dockers/jenkins_home/:/var/jenkins_home  # 这是我们一开始创建的目录挂载到容器内的jenkins_home目录
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker                # 这是为了我们可以在容器内使用docker命令
      - /usr/local/go:/usr/local/go   # 使用go go的安装目录 
      - /usr/local/bin/docker-compose:/usr/local/bin/docker-compose

jenkins 容器中没有source 命令  

容器中是ubuntu系统 默认使用dash 作为解释器 不兼容source

登录容器 修改 dash解释器为bash 解释器

# cd /bin 
# ls -lh sh
lrwxrwxrwx. 1 root root 4 Jan  5  2023 sh -> dash
# rm sh
# ln -s /bin/bash /bin/sh
# ls -lh sh
lrwxrwxrwx. 1 root root 9 Mar 18 14:51 sh -> /bin/bash

source命令可以执行了。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

村长在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值