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
lang-bash
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..