简体   繁体   English

从 Jenkins 管道调用时,Bash 脚本从不接收参数

[英]Bash script never receives arguments when called from Jenkins pipeline

I'm running a declarative Jenkins pipeline on Ubuntu 18.04 slave.我在 Ubuntu 18.04 slave 上运行声明式 Jenkins 管道。 My issue is that whenever I'm trying to provide arguments from a sh step to a bash script, the arguments are not there.我的问题是,每当我尝试从sh步骤向 bash 脚本提供参数时,参数都不存在。 When running the exact same commands from either a terminal directly or another script file (similarly how Jenkins does it via temp file) the arguments work fine.当直接从终端或另一个脚本文件(类似于 Jenkins 通过临时文件执行此操作)运行完全相同的命令时,参数工作正常。

The Jenkinsfile looks something like Jenkinsfile 看起来像

pipeline {
    agent { label "ubuntu" }
    options { timeout(time: 1, unit: 'HOURS') }
    stages {
        stage('Build') {
            steps {
                sh """
                #!/bin/bash
                ...
                . ./Scripts/install_tools.sh "force"
                """
            }
        }
    }
}

The pipeline itself runs smooth and does what I need it to do.管道本身运行平稳,可以完成我需要它做的事情。 The problem is that when calling install_tools.sh no arguments are found.问题是调用install_tools.sh时找不到参数。 The script looks something like脚本看起来像

#!/bin/bash
echo "Running $0"
echo "Args: $@"

...

The line echo "Args: $@" I have tried also with $* and $1 - Each time the arguments are returned as empty, but only when running from the pipeline.echo "Args: $@"我也尝试过使用$*$1 - 每次参数返回为空,但仅在从管道运行时。 It seems to me that this is related to some Groovy stuff, but I have no clue what.在我看来,这与一些 Groovy 的东西有关,但我不知道是什么。

How do I call a bash script during the pipeline and get the arguments passed properly?如何在管道期间调用 bash 脚本并正确传递参数?

put #!/bin/bash into the first line#!/bin/bash放入第一行

sh """#!/bin/bash
   . ./Scripts/install_tools.sh "force"
   """

otherwise .否则 。 (dot) command could have different meaning (点)命令可能有不同的含义

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

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