-1

I want to create a Terraform utility module that can clone an AVM repository from GitHub and push it to an existing Azure DevOps repo. But I face many issues and Im not able to debug.

Here is the file structure

avm-utl-clone/
├── main.tf
├── variables.tf
├── terraform.tfvars
├── scripts/
│ └── clone_and_push.ps1
└── outputs.tf

Variables.tf

variable "avm_repo_url" {
 description = "AVM repository URL"
 type = string
}
variable "ado_repo_url" {
 description = "ado organization URL"
 type = string
}
variable "ado_project" {
 description = "ado project name"
 type = string
}
variable "ado_repo_name" {
 description = "ado repository name"
 type = string
}
variable "ado_pat" {
 description = "ado Personal Access Token"
 type = string
 sensitive = true
}

terraform.tfvars:

avm_repo_url = "https://github.com/Azure/terraform-azurerm-avm-res-storage-storageaccount"
ado_repo_url = "https://dev.azure.com/org/"
ado_project = "contoso"
ado_repo_name = "avm-storage-test"
ado_pat = "XXXXXXXXXX"

main.tf

resource "null_resource" "clone_and_push" {
 provisioner "local-exec" {
 command = "powershell -ExecutionPolicy Bypass -File ./scripts/clone_and_push.ps1 -avm_repo_url '${var.avm_repo_url}' -ado_repo_url '${var.ado_repo_url}' -ado_project '${var.ado_project}' -ado_repo_name '${var.ado_repo_name}' -ado_pat '${var.ado_pat}'"
 }
}

clone_and_push.ps1

$ErrorActionPreference = "Stop"
param(
 [string]$avmRepoUrl,
 [string]$ado_repo_url,
 [string]$ado_project,
 [string]$ado_repo_name,
 [string]$ado_pat
)
# Create temp folder
$tempDir = "$env:TEMP\avm-temp"
if (Test-Path $tempDir) {
 Remove-Item -Recurse -Force $tempDir
}
New-Item -ItemType Directory -Path $tempDir | Out-Null
# Clone GitHub repo
git clone $avmRepoUrl $tempDir
# Move into repo folder
Set-Location $tempDir
# Configure Git credentials
$credPath = "$env:USERPROFILE\.git-credentials"
# $credLine = "https://${ado_pat}:@${ado_repo_name}"
$credLine = "https://${ado_pat}:@dev.azure.com"
Set-Content -Path $credPath -Value $credLine
git config --global credential.helper store
# Add Azure DevOps remote and push
git remote add azure "$ado_repo_url/$ado_project/_git/$ado_repo_name"
git push azure --force --all
git push azure --force --tags
Write-Host "Successfully pushed to Azure DevOps repo: $ado_repo_url/$ado_project/_git/$ado_repo_name"
# Write-Host "Successfully pushed to Azure DevOps repo: $ado_repo_url"

I get infinit loop of errors. I tried to hard code the variable on the PS1 file and it works. But it blocks when passing params through main cmd.

Any help?

Martin Atkins
76.3k8 gold badges152 silver badges168 bronze badges
asked Oct 20 at 9:44
1
  • Can you add the error message that you get in an "infinite loop"? It's not clear if your problem is how the parameters are being passed or something wrong in your powershell logic - maybe add some logging that demonstrates whether the parameters are being received correctly. If they're not, you could try using environment { MY_VAR = var.my_var } to pass the variables via the environment and collect them from there inside the powershell script and bypass any escaping problems with apostrophes / quotes / special characters in your parameter values.. Commented Oct 20 at 18:32

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.