terraspace_ci_circleci 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: 88aa7a619627c1ffc00c3be9eb25f1ed52aef51113d614fd2a7a25537aa78f03
4
+ data.tar.gz: 24d73984a44ce20c180dde3ea7c0deccd00c2ae9ade255595a8a040bc42cdaad
5
+ SHA512:
6
+ metadata.gz: 748151767f81f5fe0df19ebebb01f0855cb4aebdf33922dde3e8e106188cfffabe0df241c561d1a111ff9fb17a1fb0309fc4a31b0a93344ee171b3b9aa4605ea
7
+ data.tar.gz: aa01e0580f0e66e274cd9f57f882aa626daf42a1b0dc503031c2ed4c3d8311979eb19789c98e78103c8d5289d8fc1efdc8f8898c00898af61a9def62f29d2276
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-14
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_circleci
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/terraspace_ci_circleci.png)](http://badge.fury.io/rb/terraspace_ci_circleci)
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 CircleCI.
10
+
11
+ ## Installation
12
+
13
+ Add gem to your Terraspace project
14
+
15
+ Gemfile
16
+
17
+ gem terraspace_ci_circleci
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_circleci/
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,36 @@
1
+ #!/bin/bash
2
+ set -eux
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/circleci/.tfenv/bin/terraform
10
+ # shim for terraform since it's since is not the .tfenv/bin
11
+ mkdir -p /home/circleci/.local/bin
12
+ cat << 'EOF' > /home/circleci/.local/bin/terraform
13
+ export PATH="$HOME/.tfenv/bin:$PATH"
14
+ exec terraform "$@"
15
+ EOF
16
+ chmod +x /home/circleci/.local/bin/terraform
17
+
18
+ # install terraspace
19
+ bundle install
20
+ # shim for circleci
21
+ mv ~/.rubygems/bin/terraspace ~/.rubygems/bin/terraspace.rb
22
+ cat << 'EOF' > ~/.rubygems/bin/terraspace
23
+ #!/bin/bash
24
+ EXE=~/.rubygems/bin/terraspace.rb
25
+ if [ -f config/app.rb ]; then
26
+ exec bundle exec $EXE "$@"
27
+ else
28
+ exec $EXE "$@"
29
+ fi
30
+ EOF
31
+ chmod +x ~/.rubygems/bin/terraspace
32
+ terraspace --version
33
+
34
+ # install infracost
35
+ # https://www.infracost.io/docs/
36
+ curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh
@@ -0,0 +1,25 @@
1
+ #!/bin/bash
2
+ set -ex
3
+
4
+ # Looks like only able to conditionally decide with CIRCLE_PULL_REQUEST and CIRCLE_BRANCH
5
+ # at runtime instead of circleci evaluation time for the workflow we're trying to achieve.
6
+ # So will "cancel" workflows that don't really want to run we can see this with the status visually.
7
+ #
8
+ if [ -n "$CIRCLE_PULL_REQUEST" ]; then
9
+ exec terraspace plan "$@"
10
+ else
11
+ if [ "$CIRCLE_BRANCH" == "main" ]; then
12
+ exec terraspace up "$@" -y
13
+ else
14
+ # cancel workflow and set status
15
+ if [ -n "$CIRCLE_TOKEN" ] ; then
16
+ # https://circleci.com/docs/api/v2/index.html#operation/cancelWorkflow
17
+ curl --request POST \
18
+ --header "Circle-Token: $CIRCLE_TOKEN" \
19
+ https://circleci.com/api/v2/workflow/$CIRCLE_WORKFLOW_ID/cancel
20
+ sleep 10 # seems to allow the curl request to cancel consistently
21
+ fi
22
+ # For correct Workflow Cancelled Status also. Correct status only if also cancel via the API
23
+ circleci step halt
24
+ fi
25
+ fi
@@ -0,0 +1,57 @@
1
+ version: 2.1
2
+
3
+ common_env_vars: &common_env_vars
4
+ AWS_REGION: us-west-2
5
+
6
+ parameters:
7
+ manual:
8
+ type: boolean
9
+ default: false
10
+
11
+ jobs:
12
+ # Manually triggered: TS_ENV=prod
13
+ prod_plan:
14
+ docker:
15
+ - image: cimg/ruby:3.1.0
16
+ environment: *common_env_vars
17
+ steps:
18
+ - checkout
19
+ - run: .circleci/bin/install
20
+ - run: TS_ENV=prod terraspace plan demo
21
+ prod_up:
22
+ docker:
23
+ - image: cimg/ruby:3.1.0
24
+ environment: *common_env_vars
25
+ steps:
26
+ - checkout
27
+ - run: .circleci/bin/install
28
+ - run: TS_ENV=prod terraspace up demo -y
29
+
30
+ # On PR: TS_ENV=dev terraspace plan
31
+ # On push: TS_ENV=dev terraspace up - checks a specific branch only
32
+ dev_plan_or_up:
33
+ docker:
34
+ - image: cimg/ruby:3.1.0
35
+ environment: *common_env_vars
36
+ steps:
37
+ - checkout
38
+ - run: .circleci/bin/install
39
+ - run: .circleci/bin/terraspace-plan-or-up.sh demo # depends if PR
40
+
41
+ workflows:
42
+ prod_plan_then_up_with_approval:
43
+ when: << pipeline.parameters.manual >>
44
+ jobs:
45
+ - prod_plan
46
+ - hold:
47
+ type: approval
48
+ requires:
49
+ - prod_plan
50
+ - prod_up:
51
+ requires:
52
+ - hold
53
+ dev_plan_or_up:
54
+ when:
55
+ not: << pipeline.parameters.manual >>
56
+ jobs:
57
+ - dev_plan_or_up
@@ -0,0 +1,23 @@
1
+ require "zeitwerk"
2
+
3
+ module TerraspaceCiCircleci
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_CIRCLECI_AUTOLOAD_LOG"]
18
+ loader.setup
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,53 @@
1
+ require "uri"
2
+
3
+ module TerraspaceCiCircleci
4
+ class Interface
5
+ # Interface method. Returns Hash of properties.
6
+ def vars
7
+ {
8
+ build_system: "circleci",
9
+ host: host,
10
+ full_repo: full_repo,
11
+ branch_name: ENV['CIRCLE_BRANCH'],
12
+ # urls
13
+ pr_url: ENV['CIRCLE_PULL_REQUEST'],
14
+ build_url: ENV['CIRCLE_BUILD_URL'],
15
+ # additional properties
16
+ build_type: build_type,
17
+ pr_number: pr_number,
18
+ sha: ENV['CIRCLE_SHA1'],
19
+ # additional properties
20
+ # commit_message: ENV['REPLACE_ME'],
21
+ build_id: ENV['CIRCLE_BUILD_NUM'],
22
+ build_number: ENV['CIRCLE_BUILD_NUM'],
23
+ }
24
+ end
25
+
26
+ # IE: CIRCLE_BUILD_URL=https://circleci.com/gh/ORG/REPO/7
27
+ def host
28
+ uri = URI(ENV['CIRCLE_BUILD_URL'])
29
+ "#{uri.scheme}://#{uri.host}"
30
+ end
31
+
32
+ # IE: CIRCLE_REPOSITORY_URL=git@github.com:ORG/REPO.git
33
+ def full_repo
34
+ url = ENV['CIRCLE_REPOSITORY_URL']
35
+ full_repo = if url.include?(':')
36
+ url.split(':').last
37
+ else
38
+ URI(url).path.sub(%r{^/},'')
39
+ end
40
+ full_repo.sub('.git','')
41
+ end
42
+
43
+ def build_type
44
+ ENV['CIRCLE_PULL_REQUEST'] ? 'pull_request' : 'push'
45
+ end
46
+
47
+ # CIRCLE_PULL_REQUEST=https://github.com/ORG/REPO/pull/2
48
+ def pr_number
49
+ pr = ENV['CIRCLE_PULL_REQUEST']
50
+ pr.split('/').last if pr
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TerraspaceCiCircleci
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "terraspace_ci_circleci/autoloader"
4
+ TerraspaceCiCircleci::Autoloader.setup
5
+
6
+ require "json"
7
+
8
+ module TerraspaceCiCircleci
9
+ class Error < StandardError; end
10
+ end
11
+
12
+ Terraspace::Cloud::Ci.register(
13
+ name: "circleci",
14
+ env_key: "CIRCLECI",
15
+ root: __dir__,
16
+ exe: ".circleci/bin", # terraspace new ci NAME generator will make files in this folder executable
17
+ )
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/terraspace_ci_circleci/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "terraspace_ci_circleci"
7
+ spec.version = TerraspaceCiCircleci::VERSION
8
+ spec.authors = ["Tung Nguyen"]
9
+ spec.email = ["tung@boltops.com"]
10
+
11
+ spec.summary = "Terraspace CI CircleCI support"
12
+ spec.homepage = "https://github.com/boltops-tools/terraspace_ci_circleci"
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_circleci"
18
+ spec.metadata["changelog_uri"] = "https://github.com/boltops-tools/terraspace_ci_circleci/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|circleci)|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,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terraspace_ci_circleci
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-14 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/.circleci/bin/install
42
+ - lib/template/.circleci/bin/terraspace-plan-or-up.sh
43
+ - lib/template/.circleci/config.yml
44
+ - lib/terraspace_ci_circleci.rb
45
+ - lib/terraspace_ci_circleci/autoloader.rb
46
+ - lib/terraspace_ci_circleci/interface.rb
47
+ - lib/terraspace_ci_circleci/version.rb
48
+ - terraspace_ci_circleci.gemspec
49
+ homepage: https://github.com/boltops-tools/terraspace_ci_circleci
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/boltops-tools/terraspace_ci_circleci
54
+ source_code_uri: https://github.com/boltops-tools/terraspace_ci_circleci
55
+ changelog_uri: https://github.com/boltops-tools/terraspace_ci_circleci/blob/master/CHANGELOG.md
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.6.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.3.12
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Terraspace CI CircleCI support
75
+ test_files: []