Skip to content

Commit 5e420a5

Browse files
committed
...
1 parent ac3f91c commit 5e420a5

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

action.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
schema-version: v3
2+
kind: action
3+
metadata:
4+
name: workflow-main-action
5+
display-name: Workflows main action
6+
description: Responsible to define which workflow will be executed based on type.
7+
version: 1.0.0
8+
spec:
9+
type: python
10+
docs:
11+
en-us: docs/en-us/docs.md
12+
repository: https://github.com/stack-spot/workflow-stackspot-actions-runtime-selfhosted.git
13+
inputs:
14+
- label: Workflow type
15+
name: workflow_type
16+
type: text
17+
required: true
18+
python:
19+
workdir: .
20+
script: script.py

docs/en-us/docs.md

Whitespace-only changes.

examples/gitlab/stackspot-deploy.yml

Whitespace-only changes.

script.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
import sys
3+
from pathlib import Path
4+
import subprocess
5+
import json
6+
from sys import exit
7+
8+
9+
class RunAction:
10+
def __init__(self, metadata):
11+
self.stk = sys.argv[0]
12+
self.action_base_path = os.path.dirname(os.path.abspath(__file__))
13+
self.inputs = {k: v for k, v in metadata.inputs.items() if v is not None}
14+
15+
def __call__(self, action_name: str, **inputs):
16+
action_path = f'{self.action_base_path}/{action_name}'
17+
cmd = [self.stk, 'run', 'action', action_path, '--inputs-json', json.dumps({**self.inputs, **inputs}),'--non-interactive']
18+
result = subprocess.run(cmd)
19+
if (result.returncode == 1):
20+
print(f"Fail to execute {result.args}...")
21+
exit(1)
22+
23+
24+
def deploy_workflow(run_action: RunAction):
25+
run_action("runtime-create-manifest-action")
26+
run_action("runtime-manager-action")
27+
28+
with open('manager-output.log', 'r') as file:
29+
data = json.loads(file.read().replace("\'", "\""))
30+
31+
task_runners = dict(
32+
IAC_SELF_HOSTED=lambda **i: run_action("runtime-iac-action", **i),
33+
DEPLOY_SELF_HOSTED=lambda **i: run_action("runtime-deploy-action", **i),
34+
)
35+
for t in data['tasks']:
36+
runner = task_runners.get(t["taskType"])
37+
runner and runner(run_task_id=t["runTaskId"])
38+
39+
40+
def cancel_deploy_run(run_action: RunAction):
41+
run_action("runtime-cancel-run-action")
42+
43+
44+
def run(metadata):
45+
workflows = dict(deploy=deploy_workflow, cancel_deploy=cancel_deploy_run)
46+
run_action = RunAction(metadata)
47+
workflow_runner = workflows.get(metadata.inputs["workflow_type"])
48+
workflow_runner and workflow_runner(run_action=run_action)
49+

0 commit comments

Comments
 (0)