【问题标题】:syntax error near unexpected token `<' for shell script block in JenkinsfileJenkinsfile 中 shell 脚本块的意外标记“<”附近的语法错误
【发布时间】:2021-02-11 02:39:40
【问题描述】:

我在 Jenkinsfile 中有以下 shell 脚本代码块

stage("Compose Source Structure")
    {   
        sh '''
            set -x
            rm -vf config
            wget -nv --no-check-certificate https://test-company/k8sconfigs/test-config
            export KUBECONFIG=$(pwd)/test-config
            kubectl config view
            ns_exists=$(kubectl get namespaces | grep ${consider_namespace})
            echo "Validating k8s namespace"
            if [ -z "$ns_exists" ]
            then
                echo "No namespace ${consider_namespace} exists in the cluster ${source_cluster}"
                exit 1                          
            else
                echo "scanning namespace \'${namespace}\'"
                mkdir -p "${HOME}/cluster-backup/${namespace}"
                while read -r resource
                do
                    echo "scanning resource \'${resource}\'"
                    mkdir -p "${HOME}/sync-cluster/${namespace}/${resource}"
                    while read -r item
                    do
                        echo "exporting item \'${item}\'"
                        kubectl get "$resource" -n "$namespace" "$item" -o yaml > "${HOME}/sync-cluster/${namespace}/${resource}/${BUILD_NUMBER}-${source_cluster}-${consider_namespace}-$item.yaml"
                    done < <(kubectl get "$resource" -n "$namespace" 2>&1 | tail -n +2 | awk \'{print $1}\')
                done < <(kubectl api-resources --namespaced=true 2>/dev/null | tail -n +2 | awk \'{print $1}\')
            fi
        '''

不幸的是,我收到如下错误:

++ kubectl get namespaces
++ grep test
+ ns_exists='test              Active   2d20h'
+ echo 'Validating k8s namespace'
Validating k8s namespace
/home/jenkins/workspace/k8s-sync-from-cluster@tmp/durable-852103cd/script.sh: line 24: syntax error near unexpected token `<'

我确实尝试用“”转义“

\<

但仍然没有成功,知道我在这里做错了什么吗?

【问题讨论】:

  • 基本 POSIX sh 不理解 &lt;(command) 进程替换。

标签: shell jenkins-groovy


【解决方案1】:

来自the docs for the sh step(强调我的):

运行 Bourne shell 脚本,通常在 Unix 节点上。接受多行。
可以使用解释器选择器,例如:#!/usr/bin/perl
否则,系统默认 shell 将使用-xe 标志运行(您可以指定set +e 和/或set +x 来禁用这些标志)。

您的 Jenkins 服务器上的系统默认 shell 可能是 sh,而不是 bash。 POSIX sh 将无法识别 &lt;(command) 进程替换。

要专门使用 bash shell,您必须在三重引号后立即包含 #!/usr/bin/env bash shebang。将 shebang 放在下一行不会有任何效果。

我还冒昧地为您的 shell 代码修复了 shellcheck 警告,并删除了不必要的 \' 转义。

试试这个:

stage("Compose Source Structure")
{   
    sh '''#!/usr/bin/env bash
    set -x
    rm -vf config
    wget -nv --no-check-certificate https://test-company/k8sconfigs/test-config
    KUBECONFIG="$(pwd)/test-config"
    export KUBECONFIG
    kubectl config view
    ns_exists="$(kubectl get namespaces | grep "${consider_namespace}")"
    echo "Validating k8s namespace"
    if [ -z "$ns_exists" ]
    then
        echo "No namespace ${consider_namespace} exists in the cluster ${source_cluster}"
        exit 1                          
    else
        echo "scanning namespace '${namespace}'"
        mkdir -p "${HOME}/cluster-backup/${namespace}"
        while read -r resource
        do
            echo "scanning resource '${resource}'"
            mkdir -p "${HOME}/sync-cluster/${namespace}/${resource}"
            while read -r item
            do
                echo "exporting item '${item}'"
                kubectl get "$resource" -n "$namespace" "$item" -o yaml > "${HOME}/sync-cluster/${namespace}/${resource}/${BUILD_NUMBER}-${source_cluster}-${consider_namespace}-$item.yaml"
            done < <(kubectl get "$resource" -n "$namespace" 2>&1 | tail -n +2 | awk '{print $1}')
        done < <(kubectl api-resources --namespaced=true 2>/dev/null | tail -n +2 | awk '{print $1}')
    fi
    '''
}

【讨论】:

  • 漂亮!,就像一个魅力!我们一起成长:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2016-09-06
相关资源
最近更新 更多