Skip to content

Commit 4064813

Browse files
authored
PowerShell scripts (#239)
* Added initial versions of PowerShell script to build the provider * Updated Powershell build scripts * Added parameter -SkipTests to scripts\build.ps1 * Fixed trailing whitespaces for scripts\build.ps1 and scripts\unittest.ps1 * Initial check in scripts\local-install.ps1 * scripts\build.ps1 will add .exe extension on Windows * scripts\local-install.ps1 now installes provider on Windows correctly * Added parameter -Install scripts\build.ps1 * Fixed a spelling error of System namespace in scripts\local-install.ps1 * Initial check in of scripts\acctest.ps1 and added tags support in scripts\unittest.ps1 * Added same parameters to scripts\unittest.ps1 as scripts\acctest.ps1 * Added validation to -Tags parameter in scripts\acctest.ps1 * Added -DebugBuild parameter to scripts\build.ps1 * Forcing LF line ending for GO files * Added scripts\gofmtcheck.ps1 * Added missing -s flag to gofmt in scripts\gofmtcheck.ps1 * Added scripts\lint-check-go.ps1 * Added link to Gitter channel and Go Report Card back to README.md * * Added parameter -GoMod to control the -mod build flag * Added missing environment variables during build * Added cleanup process for build environment variables * Removed unsupported -Fix parameter from scripts\lint-check-go.ps1 * Added documentation for developing on Windows Co-authored-by: Thomas Meckel <tmeckel@users.noreply.github.com>
1 parent 9094d0b commit 4064813

File tree

9 files changed

+392
-2
lines changed

9 files changed

+392
-2
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
go.mod text eol=lf
22
go.sum text eol=lf
3-
*.sh text eol=lf
3+
*.sh text eol=lf
4+
*.go text eol=lf

README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# Terraform Provider for Azure DevOps (Devops Resource Manager)
22

3+
[![Gitter](https://badges.gitter.im/terraform-provider-azuredevops/community.svg)](https://gitter.im/terraform-provider-azuredevops/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/microsoft/terraform-provider-azuredevops)](https://goreportcard.com/report/github.com/microsoft/terraform-provider-azuredevops)
5+
36
The AzureRM Provider supports Terraform 0.12.x and later.
47

58
* [Terraform Website](https://www.terraform.io)
69
* [AzDO Website](https://azure.microsoft.com/en-us/services/devops/)
710
* [AzDO Provider Documentation](website/docs/index.html.markdown)
811
* [AzDO Provider Usage Examples](./examples/)
12+
* [Gitter Channel](https://gitter.im/terraform-provider-azuredevops/community)
913

1014
## Usage Example
1115

@@ -50,17 +54,24 @@ resource "azuredevops_build_definition" "build_definition" {
5054
* [Go](https://golang.org/doc/install) version 1.13.x (to build the provider plugin)
5155

5256
If you're on Windows you'll also need:
57+
* [Git for Windows](https://git-scm.com/download/win)
58+
59+
If you what to use the `makefile` build strategy on Windows it's required to install
60+
5361
* [Make for Windows](http://gnuwin32.sourceforge.net/packages/make.htm)
54-
* [Git Bash for Windows](https://git-scm.com/download/win)
5562

5663
For *GNU32 Make*, make sure its bin path is added to PATH environment variable.*
5764

5865
For *Git Bash for Windows*, at the step of "Adjusting your PATH environment", please choose "Use Git and optional Unix tools from Windows Command Prompt".*
5966

67+
As [described below](#build-using-powerShell-scripts) we provide some PowerShell scripts to build the provider on Windows, without the requiremet to install any Unix based tools aside Go.
68+
6069
## Developing the Provider
6170

6271
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.13+ is **required**). You'll also need to correctly setup a [GOPATH](http://golang.org/doc/code.html#GOPATH), as well as adding `$GOPATH/bin` to your `$PATH`.
6372

73+
### Using the GOPATH model
74+
6475
First clone the repository to: `$GOPATH/src/github.com/terraform-providers/terraform-provider-azuredevops`
6576

6677
```sh
@@ -69,6 +80,13 @@ $ git clone git@github.com:terraform-providers/terraform-provider-azuredevops.gi
6980
$ cd $GOPATH/src/github.com/terraform-providers/terraform-provider-azuredevops.git
7081
```
7182

83+
### Using a directory separate from GOPATH
84+
85+
The infrastructure supports building and testing the provider outside `GOPATH` in an arbitrary directory.
86+
In this scenario all required packages of the provider during build will be managed via the `pkg` in `$GOPATH`. As with the [GOPATH Model](#using-the-gopath-model), you can redefine the `GOPATH` environment variable to prevent existing packages in the current `GOPATH` directory from being changed.
87+
88+
### Build using make
89+
7290
Once inside the provider directory, you can run `make tools` to install the dependent tooling required to compile the provider.
7391

7492
At this point you can compile the provider by running `make build`, which will build the provider and put the provider binary in the `$GOPATH/bin` directory.
@@ -98,6 +116,56 @@ The majority of tests in the provider are Acceptance Tests - which provisions re
98116
make testacc SERVICE='resource' TESTARGS='-run=TestAccAzureRMResourceGroup' TESTTIMEOUT='60m'
99117
```
100118

119+
### Build using PowerShell scripts
120+
121+
If you like to develop on Windows, we provide a set of PowerShell scripts to build and test the provider.
122+
They don't offer the luxury of a Makefile environment but are quite sufficient to develop on Windows.
123+
124+
#### `scripts\build.ps1`
125+
126+
The `build.ps1`is used to build the provider. Aside this the script runs (if not skipped) the defined unit tests and is able to install the compiled provider locally.
127+
128+
| Parameter | Description |
129+
|-------------|-------------------------------------------------------------------------------------------|
130+
| -SkipTests | Skip running unit tests during build |
131+
| -Install | Install the provider locally, after a successful build |
132+
| -DebugBuild | Build the provider with extra debugging information |
133+
| -GoMod | Control the `-mod` build parameter: Valid values: '' (Empty string), 'vendor', 'readonly' |
134+
135+
#### `scripts\unittest.ps1`
136+
137+
The script is used to execute unit tests. The script is also executed by `build.ps1` if the `-SkipTest` are not specified.
138+
139+
| Parameter | Description |
140+
|-------------|-----------------------------------------------------------------------------------------------------------------------------------|
141+
| -TestFilter | A GO regular expression which filters the test functions to be executed |
142+
| -Tag | Tests in the provider project are organized with GO build tags. The parameter accepts a list of tag names which should be tested. |
143+
| -GoMod | Control the `-mod` build parameter: Valid values: '' (Empty string), 'vendor', 'readonly' |
144+
145+
#### `scripts\acctest.ps1`
146+
147+
The script is used to execute unit tests.
148+
149+
| Parameter | Description |
150+
|-------------|-----------------------------------------------------------------------------------------------------------------------------------|
151+
| -TestFilter | A GO regular expression which filters the test functions to be executed |
152+
| -Tag | Tests in the provider project are organized with GO build tags. The parameter accepts a list of tag names which should be tested. |
153+
| -GoMod | Control the `-mod` build parameter: Valid values: '' (Empty string), 'vendor', 'readonly' |
154+
155+
#### `scripts\gofmtcheck.ps1`
156+
157+
To validate if all `.go` files adhere to the required formatting rules, execute `gofmtcheck.ps1`
158+
159+
| Parameter | Description |
160+
|-----------|----------------------------------------------------------------------------------------------------------------|
161+
| -Fix | Fix any formatting rule deviations automatically. If the parameter is not set, the script runs in report mode. |
162+
163+
#### `scripts\lint-check-go.ps1`
164+
165+
Like with `gofmtcheck.ps1` the script validate if all `.go` files adhere to the required formatting rules and if any style mistakes exist. In difference to `gofmtcheck.ps1` the script uses Golint instead of Gofmt.
166+
167+
## Environment variables for acceptance tests
168+
101169
The following Environment Variables must be set in your shell prior to running acceptance tests:
102170

103171
- `AZDO_ORG_SERVICE_URL`

scripts/acctest.ps1

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[CmdletBinding()]
2+
param (
3+
[Parameter()]
4+
[ValidateNotNullOrEmpty()]
5+
[string]
6+
$TestFilter = '^TestAcc',
7+
8+
[Parameter()]
9+
[ValidateNotNullOrEmpty()]
10+
[string[]]
11+
$Tag = 'all',
12+
13+
[Parameter()]
14+
[ValidateSet('', 'readonly', 'vendor')]
15+
[string]
16+
$GoMod = 'vendor'
17+
)
18+
19+
$script:PSDefaultParameterValues = @{
20+
'*:Confirm' = $false
21+
'*:ErrorAction' = 'Stop'
22+
}
23+
24+
. (Join-Path -Path $PSScriptRoot -ChildPath 'commons.ps1' -Resolve)
25+
26+
Write-Host "Executing acceptance tests"
27+
Push-Location -Path $SOURCE_DIR
28+
try {
29+
# This is similar to the unit test command aside from the following:
30+
# - TF_ACC=1 is a flag that will enable the acceptance tests. This flag is
31+
# documented here:
32+
# https://www.terraform.io/docs/extend/testing/acceptance-tests/index.html#running-acceptance-tests
33+
#
34+
# - A `-run` parameter is used to target *only* tests starting with `TestAcc`. This prefix is
35+
# recommended by Hashicorp and is documented here:
36+
# https://www.terraform.io/docs/extend/testing/acceptance-tests/index.html#test-files
37+
#
38+
# Using build tags as test filter: https://stackoverflow.com/a/24036237
39+
$env:TF_ACC=1
40+
$env:TF_SCHEMA_PANIC_ON_ERROR=1
41+
$env:GO111MODULE='on'
42+
43+
$argv = @(
44+
'test',
45+
"-mod=$(if ('' -ne $GoMod) { $GoMod } else { $null })",
46+
'-v'
47+
)
48+
if ($TestFilter) {
49+
$argv += @('-run', $TestFilter)
50+
}
51+
if ($Tag -and 0 -lt $Tag.Length) {
52+
$argv += @('-tags', [string]::Join(' ', $Tag))
53+
}
54+
go @argv ./...
55+
if ($LASTEXITCODE) {
56+
throw "Build finished in error due to failed tests"
57+
}
58+
}
59+
finally {
60+
'TF_ACC', 'TF_SCHEMA_PANIC_ON_ERROR', 'GO111MODULE' `
61+
| ForEach-Object -Process {Remove-Item -Path "Env:$_" }
62+
Pop-Location
63+
}

scripts/build.ps1

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[CmdletBinding()]
2+
param (
3+
[Parameter()]
4+
[switch]
5+
$SkipTests,
6+
7+
[Parameter()]
8+
[switch]
9+
$Install,
10+
11+
[Parameter()]
12+
[switch]
13+
$DebugBuild,
14+
15+
[Parameter()]
16+
[ValidateSet('', 'readonly', 'vendor')]
17+
[string]
18+
$GoMod = 'vendor'
19+
)
20+
21+
$script:PSDefaultParameterValues = @{
22+
'*:Confirm' = $false
23+
'*:ErrorAction' = 'Stop'
24+
}
25+
26+
. (Join-Path -Path $PSScriptRoot -ChildPath 'commons.ps1' -Resolve)
27+
28+
function clean() {
29+
Write-Host "Cleaning $BUILD_DIR"
30+
if (Test-Path -Path $BUILD_DIR) {
31+
Remove-Item -Recurse -Force -Path $BUILD_DIR
32+
}
33+
$null = New-Item -ItemType Container -Path $BUILD_DIR
34+
}
35+
36+
function compile() {
37+
$NAME = Get-Content -Raw -Path $PROVIDER_NAME_FILE
38+
$VERSION = Get-Content -Raw -Path $PROVIDER_VERSION_FILE
39+
40+
$BUILD_ARTIFACT="terraform-provider-${NAME}_v${VERSION}"
41+
if ($env:OS -like '*Windows*') {
42+
$BUILD_ARTIFACT += '.exe'
43+
}
44+
Write-Host "Attempting to build $BUILD_ARTIFACT"
45+
Push-Location -Path $SOURCE_DIR
46+
try {
47+
$env:GO111MODULE='on'
48+
49+
go mod download
50+
if ($LASTEXITCODE) {
51+
throw "Failed to download modules"
52+
}
53+
54+
$argv = @(
55+
'build',
56+
"-mod=$(if ('' -ne $GoMod) { $GoMod } else { $null })",
57+
'-o',
58+
"$BUILD_DIR/$BUILD_ARTIFACT"
59+
)
60+
if ($DebugBuild) {
61+
$argv += @( '-gcflags="all=-N -l"' )
62+
}
63+
go @argv
64+
if ($LASTEXITCODE) {
65+
throw "Build failed"
66+
}
67+
}
68+
finally {
69+
'GO111MODULE' `
70+
| ForEach-Object -Process {Remove-Item -Path "Env:$_" }
71+
Pop-Location
72+
}
73+
}
74+
75+
function clean_and_build() {
76+
clean
77+
compile
78+
if (-not $SkipTests) {
79+
& (Join-Path -Path $PSScriptRoot -ChildPath 'unittest.ps1' -Resolve) -GoMod $GoMod
80+
}
81+
Write-Host "Build finished successfully"
82+
if ($Install) {
83+
& (Join-Path -Path $PSScriptRoot -ChildPath 'local-install.ps1' -Resolve)
84+
}
85+
}
86+
87+
clean_and_build

scripts/commons.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[CmdletBinding()]
2+
param (
3+
)
4+
5+
$SCRIPTS_DIR=$PSScriptRoot
6+
$BUILD_DIR=[IO.Path]::GetFullPath([IO.Path]::Combine($SCRIPTS_DIR, '..', 'bin'))
7+
$SOURCE_DIR=[IO.Path]::GetFullPath([IO.Path]::Combine($SCRIPTS_DIR, '..'))
8+
$PROVIDER_NAME_FILE=[IO.Path]::GetFullPath([IO.Path]::Combine($SCRIPTS_DIR, '..', 'PROVIDER_NAME.txt'))
9+
$PROVIDER_VERSION_FILE=[IO.Path]::GetFullPath([IO.Path]::Combine($SCRIPTS_DIR, '..', 'PROVIDER_VERSION.txt'))

scripts/gofmtcheck.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[CmdletBinding()]
2+
param (
3+
[switch]
4+
$Fix
5+
)
6+
7+
$script:PSDefaultParameterValues = @{
8+
'*:Confirm' = $false
9+
'*:ErrorAction' = 'Stop'
10+
}
11+
12+
. (Join-Path -Path $PSScriptRoot -ChildPath 'commons.ps1' -Resolve)
13+
14+
if ($Fix) {
15+
# Check gofmt
16+
echo "==> Fixing gofmt deviations..."
17+
18+
# This filter should match the search filter in ../GNUMakefile
19+
$null = Get-ChildItem -Path $SOURCE_DIR -Recurse -Filter '*.go' `
20+
| Select-Object -ExpandProperty FullName `
21+
| Select-String -NotMatch -SimpleMatch vendor `
22+
| ForEach-Object -Process { gofmt.exe -s -w $_ }
23+
}
24+
else {
25+
# Check gofmt
26+
echo "==> Checking that code complies with gofmt requirements..."
27+
28+
# This filter should match the search filter in ../GNUMakefile
29+
$gofmt_files= Get-ChildItem -Path $SOURCE_DIR -Recurse -Filter '*.go' `
30+
| Select-Object -ExpandProperty FullName `
31+
| Select-String -NotMatch -SimpleMatch vendor `
32+
| ForEach-Object -Process { gofmt.exe -s -l $_ }
33+
34+
if ($gofmt_files) {
35+
echo 'gofmt needs running on the following files:'
36+
echo "${gofmt_files}"
37+
echo "You can use this command and pass the -Fix parameter to reformat code."
38+
exit 1
39+
}
40+
}

scripts/lint-check-go.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[CmdletBinding()]
2+
param (
3+
)
4+
5+
$script:PSDefaultParameterValues = @{
6+
'*:Confirm' = $false
7+
'*:ErrorAction' = 'Stop'
8+
}
9+
10+
. (Join-Path -Path $PSScriptRoot -ChildPath 'commons.ps1' -Resolve)
11+
12+
13+
echo "[INFO] Linting Go Files... If this fails, run 'golint ./... | grep -v 'vendor' ' to see errors"
14+
15+
Push-Location -Path $SOURCE_DIR
16+
try {
17+
go get -u golang.org/x/lint/golint 2>$null
18+
if ($LASTEXITCODE) {
19+
throw "Failed to install or update golint"
20+
}
21+
22+
go list ./... `
23+
| Select-String -NotMatch -SimpleMatch 'vendor' `
24+
| ForEach-Object -Process {
25+
$package = $_
26+
golint.exe -set_exit_status $package
27+
if ($LASTEXITCODE) {
28+
Write-Error -Message "Linting failed for package: $package"
29+
}
30+
}
31+
}
32+
finally {
33+
Pop-Location
34+
}

scripts/local-install.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[CmdletBinding()]
2+
param (
3+
[Parameter()]
4+
[string]
5+
$PluginsDirectory
6+
)
7+
8+
$script:PSDefaultParameterValues = @{
9+
'*:Confirm' = $false
10+
'*:ErrorAction' = 'Stop'
11+
}
12+
13+
. (Join-Path -Path $PSScriptRoot -ChildPath 'commons.ps1' -Resolve)
14+
15+
if (-not $PluginsDirectory) {
16+
# https://www.terraform.io/docs/plugins/basics.html
17+
# https://www.terraform.io/docs/extend/how-terraform-works.html#discovery
18+
if ($env:OS -like '*Windows*') {
19+
$PluginsDirectory = [System.IO.Path]::Combine($env:APPDATA, 'terraform.d', 'plugins')
20+
}
21+
else {
22+
$PluginsDirectory = [System.IO.Path]::Combine($HOME, '.terraform.d', 'plugins')
23+
}
24+
}
25+
26+
if (Test-Path -Path $PluginsDirectory) {
27+
Write-Verbose -Message "Terraform Plugins directory [$PluginsDirectory] already exists"
28+
}
29+
else {
30+
Write-Verbose -Message "Creating Terraform Plugins directory [$PluginsDirectory]"
31+
$null = New-Item -Path $PluginsDirectory -ItemType Directory
32+
}
33+
34+
Write-Host "Installing provider to $PluginsDirectory"
35+
Copy-Item -Path (Join-Path -Path $BUILD_DIR -ChildPath '*') -Destination $PluginsDirectory -Force

0 commit comments

Comments
 (0)