简体   繁体   English

从 groovy 管道文件在 linux Jenkins 服务器上执行 powershell 脚本?

[英]Execute powershell script on a linux Jenkins server from groovy pipeline file?

So after spending half a day as our surprise jenkins devops guy, I'm not sure if this scenario is even possible.因此,在作为我们令人惊讶的 jenkins devops 人员花了半天时间后,我不确定这种情况是否可能发生。 We have a linux Jenkins server that runs a groovy file to build some of our software.我们有一个 linux Jenkins 服务器运行一个 groovy 文件来构建我们的一些软件。 Recently we moved from dropbox to onedrive and one of my tasks is to change all our CI apps to upload files to onedrive.最近我们从 dropbox 转移到 onedrive,我的任务之一是更改我们所有的 CI 应用程序以将文件上传到 onedrive。 Using appveyor and powershell I was able to do this, but Jenkins is giving me problems.使用 appveyor 和 powershell 我能够做到这一点,但是 Jenkins 给我带来了问题。 I was hoping to re-use the powershell scripts I wrote for appveyor, tweak them, and use them in the Jenkins pipeline.我希望重新使用我为 appveyor 编写的 powershell 脚本,调整它们,然后在 Jenkins 管道中使用它们。 I've installed the Powershell plugin on our Jenkins server, but it's still not finding powershell. I've tested the GetAccessToken.ps1 script locally (but I'm on w10), and it's fine.我已经在我们的 Jenkins 服务器上安装了 Powershell 插件,但它仍然找不到 powershell。我已经在本地测试了 GetAccessToken.ps1 脚本(但我在 w10 上),没问题。

Groovy code snippet Groovy 代码片段

stage('Deploy')
        {
            environment
            {
                ONEDRIVE_ID = credentials('OneDriveClientId')
                ONEDRIVE_SECRET = credentials('OneDriveSecretKey')
                ONEDRIVE_TOKEN = powershell "Common/Build/GetAccessToken.ps1 ${ONEDRIVE_ID} ${ONEDRIVE_SECRET}"
            }
            steps
            {
                echo 'Deploying Code....'
                sh "chmod +x Common/Build/UploadToOnedrive.ps1"
                powershell "Common/Build/UploadToOnedrive.ps1 -t ${ONEDRIVE_TOKEN} -s bootloader/redacted/Build/redacted-f redacted -v ${VERSION} -b ${BRANCH_NAME}"
            }
        }

Error错误

Caused: java.io.IOException: Cannot run program "powershell" (in directory "/var/lib/jenkins/workspace/test_branch"): error=2, No such file or directory

I've tried about 10 different ways of calling the script, based on other SO posts and jenkins.io posts, and I get varying degrees of failure.基于其他 SO 帖子和 jenkins.io 帖子,我尝试了大约 10 种不同的脚本调用方式,但我遇到了不同程度的失败。 I'm wondering if I need a Windows slave to run powershell (although I thought the plugin would allow me to).我想知道我是否需要一个 Windows slave 来运行 powershell(尽管我认为该插件允许我这样做)。 Last resort I'll just try to alter the old bash scripts to upload using curl but I was really hoping to use the powershell scripts.最后的手段我只是尝试更改旧的 bash 脚本以使用 curl 上传,但我真的希望使用 powershell 脚本。

Oh, and here's my PATH.哦,这是我的路径。 I think I read that pwsh/powershell gets installed in /usr/local/sbin or /usr/local/bin so that shouldn't be the issue我想我读到 pwsh/powershell 安装在 /usr/local/sbin 或 /usr/local/bin 中,所以这不应该是问题

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Thanks to Shane's sanity check, I was able to install pwsh on the Jenkins ubuntu server and then my groovy pwsh steps started working.感谢 Shane 的完整性检查,我能够在 Jenkins ubuntu 服务器上安装 pwsh,然后我的 groovy pwsh 步骤开始工作。 I'm still having some issues passing variables but that's for another topic.我在传递变量时仍然遇到一些问题,但那是另一个话题。 I'm now able to use scripts like this in my groovy script我现在可以在我的 groovy 脚本中使用这样的脚本

environment {
    ONEDRIVE_TOKEN = pwsh (script: "Common/Build/GetAccessToken.ps1 ${env.ONEDRIVE_ID} ${env.ONEDRIVE_SECRET}", returnStdout: true)
}

Update;更新; here is how I'm able to call a powershell script from groovy with variables这是我如何使用变量从 groovy 调用 powershell 脚本

sh 'pwsh Common/Build/UploadToOnedrive.ps1 -t $ONEDRIVE_TOKEN -s "Release" -f "testname" -v $VERSION -b $BRANCH_NAME'   

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

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