简体   繁体   English

为什么 powershell env 可以在本地运行,但不能与 dockerfile 一起使用?

[英]Why does powershell env works locally but not with dockerfile?

So I ran this command on my local machine.所以我在本地机器上运行了这个命令。 Scrubbed link for obvious reasons.出于显而易见的原因删除了链接。

scalar clone --full-clone https://$env:USERNAME:$env:PAT_TOKEN@<MY GIT REPO LINK HERE>

But running this doesn't work in a dockerfile.但是在 dockerfile 中运行它不起作用。 Even with the ARGs passed in during the build.即使在构建过程中传入了 ARG。 And yes, I've checked them by printing them out.是的,我已经通过打印出来检查过它们。

RUN scalar clone --full-clone https://$env:USERNAME:$env:PAT_TOKEN@<MY GIT REPO LINK HERE>

But if I just add a back slash in the right place, it works...但是如果我只是在正确的位置添加一个反斜杠,它就可以工作......

RUN scalar clone --full-clone https://$env:USERNAME\:$env:PAT_TOKEN@<MY GIT REPO LINK HERE>

Any ideas?有任何想法吗?

I don't think it works locally, because you appear to have hit a bug (as of PowerShell 7.0) , which can more simply be demonstrated thus:我不认为它在本地工作,因为您似乎遇到了一个错误(从 PowerShell 7.0 开始) ,可以更简单地证明如下:

# Command based on *Windows* environment vars., but the bug affects Unix too.
PS> Write-Output $env:OS:$env:USERNAME@more
jdoe@more  # !! only the *2nd* variable value is output.

The bug surfaces when two $env:<name> references are directly joined with a : (your insertion of \\ before the : bypassed the bug).当两个$env:<name>引用直接与:连接时,错误就会$env:<name> (您在:之前插入的\\绕过了错误)。

The workaround is to enclose the variable name (including the env: namespace prefix) in {...} , which is PowerShell's mechanism for disambiguating variable names from adjacent characters in (implicit) expandable strings:解决方法是将变量名称(包括env:命名空间前缀)括在{...} ,这是 PowerShell 用于从(隐式)可扩展字符串中的相邻字符中消除变量名称歧义的机制:

# Workaround: enclose the variable name in {...}
PS> Write-Output ${env:OS}:$env:USERNAME@more
Windows_NT:jdoe@more  # OK.

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

相关问题 在 Powershell 本地工作的 -split &#39; &#39; 在 Azure 管道 Powershell 任务中不起作用 - -split ' ' that works locally in Powershell does not work in a Azure pipeline Powershell task 为什么我的ruby应用程序可以在本地运行并且不能在Heroku上运行? - Why my ruby application works locally and does not run on Heroku? 为什么 heroku 我的构建失败并说它在本地工作时找不到文件? - Why does heroku fail my build and says it cannot find a file when it works locally? 如何在Dockerfile中本地安装pip包? - How to install a pip package locally in Dockerfile? 为什么 pip 在后续的 conda env 导出中导出不同的版本? - why does pip export with a different version on subsequent conda env exports? 为什么git://有效,但git @没有 - Why does git:// works but git@ does not 为什么 GitHub 页面没有反映我在本地所做的更改 - Why GitHub page does not reflect the changes I made locally 为什么git pull不更新本地更改的文件? - Why does git pull not update locally changed files? 为什么 git clone 只能在以管理员身份运行的 Powershell 中工作? - Why git clone works only in Powershell running as administrator? 为什么Source Tree不能通过Git区分Powershell脚本(ps1) - Why does Source Tree not Git Diff a Powershell Script (ps1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM