简体   繁体   中英

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. Even with the ARGs passed in during the build. 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:

# 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).

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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