【发布时间】:2018-10-26 16:09:46
【问题描述】:
我正在尝试用 jenkinsfile 中的“_”替换 Git 分支名称中的“/”,以便我可以用分支名称标记我的 docker 映像。在 bash 中,以下命令可以正常工作
echo "${git_branch_name//\//_}"
但是当在 jenkinsfile 中使用上面的命令时,它会抛出一个错误。
#!/usr/bin/env groovy
def commit_id
def imagetag
def branch_name
def git_branch_name
node('Nodename') {
stage('checkout') {
checkout (scm).$Branch_Param
sh "git rev-parse --short HEAD > .git/commit-id"
commit_id = readFile('.git/commit-id').trim()
sh "git rev-parse --abbrev-ref HEAD > .git/branch-name"
git_branch_name = readFile('.git/branch-name').trim()
branch_name= sh "echo ${git_branch_name//\//_}"
sh "echo ${commit_id}"
sh "echo ${branch_name}"
sh "echo Current branch is ${branch_name}"
}
}
WorkflowScript: 15: end of line reached within a simple string 'x' or "x" or /x/;
solution: for multi-line literals, use triple quotes '''x''' or """x""" or /x/ or $/x/$ @ line 15, column 28.
sh "branch_name = echo ${git_branch_name//\//_}"
我在这里做错了什么?我应该使用 Groovy 正则表达式而不是 shell 吗?为什么 shell 没有被正确解释?
谢谢
【问题讨论】:
-
我能够使用 groovy branch_name = git_branch_name.replaceAll("/", "_") 来解决这个问题,但我想知道乳清壳解释不起作用
-
因为
sh是 Bourne shell 而不是 Bash,也许吧? documentation 表示您可以选择带有 shebang 行的解释器,所以添加#!/bin/bash可能会起作用吗? sh 不理解${parameter//pattern/replacement}扩展,只有 Bash 可以。
标签: jenkins sh jenkins-pipeline