Module: GhaConfig::Writer

Defined in:
lib/gha_config/writer.rb

Class Method Summary collapse

Class Method Details

.cleanup(out) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gha_config/writer.rb', line 66

def self.cleanup(out)
  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 [...]

  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



3
4
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
# File 'lib/gha_config/writer.rb', line 3

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(config.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 }}'
          })

  output_hash['jobs'] = {}
  config.parsed_config['jobs'].each do |name, job|
    new_job = {
      'runs-on' => '[ubuntu, runner-fleet]'
    }
    job['runs-on'] = '[ubuntu, runner-fleet]'
    job['steps'].unshift({
                            '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 }}'
                            }})
    job['steps'].unshift({
                           'name' => 'Checkout code',
                           'uses' => 'actions/checkout@v2'
                         })
    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)
  header = "######## GENERATED FROM ./github/workflow-src/CI.yml\n######## USING gha_config https://github.com/wishabi/gha-config\n\n  OUT\n  output = header + output\n  File.write(output_file, output)\nend\n"