简体   繁体   English

Jenkins sh运行功能

[英]Jenkins sh run with functions

I have the following script. 我有以下脚本。 Unfortunately I not able to get it run on Jenkins. 不幸的是,我无法在詹金斯上运行它。

#!/bin/bash
function pushImage () {
    local serviceName=$1
    local version=$(getImageVersionTag $serviceName)
    cd ./dist/$serviceName
    docker build -t $serviceName .
    docker tag $serviceName gcr.io/$PROJECT_ID/$serviceName:$version
    docker tag $serviceName gcr.io/$PROJECT_ID/$serviceName:latest
    docker push gcr.io/$PROJECT_ID/$serviceName
    cd ../..
}

function getImageVersionTag () {
    local serviceName=$1

    if [ $BUILD_ENV = "dev" ];
    then
        echo $(timestamp)
    else
        if [ $serviceName = "api" ];
        then
            echo $(git tag -l --sort=v:refname | tail -1 | awk -F. '{print $1"-"$2"-"$3"-"$4}')
        else
            echo $(git tag -l --sort=refname | tail -1 | awk -F. '{print $1"-"$2"-"$3"-"$4}')
        fi
    fi
}

function timestamp () {
    echo $(date +%s%3N)
}

set -x
## might be api or static-content
pushImage $1

I'm receiving this error on Jenkins 我在詹金斯上收到此错误

10:10:17 + sh push-image.sh api
10:10:17 push-image.sh: 2: push-image.sh: Syntax error: "(" unexpected

I already configured Jenkins global parameter to /bin/bash as default shell execute environment, but still having same error. 我已经将Jenkins全局参数配置为/bin/bash作为默认的shell执行环境,但是仍然存在相同的错误。

The main issue here in usage of functions, as other scripts that has been executed successfully don't have any. 这里的主要问题是函数的用法,因为其他已成功执行的脚本没有任何脚本。

How this can be fixed? 如何解决?

Short answer: make sure you're running bash and not sh 简短答案:确保您正在执行bash而不是sh

Long answer: sh (which is run here despite your effort of adding a shebang) is the bourne shell and does not understand the function keyword. 长答案: sh (尽管您努力添加shebang仍在此处运行)是bourne shell ,并且不理解function关键字。 Simply removing it will solve your issue. 只需将其删除即可解决您的问题。

Please note however that all your variable expansions should be quoted to prevent against word splitting and globbing. 但是请注意,所有变量扩展都应加引号,以防止出现单词拆分和乱码现象。 Ex: local version=$(getImageVersionTag "$serviceName") 例如: local version=$(getImageVersionTag "$serviceName")

See shellcheck.net for more problems appearing in your file (usage of local var=$(...) ) and explicit list of snippets which are missing quotes. 请参阅shellcheck.net,以获取文件中出现的更多问题(使用local var=$(...) )以及缺少引号的摘要列表。

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

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