如何将参数传递给从 Jenkinsfile 调用的 shell 脚本?

问题描述 投票:0回答:1

我的

Jenkinsfile
中有以下代码:

def requirementsPusher(some_argument) {
    sshagent(credentials: ['jenkins-key-new']){
    sh("./build-scripts/pushreqs.sh")
    }
}

我正在尝试将上面的

some_argument
传递到我正在调用的
pushreqs.sh
shell 脚本中。

仅供参考,我的

pushreqs.sh
看起来像这样:

#!/bin/bash

set -x  # echo on

. ${0%/*}/common.sh

staged=$(git diff --name-only --staged)
if [ "$staged" == "" ] ; then
    echo "No changes staged, nothing to do"
    exit 0
elif [ "$staged" == "requirements.txt" ] ; then
    echo committing and pushing requirements.txt .. should be only on master and not on rebuild
    git commit --no-verify -m 'Version updates by Jenkins master build' requirements.txt || exit 3
    git push origin HEAD:build_test_latest || exit 2 
    exit 0
else
    echo "Fail - unexpected changes staged: ${staged}"
    exit 5 
fi

我正在尝试将

some_argument
(其中包含 Jira 票证的名称)添加到我的
git commit
消息中。

只是想知道上述情况是否可能 - 我四处寻找一些有关将参数传递到 shell 脚本的类似帖子,但找不到任何特别可以回答上述问题的帖子。

shell scripting jenkins-pipeline jenkins-groovy
1个回答
0
投票

您必须首先更新 shell 脚本以获取位置参数。最小版本:

#!/usr/bin/env bash

ticket=$1

git commit -m "$ticket Version updates by Jenkins master build" requirements.txt

并且您必须更新调用以传入参数:

sh """
    ./build-scripts/pushreqs.sh "${some_argument}"
"""

引用确保如果

some_argument
包含空格或 shell 特殊字符,不会发生任何奇怪的情况。

© www.soinside.com 2019 - 2024. All rights reserved.