terraspace_ci_azure 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 29397c0d042aab0f08259211bd43983b6716ffad53280773eb7869abf56b1c72
4
+ data.tar.gz: d61509334b832dd3aaf7b951a3bbd7b2e855609ee663c0630521421b5aeaaf5e
5
+ SHA512:
6
+ metadata.gz: b69b7c6680f7d4f655866c9dcebbc4bc23d4c92497979fc10c85474c82a7bcb5ed8030461bb84c6beda956ff62a7021585211cf4480e4daa17131b181285b83b
7
+ data.tar.gz: 0c2c641ac7df2360df06a508ddc309a35c097fedc3775024d83173df1d1db5fe5f439eb577c19519870488fe701b0ce0c4809528c4a4091982ab37e99515af82
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2022-07-16
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in gemspec
6
+ gemspec
7
+
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "rubocop"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Tung Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # terraspace_ci_azure
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/terraspace_ci_azure.png)](http://badge.fury.io/rb/terraspace_ci_azure)
4
+
5
+ [![BoltOps Badge](https://img.boltops.com/boltops/badges/boltops-badge.png)](https://www.boltops.com)
6
+
7
+ [![BoltOps Learn Badge](https://img.boltops.com/boltops-learn/boltops-learn.png)](https://learn.boltops.com)
8
+
9
+ Provides Terraspace CI support for Azure.
10
+
11
+ ## Installation
12
+
13
+ Add gem to your Terraspace project
14
+
15
+ Gemfile
16
+
17
+ gem "terraspace_ci_azure"
18
+
19
+ Then to install run
20
+
21
+ bundle
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/boltops-tools/terraspace_ci_azure/
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,39 @@
1
+ #!/bin/bash
2
+
3
+ # install terraform
4
+ git clone https://github.com/tfutils/tfenv.git ~/.tfenv
5
+ export PATH="$HOME/.tfenv/bin:$PATH"
6
+ tfenv install latest
7
+ tfenv use latest
8
+ terraform --version
9
+ type terraform # $HOME/.tfenv/bin/terraform
10
+ # shim for terraform since it's since is not the .tfenv/bin
11
+ # HOME = /home/vsts for azure pipeline
12
+ # Note: export PATH causes infinite loop in azure env for some reason. IE:
13
+ # export PATH="~/.tfenv/bin:$PATH"
14
+ # Instead use full path: ~/.tfenv/bin/terraform
15
+ mkdir -p ~/.local/bin
16
+ cat << 'EOF' > ~/.local/bin/terraform
17
+ exec ~/.tfenv/bin/terraform "$@"
18
+ EOF
19
+ chmod +x ~/.local/bin/terraform
20
+ terraform -v
21
+
22
+ # install terraspace
23
+ bundle install
24
+ # Details of azure shell PATH: https://gist.github.com/tongueroo/db796d7facd6b2a7635a50b965e73736
25
+ # Shim so dont have to remember bundle exec
26
+ cat << 'EOF' > ~/.local/bin/terraspace
27
+ #!/bin/bash
28
+ if [ -f config/app.rb ]; then
29
+ exec bundle exec terraspace "$@"
30
+ else
31
+ exec terraspace "$@"
32
+ fi
33
+ EOF
34
+ chmod +x ~/.local/bin/terraspace
35
+ terraspace --version
36
+
37
+ # install infracost
38
+ # https://www.infracost.io/docs/
39
+ curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh
@@ -0,0 +1,42 @@
1
+ # Trigger for branch workflow
2
+ trigger:
3
+ branches:
4
+ exclude: [ "main" ] # plan on all branches except main
5
+
6
+ pool:
7
+ vmImage: ubuntu-latest
8
+
9
+ # Note: common-variables group must already exist.
10
+ # Using group to reduce some duplication. Instead of:
11
+ #
12
+ # variables:
13
+ # - name: one
14
+ # value: initialValue
15
+ #
16
+ # Docs: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#understand-variable-syntax
17
+ variables:
18
+ - group: common-variables
19
+
20
+ steps:
21
+ - task: UseRubyVersion@0
22
+ inputs:
23
+ versionSpec: '>= 3.1'
24
+
25
+ # Note: script must be first parameter
26
+ # Reference Docs: https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/steps-script?view=azure-pipelines
27
+ - script: |
28
+ .azure/bin/install
29
+ displayName: install terraspace
30
+
31
+ # Secret variables must be exported to be available.
32
+ # Plain text variables are automatically available.
33
+ - script: |
34
+ export ARM_CLIENT_ID=$(ARM_CLIENT_ID)
35
+ export ARM_CLIENT_SECRET=$(ARM_CLIENT_SECRET)
36
+ export ARM_SUBSCRIPTION_ID=$(ARM_SUBSCRIPTION_ID)
37
+ export ARM_TENANT_ID=$(ARM_TENANT_ID)
38
+ export TS_TOKEN=$(TS_TOKEN)
39
+ # Uncomment export INFRACOST_API_KEY if using cost estimates
40
+ # export INFRACOST_API_KEY=$(INFRACOST_API_KEY)
41
+ terraspace plan demo
42
+ displayName: terraspace plan
@@ -0,0 +1,40 @@
1
+ trigger:
2
+ - main # once merged into main `terraspace up` runs on dev env
3
+
4
+ pool:
5
+ vmImage: ubuntu-latest
6
+
7
+ # Note: common-variables group must already exist.
8
+ # Using group to reduce some duplication. Instead of:
9
+ #
10
+ # variables:
11
+ # - name: one
12
+ # value: initialValue
13
+ #
14
+ # Docs: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#understand-variable-syntax
15
+ variables:
16
+ - group: common-variables
17
+
18
+ steps:
19
+ - task: UseRubyVersion@0
20
+ inputs:
21
+ versionSpec: '>= 3.1'
22
+
23
+ # Note: script must be first parameter
24
+ # Reference Docs: https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/steps-script?view=azure-pipelines
25
+ - script: |
26
+ .azure/bin/install
27
+ displayName: install terraspace
28
+
29
+ # Secret variables must be exported to be available.
30
+ # Plain text variables are automatically available.
31
+ - script: |
32
+ export ARM_CLIENT_ID=$(ARM_CLIENT_ID)
33
+ export ARM_CLIENT_SECRET=$(ARM_CLIENT_SECRET)
34
+ export ARM_SUBSCRIPTION_ID=$(ARM_SUBSCRIPTION_ID)
35
+ export ARM_TENANT_ID=$(ARM_TENANT_ID)
36
+ export TS_TOKEN=$(TS_TOKEN)
37
+ # Uncomment export INFRACOST_API_KEY if using cost estimates
38
+ # export INFRACOST_API_KEY=$(INFRACOST_API_KEY)
39
+ terraspace up demo -y
40
+ displayName: terraspace up
@@ -0,0 +1,46 @@
1
+ trigger: none # only manual trigger
2
+
3
+ parameters:
4
+ - name: command
5
+ displayName: Terraspace command to run
6
+ type: string
7
+ default: plan demo
8
+ values:
9
+ - plan demo
10
+ - up demo
11
+ - down demo
12
+ - name: TS_ENV
13
+ displayName: Terraspace env
14
+ type: string
15
+ default: prod
16
+ values:
17
+ - dev
18
+ - prod
19
+
20
+ pool:
21
+ vmImage: ubuntu-latest
22
+
23
+ variables:
24
+ - group: common-variables
25
+
26
+ steps:
27
+ - task: UseRubyVersion@0
28
+ inputs:
29
+ versionSpec: '>= 3.1'
30
+
31
+ - script: |
32
+ .azure/bin/install
33
+ displayName: install terraspace
34
+
35
+ - script: |
36
+ export ARM_CLIENT_ID=$(ARM_CLIENT_ID)
37
+ export ARM_CLIENT_SECRET=$(ARM_CLIENT_SECRET)
38
+ export ARM_SUBSCRIPTION_ID=$(ARM_SUBSCRIPTION_ID)
39
+ export ARM_TENANT_ID=$(ARM_TENANT_ID)
40
+ export TS_TOKEN=$(TS_TOKEN)
41
+ export TS_ENV=${{ parameters.TS_ENV }}
42
+ # Uncomment export INFRACOST_API_KEY if using cost estimates
43
+ # export INFRACOST_API_KEY=$(INFRACOST_API_KEY)
44
+ # always use -y in case user forgets in parameter
45
+ terraspace ${{ parameters.command }} -y
46
+ displayName: run terraspace
@@ -0,0 +1,23 @@
1
+ require "zeitwerk"
2
+
3
+ module TerraspaceCiAzure
4
+ class Autoloader
5
+ class Inflector < Zeitwerk::Inflector
6
+ def camelize(basename, _abspath)
7
+ map = { version: "VERSION" }
8
+ map[basename.to_sym] || super
9
+ end
10
+ end
11
+
12
+ class << self
13
+ def setup
14
+ loader = Zeitwerk::Loader.new
15
+ loader.inflector = Inflector.new
16
+ loader.push_dir(File.dirname(__dir__)) # lib
17
+ loader.log! if ENV["TERRASPACE_CI_AZURE_AUTOLOAD_LOG"]
18
+ loader.setup
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,114 @@
1
+ require "uri"
2
+
3
+ module TerraspaceCiAzure
4
+ class Interface
5
+ # Interface method. Returns Hash of properties.
6
+ def vars
7
+ {
8
+ build_system: "azure",
9
+ host: host,
10
+ full_repo: full_repo,
11
+ branch_name: branch_name,
12
+ # urls
13
+ pr_url: pr_url,
14
+ build_url: build_url,
15
+ branch_url: branch_url,
16
+ commit_url: commit_url,
17
+ # additional properties
18
+ build_type: build_type,
19
+ pr_number: pr_number,
20
+ sha: sha,
21
+ # additional properties
22
+ # commit_message: ENV['REPLACE_ME'],
23
+ build_id: build_id,
24
+ build_number: ENV['BUILD_BUILDNUMBER'], # IE: BUILD_BUILDNUMBER=20220715.12
25
+ }
26
+ end
27
+
28
+ def branch_name
29
+ if pr_number
30
+ message = ENV['BUILD_SOURCEVERSIONMESSAGE']
31
+ md = message.match(/Merge pull request \d+ from (.*) into (.*)/)
32
+ if md
33
+ # IE: BUILD_SOURCEVERSIONMESSAGE=Merge pull request 2 from feature into main
34
+ # Its a bit weird but with azure repos with check policy trigger
35
+ md[1]
36
+ else # GitHub and Bitbucket PR has actual branch though
37
+ # IE: SYSTEM_PULLREQUEST_SOURCEBRANCH=feature
38
+ message
39
+ end
40
+ else # push
41
+ ENV['BUILD_SOURCEBRANCHNAME']
42
+ end
43
+ end
44
+
45
+ def sha
46
+ ENV['BUILD_SOURCEVERSION']
47
+ end
48
+
49
+ # IE: BUILD_REPOSITORY_URI=https://tongueroo@dev.azure.com/tongueroo/infra-project/_git/infra-ci
50
+ def host
51
+ uri = URI(ENV['BUILD_REPOSITORY_URI'])
52
+ "#{uri.scheme}://#{uri.host}"
53
+ end
54
+
55
+ # removes the user@ part
56
+ # IE: BUILD_REPOSITORY_URI=https://tongueroo@dev.azure.com/tongueroo/infra-project/_git/infra-ci
57
+ def base_project_url
58
+ uri = URI(ENV['BUILD_REPOSITORY_URI'])
59
+ base_path = uri.path.split('/')[0..2].join('/')
60
+ "#{uri.scheme}://#{uri.host}#{base_path}"
61
+ end
62
+
63
+ def base_repo_url
64
+ uri = URI(ENV['BUILD_REPOSITORY_URI'])
65
+ "#{uri.scheme}://#{uri.host}#{uri.path}"
66
+ end
67
+
68
+ # IE: BUILD_REPOSITORY_URI=https://tongueroo@dev.azure.com/tongueroo/infra-project/_git/infra-ci
69
+ def full_repo
70
+ uri = URI(ENV['BUILD_REPOSITORY_URI'])
71
+ org = uri.path.split('/')[1] # since there's a leading /
72
+ repo = ENV['BUILD_REPOSITORY_NAME'] # tongueroo
73
+ "#{org}/#{repo}"
74
+ end
75
+
76
+ def build_type
77
+ ENV['SYSTEM_PULLREQUEST_PULLREQUESTID'] ? 'pull_request' : 'push'
78
+ end
79
+
80
+ # IE: SYSTEM_PULLREQUEST_PULLREQUESTID=2
81
+ def pr_number
82
+ ENV['SYSTEM_PULLREQUEST_PULLREQUESTID']
83
+ end
84
+
85
+ def build_id
86
+ ENV['BUILD_BUILDID']
87
+ end
88
+
89
+ # https://dev.azure.com/tongueroo/infra-project/_git/infra-ci/pullrequest/2
90
+ # IE: BUILD_REPOSITORY_URI=https://tongueroo@dev.azure.com/tongueroo/infra-project/_git/infra-ci
91
+ def pr_url
92
+ return unless pr_number
93
+ uri = URI(ENV['BUILD_REPOSITORY_URI'])
94
+ "#{base_repo_url}/pullrequest/#{pr_number}"
95
+ end
96
+
97
+ # IE: BUILD_BUILDID=74
98
+ # https://dev.azure.com/tongueroo/infra-project/_build/results?buildId=152&view=results
99
+ def build_url
100
+ return unless build_id
101
+ "#{base_project_url}/_build/results?buildId=#{build_id}&view=results"
102
+ end
103
+
104
+ # IE: https://dev.azure.com/tongueroo/infra-project/_git/infra-ci/commit/2eac582a6c0582c426f5304619aafd1db4f12434
105
+ def commit_url
106
+ "#{base_repo_url}/commit/#{sha}"
107
+ end
108
+
109
+ # IE: https://dev.azure.com/tongueroo/infra-project/_git/infra-ci?version=GBfeature
110
+ def branch_url
111
+ "#{base_repo_url}?version=GB#{branch_name}"
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TerraspaceCiAzure
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "terraspace_ci_azure/autoloader"
4
+ TerraspaceCiAzure::Autoloader.setup
5
+
6
+ require "json"
7
+
8
+ module TerraspaceCiAzure
9
+ class Error < StandardError; end
10
+ end
11
+
12
+ require "terraspace"
13
+ Terraspace::Cloud::Ci.register(
14
+ name: "azure",
15
+ env_key: "SYSTEM_TEAMFOUNDATIONSERVERURI",
16
+ root: __dir__,
17
+ exe: ".azure/bin", # terraspace new ci NAME generator will make files in this folder executable
18
+ )
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/terraspace_ci_azure/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "terraspace_ci_azure"
7
+ spec.version = TerraspaceCiAzure::VERSION
8
+ spec.authors = ["Tung Nguyen"]
9
+ spec.email = ["tung@boltops.com"]
10
+
11
+ spec.summary = "Terraspace CI Azure support"
12
+ spec.homepage = "https://github.com/boltops-tools/terraspace_ci_azure"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/boltops-tools/terraspace_ci_azure"
18
+ spec.metadata["changelog_uri"] = "https://github.com/boltops-tools/terraspace_ci_azure/blob/master/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|azure)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "zeitwerk"
32
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terraspace_ci_azure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zeitwerk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - tung@boltops.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/template/.azure/bin/install
42
+ - lib/template/.azure/pipelines/dev-plan.yml
43
+ - lib/template/.azure/pipelines/dev-up.yml
44
+ - lib/template/.azure/pipelines/manual.yml
45
+ - lib/terraspace_ci_azure.rb
46
+ - lib/terraspace_ci_azure/autoloader.rb
47
+ - lib/terraspace_ci_azure/interface.rb
48
+ - lib/terraspace_ci_azure/version.rb
49
+ - terraspace_ci_azure.gemspec
50
+ homepage: https://github.com/boltops-tools/terraspace_ci_azure
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/boltops-tools/terraspace_ci_azure
55
+ source_code_uri: https://github.com/boltops-tools/terraspace_ci_azure
56
+ changelog_uri: https://github.com/boltops-tools/terraspace_ci_azure/blob/master/CHANGELOG.md
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.6.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.3.12
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Terraspace CI Azure support
76
+ test_files: []