简体   繁体   English

将环境变量传递给 jenkins 管道 bash 脚本

[英]Pass environment variable to jenkins pipeline bash script

Hey I'm trying to make changes to the environment variable GIT_BRANCH and parse the right side of the /, i know this can be achieved with cut like this: $(echo ${env.GIT_BRANCH} | cut -d \"/\" -f 2 )嘿,我正在尝试更改环境变量 GIT_BRANCH 并解析 / 的右侧,我知道这可以通过这样的 cut 来实现: $(echo ${env.GIT_BRANCH} | cut -d \"/\" -f 2 )

Thing is, cannot make it work in Jenkins pipelines, error: bad substitution问题是,不能让它在 Jenkins 管道中工作,错误:替换错误

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh "docker build -t jpq/jpq:test ."
            }
        }
        stage('Test') {
            steps {
                sh  "docker run jpq/jpq:test python3 tests.py"
            }
        }
         stage('Push') {
             steps {
                sh '''#!/bin/bash
                  BRANCH=\$(echo \${env.GIT_BRANCH} | cut -d \"/\" -f 2 )
                  echo ${BRANCH}
                  docker tag jpq/jpq:test jpq/jpq:${BRANCH}
                  docker push jpq/jpq:test
                    '''
             }
         }
        // stage('Deploy') {
        //     steps {
        //     }
        // }
    }

} }

How can I correctly generate the BRANCH variable and pass it to the docker tag?如何正确生成 BRANCH 变量并将其传递给 docker 标签?

This should work:这应该有效:

stage('Push') {
     steps {
        sh '''#!/bin/bash
            #printenv
            BRANCH=$(echo ${GIT_BRANCH} | cut -d "/" -f2)
            echo "Branch: ${BRANCH}"
        '''
    }
}

Note: To see what all environment variables are available to the shell block, you may use printenv .注意:要查看 shell 块可用的所有环境变量,您可以使用printenv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM