0

What specific syntax needs to be changed in the windows command line commands below in order for git remote add origin %repoWithToken% to resolve to the intended valid URL?

COMMANDS THAT ARE FAILING:

The following commands are run in a pwsh shell on a windows-latest GitHub runner.

set repoWithToken="https://"$GIT_PAT"@github.com/accountName/repoName.git"
git init
git remote add origin %repoWithToken%

ERROR MESSAGE:

The following error message is given by the GitHub windows-latest runner when the above code is run:

fatal: '%repoWithToken%' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Error: Process completed with exit code 1.

The answer can just be simple windows command line command. No need for any fancy PowerShell.

Also, $GIT_PAT is correct GitHub syntax, though it is possible that the concatenation in set repoWithToken="https://"$GIT_PAT"@github.com/accountName/repoName.git" might need to be done differently in the code above. This is in the process of being translated from Bash.

9
  • 1
    In PowerShell set is an alias for Set-Variable. Is there a special reason why you're not using the native PowerShell cmdlets? If you want to use a variable in PowerShell it will not work with %repoWithToken%. You have to use $repoWithToken. And you can create the variable like this: $repoWithToken = "https://$($GIT_PAT)@github.com/accountName/repoName.git" Commented Sep 16, 2022 at 23:19
  • @Olaf The reason is I just want to keep it simple. I am a bash and python person who occasionally needs to write a simple windows script. If you have a simple solution about which you feel conviction, I would be happy to see it. Commented Sep 16, 2022 at 23:22
  • Since you seem familiar with CMD the simplest solution would be to use CMD instead of PowerShell. Commented Sep 16, 2022 at 23:25
  • @Olaf There are a couple other powershell commands upstream in the same script, so that making these lines become powershell lines is an option. Separately, a windows command prompt version would need to do the concatenation correctly. The above concatenation of the $GIT_PAT variable works in bash, but $GIT_PAT is a GitHub variable, so I am not sure if %$GIT_PAT% would work. Commented Sep 16, 2022 at 23:32
  • 2
    I'm not sure if I really get it. If you want to use $GIT_PAT literally you can assign the variable in PowerShell like this: $repoWithToken = 'https://[email protected]/accountName/repoName.git' ( enclosed in single quotes ). And you can use it like this: git remote add origin $repoWithToken Commented Sep 16, 2022 at 23:41

1 Answer 1

1

Building on Olaf's helpful comments:

  • set <varName>=<value is used in cmd.exe for assigning variables; while in PowerShell set is a built-in alias for Set-Variable, the latter has very different syntax.

    • More importantly, its use is rarely needed, because variables are assigned as $<varName> = <value>

    • PowerShell uses the same syntax on setting and getting variable values, $<varName>; thus, after having assigned to a variable named repoWithToken with $repoWithToken = <value>, you also refer to its value later with $repoWithToken (by contrast, %repoWithToken% is again cmd.exe syntax).

  • Assuming that GIT_PAT is the name of an environment variable, you must refer to it as $env:GIT_PAT in PowerShell variable (with explicit name delineation: ${env:GIT_PAT})

  • Unlike Bash (and POSIX-compatible shells in general), PowerShell only allows you to compose a single string from a mix of quoted and unquoted tokens if the first token is unquoted.

    • Something like "https://"$GIT_PAT"@github.com/accountName/repoName.git" therefore doesn't work as a single string argument, because its first token is quoted, causing PowerShell to break it into two arguments in this case.

    • Since string interpolation is needed here in order to replace ${env:GIT_PAT} with its value, simply enclose the entire value in "...", i.e. make it an expandable string

Therefore:

$repoWithToken = "https://${env:GIT_PAT}@github.com/accountName/repoName.git"
git init
git remote add origin $repoWithToken
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.