'sh:sh:找不到命令'在多分支管道中使用withCredentials块时

时间:2019-09-06 15:29:47

标签: jenkins groovy jenkins-pipeline

我正在建立一个Jenkins多分支管道,以使用Fastlane为Crashlytics构建iOS应用。现在,Crashlytics的API密钥和构建秘诀以明文形式存储在我的Fastfile中,因此我想将它们移至Jenkins凭证,以便任何访问我的GitHub存储库的人都无法获得该信息。

我按照here详细说明创建了凭据并从Jenkinsfile访问它们。我将它们创建为“秘密文本”类型的凭据。这是他们在詹金斯(Jenkins)中的样子(没有足够的代表来张贴图片,所以有一些链接): api token credentials build secret credentials

这是我在Jenkinsfile中访问这些凭据的方式:

pipeline {
    agent any
    stages {
        ...
        stage('Deploy Patient App') {
            steps {
                withCredentials([
                    string(credentialsId: 'crashlytics_api', variable: 'api_token'),
                    string(credentialsId: 'crashlytics_build', variable: 'build_secret')
                ]) {
                    sh '''
                        set +x
                        bundle exec fastlane deploy app:\'Therapy\' api_token:$api_token build_secret:$build_secret
                    '''
                }
            }
        }
        ...
    }
}

现在,当我在詹金斯中运行管道时,我得到以下输出:

[Pipeline] stage
[Pipeline] { (Deploy Patient App)
[Pipeline] withCredentials
Masking supported pattern matches of $api_token or $build_secret
[Pipeline] {
[Pipeline] sh
sh: sh: command not found

我的Jenkinsfile中我在做什么错?还是其他地方有问题?

1 个答案:

答案 0 :(得分:0)

我也是新来的,但我想我可以帮忙...

我做了一些研究,发现: https://github.com/jenkinsci/aws-credentials-plugin/issues/22#issuecomment-317203165

尽管这是另一个插件,但我注意到它们的相似之处在于它们也使用了声明性管道,并且它们不需要script {}块。

用您的代码替换Github注释中的差异如下:

stage('Deploy Patient App') {
    steps {
        withCredentials([
            string(credentialsId: 'crashlytics_api', variable: 'api_token'),
            string(credentialsId: 'crashlytics_build', variable: 'build_secret')
        ]) {
            sh 'bundle exec fastlane deploy app:\'Therapy\' api_token:$api_token build_secret:$build_secret'
        }
    }
}

另一个建议是使用双引号" ",否则它们将收到script.sh: Bad substitution错误。

PS:抱歉,我无法亲自测试此代码,但在Github上确实获得了很多好评。希望这会有所帮助!

相关问题