Go R1 Day 29

  • 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

1
2
3
4
5
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:

1
2
3
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.

1
2
3
4
5
6
7
const (
	repo          = "myrepopath"
	name          = "myreponame"
	buildImage    = "mcr.microsoft.com/vscode/devcontainers/base:0-focal"
	terraformDir  = "terraform/stack"
	config_import = "qa.config"
)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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


Webmentions

(No webmentions yet.)