Skip to content

Go R1 Day 29

progress🔗

  • Evaluated Mage as a replacement for bash/pwsh based tasks for automation with Azure Pipelines.
  • Was able to get terraform to run with dynamic configuration using the following approach:

Install with

go get -u github.com/magefile/mage/mg
go mod init mage-build
go get github.com/magefile/mage/mg
go get github.com/magefile/mage/sh
go mod tidy

Then to get mage-select run:

GO111MODULE=off go get github.com/iwittkau/mage-select
cd $GOPATH/src/github.com/iwittkau/mage-select
mage install

Configure some constants, which I'd probably do differently later. For now, this is a good rough start.

const (
    repo          = "myrepopath"
    name          = "myreponame"
    buildImage    = "mcr.microsoft.com/vscode/devcontainers/base:0-focal"
    terraformDir  = "terraform/stack"
    config_import = "qa.config"
)
func TerraformInit() error {
    params := []string{"-chdir=" + terraformDir}
    params = append(params, "init")
    params = append(params, "-input=false")
    params = append(params, "-var", "config_import="+config_import+".yml")

    // Backend location configuration only changes during the init phase, so you do not need to provide this to each command thereafter
    // https://github.com/hashicorp/terraform/pull/20428#issuecomment-470674564
    params = append(params, "-backend-config=./"+config_import+".tfvars")
    fmt.Println("starting terraform init")
    err := sh.RunV("terraform", params...)
    if err != nil {
        return err
    }
    return nil
}

Once terraform was initialized, it could be planned.

func TerraformPlan() error {
    mg.Deps(TerraformInit)
    params := []string{"-chdir=" + terraformDir}
    params = append(params, "plan")
    params = append(params, "-input=false")
    params = append(params, "-var", "config_import="+config_import+".yml")
    fmt.Println("starting terraform plan")
    err := sh.RunV("terraform", params...)
    if err != nil {
        return err
    }
    return nil
}
  • Of interest as well was mage-select, providing a new gui option for easier running by others joining a project.

mages-select-on-console