Module: GhaConfig::Writer

Defined in:
lib/gha_config/writer.rb

Class Method Summary collapse

Class Method Details

.cleanup(out, config) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gha_config/writer.rb', line 86

def self.cleanup(out, config)
  out = out.sub("---\n", '')
  out = out.gsub(/'on'/, 'on') # replace 'on' with on
  out = out.gsub(/: ''/, ':') # replace : '' with :
  out = out.gsub(/\n(\S)/) { "\n\n#{$1}" } # add empty line before top level keys
  out = out.gsub(/"\[/, '[')
  out = out.gsub(/\]"/, ']') # change "[...]" to [...]

  config.variables.each do |name, val|
    out = out.gsub("__#{name}__", val)
  end

  first, last = out.split("\njobs:")
  first = first.gsub(/"/, '') # remove quotes from env block
  last = last.gsub(/\n  (\S)/) { "\n\n  #{$1}" } # add empty line before job keys
  out = first + "\njobs:" + last

  out
end

.write(config, output_file) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gha_config/writer.rb', line 5

def self.write(config, output_file)
  output_hash = {}
  output_hash['name'] = 'CI'
  output_hash['on'] = config.parsed_config['on']

  default_env =  {
    'SLACK_CHANNEL' => ''
  }
  output_hash['env'] = default_env.
    merge({
            'HOME'                  => '/home/circleci',
            'AWS_ACCESS_KEY_ID'     => '${{ secrets.AWS_ACCESS_KEY_ID }}',
            'AWS_ACCOUNT_ID'        => '${{ secrets.AWS_ACCOUNT_ID }}',
            'AWS_REGION'            => '${{ secrets.AWS_REGION }}',
            'AWS_SECRET_ACCESS_KEY' => '${{ secrets.AWS_SECRET_ACCESS_KEY }}',
            'DEPLOYMENT_TYPE'       => '${{ secrets.DEPLOYMENT_TYPE }}',
            'ECR_REPOSITORY'        => '${{ secrets.ECR_REPOSITORY }}',
            'ECS_CLUSTER_PROD'      => '${{ secrets.ECS_CLUSTER_PROD }}',
            'ECS_CLUSTER_STG'       => '${{ secrets.ECS_CLUSTER_STG }}',
            'ECS_SERVICE_PROD'      => '${{ secrets.ECS_SERVICE_PROD }}',
            'ECS_SERVICE_STG'       => '${{ secrets.ECS_SERVICE_STG }}',
            'SERVICE_NAME'          => '${{ secrets.SERVICE_NAME }}',
            'SERVICE_PROFILE'       => '${{ secrets.SERVICE_PROFILE }}',
            'SERVICE_TOKEN'         => '${{ secrets.SERVICE_TOKEN }}',
            'WISHABI_ENVIRONMENT'   => '${{ secrets.WISHABI_ENVIRONMENT }}',
            'ARTIFACTORY_HOST'      => '${{ secrets.ARTIFACTORY_HOST }}',
            'ARTIFACTORY_PASSWORD'  => '${{ secrets.ARTIFACTORY_PASSWORD }}',
            'ARTIFACTORY_REALM'     => '${{ secrets.ARTIFACTORY_REALM }}',
            'ARTIFACTORY_USER'      => '${{ secrets.ARTIFACTORY_USER }}'
  }).merge(config.env)

  output_hash['jobs'] = {}
  config.parsed_config['jobs'].each do |name, job|
    new_job = {
      'runs-on' => '[ubuntu, runner-fleet]'
    }
    job['runs-on'] = '[ubuntu, runner-fleet]'
    flipp_global = {
      'name' => 'Flipp global',
      'uses' => 'wishabi/[email protected]',
      'env' => {
        'SLACK_BOT_TOKEN' => "${{ secrets.SLACK_BOT_TOKEN }}"
      },
      'timeout-minutes' => 10,
      'with' => {
        'slack_channel' => '${{env.SLACK_CHANNEL }}',
        'job_status' => '${{ job.status }}'
      }}
    if config.options['enable_ssh']
      flipp_global['with']['enable_ssh'] = true
    end
    job['steps'].unshift(flipp_global)
    unless job['steps'].any? { |s| s['uses'] == 'actions/checkout@v2'}
      checkout = {
        'name' => 'Checkout code',
        'uses' => 'actions/checkout@v2'
      }
      if config.options['use_submodules']
        checkout['with'] = {
          'token' => "${{ secrets.FLIPPCIRCLECIPULLER_REPO_TOKEN }}",
          'submodules' => 'recursive'
        }
      end
      job['steps'].unshift(checkout)
    end
    job['needs'] = "[#{job['needs'].join(', ')}]" if job['needs'].is_a?(Array)
    new_job.merge!(job)
    output_hash['jobs'][name] = new_job
  end
  output = output_hash.to_yaml
  output = cleanup(output, config)
  header = <<-OUT
######## GENERATED FROM ./github/workflow-src/CI.yml
######## USING gha_config https://github.com/wishabi/gha-config

  OUT
  output = header + output
  FileUtils.mkdir_p(File.dirname(output_file))
  File.write(output_file, output)
end