-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
175 lines (163 loc) · 6.91 KB
/
action.yml
1
2
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: Execute Terraform Environment Apply
description: Execute a terraform apply on already created environment, or create one if it doesn't exist.
branding:
icon: cloud
color: white
inputs:
environment-name:
description: Name of Cloudify environment to handle terraform apply
required: true
environment-id:
description: Id of Cloudify environment to handle terraform apply
default: "-"
required: false
labels:
description: |
A labels list of the form <key>:<value>,<key>:<value>.
Any comma and colon in <value> must be escaped with \.
The labels’ keys are saved in lowercase.
required: false
default: "-"
module-file:
description: URL/path to Terraform module archive
required: true
module-source-path:
description: path to Terraform module inside the archive
required: true
variables-file:
description: YAML/JSON file containing template variables
required: false
default: "-"
environment-file:
description: |
YAML/JSON file containing environment variables to pass to the Terraform process
required: false
default: "-"
environment-mapping:
description: |
A whitespace-delimited list of strings. For each item:
* If it is of the form `k=v`, then the value of the environment variable `k` is passed
as environment variable `v` to Terraform
* Otherwise, let the value be `x`; then the environment variable `x` is passed as-is
to Terraform
This effectively passes environment variables, visible to the CI/CD platform,
as environment variables to Terraform. Useful, among others, for
passing Cloud credentials - stored in the CI/CD platform - to Terraform
required: false
default: "-"
outputs:
terraform-outputs:
description: Terraform module outputs
value: ${{ steps.get_terraform_output.outputs.data }}
runs:
using: "composite"
steps:
- name: Check if Environment exists
if: ${{ inputs.environment-id == '-' }}
id: check_environment
uses: cloudify-cosmo/[email protected]
with:
environment-name: ${{ inputs.environment-name }}
labels: ${{ inputs.labels }}
- name: Set Deployment exists
id: check_if_deployment_exists
shell: python
run: |
import io
import os
import json
import yaml
def prepare_parameters_file(variables, environment_variables):
if variables not in ['', '-']:
if os.path.isfile(variables):
with io.open(variables, 'r', encoding='UTF-8') as f:
variables = yaml.safe_load(f)
else:
variables = json.loads(variables)
else:
variables = ''
if environment_variables not in ['', '-']:
if os.path.isfile(environment_variables):
with io.open(environment_variables, 'r', encoding='UTF-8') as f:
environment_variables = yaml.safe_load(f)
else:
environment_variables = json.loads(environment_variables)
else:
environment_variables = ''
source_path = "${{ inputs.module-source-path }}"
if source_path in ['','-']:
source_path = '.'
workflow_parms = {
'node_ids': [],
'source': "${{ inputs.module-file }}",
'source_path': source_path,
'variables': variables,
'environment_variables': environment_variables,
'force': True,
}
with open('workflow_params.yaml', 'w') as outfile:
yaml.dump(workflow_parms, outfile)
deployment_id = "${{ inputs.environment-id }}"
if deployment_id != '-':
print ('::set-output name=exists::{0}'.format('true'))
print ('::set-output name=deployment_id::{0}'.format(deployment_id))
prepare_parameters_file("${{ inputs.variables-file }}", "${{ inputs.environment-file }}")
else:
environments = json.loads(r"""${{steps.check_environment.outputs.environment-data}}""")
if len(environments)==0:
print ('::set-output name=exists::{0}'.format('false'))
elif len(environments)==1:
print('exists')
print ('::set-output name=exists::{0}'.format('true'))
print ('::set-output name=deployment_id::{0}'.format(environments[0].get('deployment_id')))
prepare_parameters_file("${{ inputs.variables-file }}", "${{ inputs.environment-file }}")
else:
dep_ids = [dep.get('deployment_id') for dep in environments]
raise Exception("Your name/labels may refer to these deployments {0} , please specify which one using environment-id input".format(dep_ids))
- name: Execute Terraform apply
if: ${{ steps.check_if_deployment_exists.outputs.exists != 'true' }}
id: terraform_apply
uses: cloudify-cosmo/[email protected]
with:
environment-name: ${{ inputs.environment-name }}
module-file: ${{ inputs.module-file }}
module-source-path: ${{ inputs.module-source-path }}
variables-file: ${{ inputs.variables-file }}
labels: ${{ inputs.labels }}
outputs-file: "env-data.json"
- name: Execute Terraform reload workflow
if: ${{ steps.check_if_deployment_exists.outputs.exists == 'true' }}
id: terraform_plan_wf
uses: cloudify-cosmo/[email protected]
with:
environment-name: ${{ steps.check_if_deployment_exists.outputs.deployment_id }}
workflow: "reload_terraform_template"
parameters-file: 'workflow_params.yaml'
- name: Get Deployment ID
if: ${{ steps.check_if_deployment_exists.outputs.exists != 'true' }}
id: get_deployment_id
shell: bash
run: |
content=`cat env-data.json`
# the following lines are only required for multi line json
content="${content//'%'/'%25'}"
content="${content//$'\n'/'%0A'}"
content="${content//$'\r'/'%0D'}"
# end of optional handling for multi line json
echo "::set-output name=packageJson::$content"
- name: Get Deployment data
id: get_deployment_data
uses: cloudify-cosmo/[email protected]
with:
environment-name: ${{ steps.check_if_deployment_exists.outputs.deployment_id || fromJson(steps.get_deployment_id.outputs.packageJson).deployment_id }}
- name: get terraform module instance runtime properties
id: get_terraform_output
shell: python
run: |
import json
environment_data = json.loads(r"""${{steps.get_deployment_data.outputs.environment-data}}""")['outputs']['terraform']
result = json.dumps(environment_data, sort_keys=True, indent=4)
result = result.replace('%','%25')
result = result.replace('\n','%0A')
result = result.replace('\r','%0D')
print ('::set-output name=data::{0}'.format(result))