jenkinsfile通用模板

此模板可用于下载代码、编译。

pipeline {
    agent { // 2~33行 编译环境初始化
        kubernetes {
            label "${UUID.randomUUID().toString()}"
            yaml """
                metadata:
                  labels:
                    some-label: some-label-value
                    class: KubernetesDeclarativeAgentTest
                  namespace: scm
                spec:               
                  nodeSelector:
                    jenkins-ci: "true"
                  containers:
                    - name: prepare
                      image: hub.hobot.cc/builder/cicd-common-tool:v12
                      command:
                      - cat
                      tty: true
                      env:
                        - name: CONTAINER_ENV_VAR
                          value: prepare
            """
        }
    }
    options {
      gitLabConnection('Gitlab connector by hobot.ci')
    }
    parameters {  //定义参数,
        string defaultValue: '', description: 'Enter gitlabSourceRepoSshUrl', name: 'gitlabSourceRepoSshUrl', trim: true
        string defaultValue: '', description: 'Enter gitlabSourceBranch', name: 'gitlabSourceBranch', trim: true
    }
    environment { //环境变量
        LANG = 'en_US.UTF-8'
    }
    stages{
        stage('lanuch') {
            steps {
                container('prepare') {  //括号里的是15行定义的 name
                    script {
                        sh 'env'
                        get_code()  
                    }
                }
            }
        }
        stage('building') { //57~65 行是编译环节
            steps {
                container('prepare') { //括号里的是15行定义的 name
                    script {
                        sh 'bash -ex build.sh'  //执行编译脚本,实际编译时需要修改
                    }
                }
            }
        }
    }
}

//函数,pipline中通过函数名调用,没有特殊需求不用修改
def get_code() {
    echo "get code process"
    checkout([
        $class: 'GitSCM',
        branches: [[name: "origin/${env.gitlabSourceBranch}"]],
        doGenerateSubmoduleConfigurations: false,
        extensions: [
            [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false],
            [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', trackingSubmodules: true],
            [$class: 'AuthorInChangelog'],
            [$class: 'UserIdentity', email: 'xxxx@horizon.ai', name: 'xxxx'],
        ],
        submoduleCfg: [],
        userRemoteConfigs: [[credentialsId: 'XXXXX', url: "${env.gitlabSourceRepoSshUrl}"]]
    ])
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值