Skip to content

Commit 972471b

Browse files
kirandevtnkomalreddy3
authored andcommitted
feat: Plugin to trigger Devtron Job (#5053)
* feat: Plugin to trigger Devtron Job (#5052) * Update 241_devtronJobTrigger.up.sql
1 parent 6394f5a commit 972471b

File tree

2 files changed

+254
-0
lines changed

2 files changed

+254
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DELETE FROM plugin_step_variable WHERE plugin_step_id=(SELECT id FROM plugin_metadata WHERE name='Devtron Job Trigger v1.0.0');
2+
DELETE FROM plugin_step where plugin_id=(SELECT id FROM plugin_metadata WHERE name='Devtron Job Trigger v1.0.0' );
3+
DELETE FROM plugin_pipeline_script where id=(SELECT id FROM plugin_metadata WHERE name='Devtron Job Trigger v1.0.0');
4+
DELETE FROM plugin_stage_mapping where plugin_id=(SELECT id from plugin_metadata where name='Devtron Job Trigger v1.0.0');
5+
DELETE FROM plugin_metadata where name='Devtron Job Trigger v1.0.0';
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
-- Plugin metadata
2+
INSERT INTO "plugin_metadata" ("id", "name", "description","type","icon","deleted", "created_on", "created_by", "updated_on", "updated_by") VALUES (nextval('id_seq_plugin_metadata'), 'Devtron Job Trigger v1.0.0','Devtronjob can be triggered with the help of name/id','PRESET','https://raw.githubusercontent.com/devtron-labs/devtron/main/assets/devtron-logo-plugin.png','f', 'now()', 1, 'now()', 1);
3+
4+
-- Plugin Stage Mapping
5+
6+
INSERT INTO "plugin_stage_mapping" ("plugin_id","stage_type","created_on", "created_by", "updated_on", "updated_by")
7+
VALUES ((SELECT id FROM plugin_metadata WHERE name='Devtron Job Trigger v1.0.0'),0,'now()', 1, 'now()', 1);
8+
9+
-- Plugin Script
10+
11+
INSERT INTO "plugin_pipeline_script" ("id", "script","type","deleted","created_on", "created_by", "updated_on", "updated_by")
12+
VALUES (
13+
nextval('id_seq_plugin_pipeline_script'),
14+
E'#!/bin/bash
15+
if [ ! "$JobPipeline" ] && [ ! "$DevtronEnv" ];
16+
then
17+
echo "Error: You must provide either JobPipeline or DevtronEnv to proceed."
18+
exit 1
19+
fi
20+
eventType=$(echo "$CI_CD_EVENT" | jq -r \'.type\')
21+
22+
if [ $StatusTimeoutSeconds -ge 100 ];
23+
then
24+
sleep_time=30
25+
else
26+
sleep_time=5
27+
fi
28+
29+
#Functions
30+
is_number() {
31+
[[ "$1" =~ ^[0-9]+$ ]]
32+
}
33+
34+
if [ "$eventType" == "CI" ];
35+
then
36+
TriggeredFrom=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.appName\')
37+
TriggeredFromPipeline=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.pipelineName\')
38+
elif [ "$eventType" == "CD" ];
39+
then
40+
TriggeredFrom=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.Pipeline.App.AppName\')
41+
TriggeredFromPipeline=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.workflowNamePrefix\')
42+
else
43+
TriggeredFrom=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.appName\')
44+
TriggeredFromPipeline=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.pipelineName\')
45+
fi
46+
47+
jobId=null
48+
jobList=$(curl -s "$DevtronEndpoint/orchestrator/job/list" -H "token: $DevtronApiToken" --data-raw "{}")
49+
if [ ! "$jobList" ];
50+
then
51+
echo "Error: Please check the DevtronApiToken or DevtronEndpoint."
52+
exit 1
53+
fi
54+
if [ "$(echo "$jobList" | jq \'.code\')" -ne 200 ];
55+
then
56+
echo "$jobList" | jq \'.result\'
57+
echo "Error: Please check the DevtronApiToken or DevtronEndpoint."
58+
exit 1
59+
fi
60+
61+
62+
if is_number $DevtronJob;
63+
then
64+
jobId=$DevtronJob
65+
else
66+
jobId=$(echo "$jobList" | jq -r --arg DevtronJob "$DevtronJob" \'.result.jobContainers[] | select(.jobName == $DevtronJob) | .jobId\')
67+
if [ ! "$jobId" ];
68+
then
69+
echo "Error: Invalid DevtronJob. Please check the DevtronJob."
70+
exit 1
71+
fi
72+
fi
73+
74+
pipelineId=null
75+
DevtronEnvId=null
76+
jobInfo=$(curl -s "$DevtronEndpoint/orchestrator/app/ci-pipeline/$jobId" -H "token: $DevtronApiToken")
77+
jobInfoReturnCode=$(echo "$jobInfo" | jq \'.code\');
78+
if [ "$jobInfoReturnCode" -ne 200 ];
79+
then
80+
echo "Error: Invalid DevtronJob. Please check the DevtronJob."
81+
exit 1
82+
fi
83+
84+
85+
if [ "$DevtronEnv" ];
86+
then
87+
if is_number "$DevtronEnv";
88+
then
89+
DevtronEnvId=$DevtronEnv
90+
result_count=$(echo "$jobInfo" | jq -r --argjson DevtronEnv "$DevtronEnv" \'[.result.ciPipelines[] | select(.environmentId == $DevtronEnv) | .id] | length\')
91+
if [ "$result_count" == 1 ];
92+
then
93+
pipelineId=$(echo "$jobInfo" | jq -r --argjson DevtronEnv "$DevtronEnv" \'.result.ciPipelines[] | select(.environmentId == $DevtronEnv) | .id\')
94+
else
95+
echo "No Environment or more than one pipelines found within the same environment. Please use the JobPipeline."
96+
exit 1
97+
fi
98+
else
99+
envList=$(curl -s "$DevtronEndpoint/orchestrator/env" -H "token: $DevtronApiToken")
100+
DevtronEnvId=$(echo "$envList" | jq -r --arg DevtronEnv "$DevtronEnv" \'.result[] | select(.environment_name == $DevtronEnv) | .id\')
101+
if [ ! "$DevtronEnvId" ];
102+
then
103+
echo "Error: Invalid DevtronEnv. Please check the DevtronEnv."
104+
exit 1
105+
fi
106+
107+
result_count=$(echo "$jobInfo" | jq -r --argjson DevtronEnvId "$DevtronEnvId" \'[.result.ciPipelines[] | select(.environmentId == $DevtronEnvId) | .id] | length\')
108+
if [ "$result_count" == 1 ];
109+
then
110+
pipelineId=$(echo "$jobInfo" | jq -r --argjson DevtronEnvId "$DevtronEnvId" \'.result.ciPipelines[] | select(.environmentId == $DevtronEnvId) | .id\')
111+
else
112+
echo "No Environment or more than one pipelines found within the same environment. Please use the JobPipeline."
113+
exit 1
114+
fi
115+
fi
116+
else
117+
if is_number "$JobPipeline";
118+
then
119+
pipelineId=$JobPipeline
120+
else
121+
pipelineId=$(echo "$jobInfo" | jq -r --arg JobPipeline "$JobPipeline" \'.result.ciPipelines[] | select(.name == $JobPipeline) | .id\')
122+
if [ ! "$pipelineId" ];
123+
then
124+
echo "Error: Invalid JobPipeline. Please check the JobPipeline."
125+
exit 1
126+
fi
127+
fi
128+
DevtronEnvId=$(echo "$jobInfo" | jq -r --argjson pipelineId "$pipelineId" \'.result.ciPipelines[] | select(.id == $pipelineId) | .environmentId\')
129+
if [ ! "$DevtronEnvId" ];
130+
then
131+
echo "Error: Invalid JobPipeline. Please check the JobPipeline."
132+
exit 1
133+
fi
134+
fi
135+
136+
gitMaterial=$(curl -s "$DevtronEndpoint/orchestrator/app/ci-pipeline/$pipelineId/material" -H "token: $DevtronApiToken")
137+
if [ ! "$gitMaterial" ];
138+
then
139+
echo "No git material found. Please check the details."
140+
exit 1
141+
fi
142+
gitMaterialId=$(echo "$gitMaterial" | jq \'.result[0].id\')
143+
144+
if [ ! "$GitCommitHash" ];
145+
then
146+
GitCommitHash=$(echo "$gitMaterial" | jq -r \'.result[0].history[0].Commit\')
147+
fi
148+
149+
150+
if [ ! "$DevtronEnvId" == null ];
151+
then
152+
triggerCurl=$(curl -s "$DevtronEndpoint/orchestrator/app/ci-pipeline/trigger" -H "token: $DevtronApiToken" --data-raw \'{"TriggeredFrom": "\'"$TriggeredFrom"\'","TriggeredFromPipeline": "\'"$TriggeredFromPipeline"\'","pipelineId":\'"$pipelineId"\',"ciPipelineMaterials":[{"Id":\'"$gitMaterialId"\',"GitCommit":{"Commit":"\'"$GitCommitHash"\'"}}],"environmentId": \'"$DevtronEnvId"\'}\')
153+
triggerCurlReturnCode=$(echo "$triggerCurl" | jq \'.code\');
154+
if [ "$triggerCurlReturnCode" -ne 200 ];
155+
then
156+
echo "$triggerCurl" | jq \'.errors\'
157+
echo "Please check the details entered."
158+
exit 1
159+
else
160+
echo "The Job $DevtronJob has been triggered using GitCommitHash $GitCommitHash"
161+
fi
162+
else
163+
triggerCurl=$(curl -s "$DevtronEndpoint/orchestrator/app/ci-pipeline/trigger" \\
164+
-H "token: $DevtronApiToken" \\
165+
--data-raw \'{"TriggeredFrom": "\'"$TriggeredFrom"\'","TriggeredFromPipeline": "\'"$TriggeredFromPipeline"\'","pipelineId":\'"$pipelineId"\',"ciPipelineMaterials":[{"Id":\'"$gitMaterialId"\',"GitCommit":{"Commit":"\'"$GitCommitHash"\'"}}]}\')
166+
triggerCurlReturnCode=$(echo "$triggerCurl" | jq \'.code\');
167+
if [ "$triggerCurlReturnCode" -ne 200 ];
168+
then
169+
echo "$triggerCurl" | jq \'.errors\'
170+
echo "Please check the details entered."
171+
exit 1
172+
else
173+
echo "The Job $DevtronJob has been triggered using GitCommitHash $GitCommitHash"
174+
fi
175+
fi
176+
177+
if [ "$StatusTimeoutSeconds" -eq -1 ] || [ "$StatusTimeoutSeconds" -eq 0 ];
178+
then
179+
echo "No waiting time provided. Hence not waiting for the Job status."
180+
else
181+
sleep 1
182+
fetch_status() {
183+
curl -s "$DevtronEndpoint/orchestrator/app/workflow/status/$jobId/v2" \\
184+
-H "token: $DevtronApiToken"
185+
}
186+
num=$(fetch_status)
187+
ci_status=$(echo "$num" | jq -r --argjson pipelineId "$pipelineId" \'.result.ciWorkflowStatus[] | select(.ciPipelineId == $pipelineId) | .ciStatus\');
188+
echo "The current status of the Job is: $ci_status";
189+
echo "Maximum waiting time is : $StatusTimeoutSeconds seconds"
190+
echo "Waiting for the Job to complete......"
191+
start_time=$(date +%s)
192+
job_completed=false
193+
while [ "$ci_status" != "Succeeded" ]; do
194+
if [ "$ci_status" == "Failed" ]; then
195+
echo "The Job has been Failed"
196+
exit 1
197+
elif [ "$ci_status" == "CANCELLED" ];then
198+
echo "Job has been Cancelled"
199+
exit 1
200+
fi
201+
current_time=$(date +%s)
202+
elapsed_time=$((current_time - start_time))
203+
if [ "$elapsed_time" -ge "$StatusTimeoutSeconds" ]; then
204+
echo "Timeout reached. Terminating the current process...."
205+
exit 1
206+
fi
207+
num=$(fetch_status)
208+
ci_status=$(echo "$num" | jq -r --argjson pipelineId "$pipelineId" \'.result.ciWorkflowStatus[] | select(.ciPipelineId == $pipelineId) | .ciStatus\')
209+
sleep $sleep_time
210+
done
211+
if [ "$ci_status" = "Succeeded" ]; then
212+
echo "The final status of the Job is: $ci_status"
213+
job_completed=true
214+
elif [ "$ci_status" = "Failed" ]; then
215+
echo "The final status of the Job is: $ci_status"
216+
else
217+
echo "The final status of the Job is: $ci_status (Timeout)"
218+
fi
219+
220+
if [ "$job_completed" = true ]; then
221+
echo "The Job has been Scuccessfully completed"
222+
else
223+
exit 1
224+
fi
225+
fi',
226+
'SHELL',
227+
'f',
228+
'now()',
229+
1,
230+
'now()',
231+
1
232+
);
233+
234+
235+
--Plugin Step
236+
237+
INSERT INTO "plugin_step" ("id", "plugin_id","name","description","index","step_type","script_id","deleted", "created_on", "created_by", "updated_on", "updated_by") VALUES (nextval('id_seq_plugin_step'), (SELECT id FROM plugin_metadata WHERE name='Devtron Job Trigger v1.0.0'),'Step 1','Runnig the plugin','1','INLINE',(SELECT last_value FROM id_seq_plugin_pipeline_script),'f','now()', 1, 'now()', 1);
238+
239+
240+
-- Input Variables
241+
242+
INSERT INTO plugin_step_variable (id,plugin_step_id,name,format, description,is_exposed,allow_empty_value,default_value,value,variable_type,value_type,previous_step_index,variable_step_index,variable_step_index_in_plugin,reference_variable_name,deleted,created_on,created_by,updated_on,updated_by)VALUES
243+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Devtron Job Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'DevtronApiToken','STRING','Enter Devtron API Token with required permissions.','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
244+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Devtron Job Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'DevtronEndpoint','STRING','Enter the URL of Devtron Dashboard for.eg (https://abc.xyz).','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
245+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Devtron Job Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'DevtronJob','STRING','Enter the name or ID of the Devtron Job to be triggered.','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
246+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Devtron Job Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'DevtronEnv','STRING','Enter the name or ID of the Environment where the Job is to be triggered. Required if JobPipeline is not given.','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
247+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Devtron Job Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'JobPipeline','STRING','Enter the name or ID of the Job pipeline to be triggered. Required if DevtronEnv is not given.','t','t',null,null,'INPUT','NEW',null,1,null,null, 'f','now()',1,'now()',1),
248+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Devtron Job Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'GitCommitHash','STRING','Enter the commit hash from which the job is to be triggered. If not given then, will pick the latest.','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
249+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Devtron Job Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'StatusTimeoutSeconds','NUMBER','Enter the maximum time to wait for the job status.', 't','t',-1,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);

0 commit comments

Comments
 (0)