Skip to content

Commit 7770d60

Browse files
kirandevtnkomalreddy3
authored andcommitted
feat: Added the Devtron CI Trigger Plugin (#4902) (#4908)
* Added the Devtron CI Trigger Plugin * Rename 235_devtron_ci_trigger_plugin.down.sql to 236_devtron_ci_trigger_plugin.down.sql * Rename 235_devtron_ci_trigger_plugin.up.sql to 236_devtron_ci_trigger_plugin.up.sql * Rename 236_devtron_ci_trigger_plugin.down.sql to 237_devtron_ci_trigger_plugin.down.sql * Rename 236_devtron_ci_trigger_plugin.up.sql to 237_devtron_ci_trigger_plugin.up.sql
1 parent af9bd21 commit 7770d60

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
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 CI Trigger v1.0.0');
2+
DELETE FROM plugin_step where plugin_id=(SELECT id FROM plugin_metadata WHERE name='Devtron CI Trigger v1.0.0' );
3+
DELETE FROM plugin_pipeline_script where id=(SELECT id FROM plugin_metadata WHERE name='Devtron CI Trigger v1.0.0');
4+
DELETE FROM plugin_stage_mapping where plugin_id=(SELECT id from plugin_metadata where name='Devtron CI Trigger v1.0.0');
5+
DELETE FROM plugin_metadata where name='Devtron CI Trigger v1.0.0';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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 CI Trigger v1.0.0','Triggers the CI pipeline of Devtron Application','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 CI 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+
triggeredFromAppName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.appName\')
16+
triggeredFromPipelineName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.pipelineName\')
17+
18+
sleep_time=5
19+
is_number() {
20+
[[ "$1" =~ ^[0-9]+$ ]]
21+
}
22+
if [ $Timeout -gt 100 ]; then
23+
sleep_time=15
24+
fi
25+
echo "------------------------------------------"
26+
echo "Starting plugin Devtron CI Trigger v1.0.0"
27+
echo "------------------------------------------"
28+
app_list=$(curl -s -H "token:$DevtronApiToken" "$DevtronEndpoint/orchestrator/app/autocomplete")
29+
code=$(echo $app_list | jq -r ".code")
30+
if [ $code -ne 200 ];then
31+
result=$(echo $app_list | jq -r \'.result\')
32+
echo "Error: $result! Please check the API token provided"
33+
exit 1
34+
fi
35+
if is_number $DevtronApp;then
36+
app_id=$DevtronApp
37+
else
38+
app_id=$(echo "$app_list" | jq -r --arg DevtronApp "$DevtronApp" \'.result[] | select(.name == $DevtronApp) | .id\')
39+
fi
40+
if ! [ $app_id ];then
41+
echo "App $DevtronApp not found! Please check the details entered. for eg.(DevtronApp,DevtronEnv,DevtronEndpoint)"
42+
exit 1
43+
fi
44+
app_workflow=$(curl -s -H "token:$DevtronApiToken" "$DevtronEndpoint/orchestrator/app/app-wf/view/$app_id")
45+
if is_number $CiPipeline;then
46+
ci_pipeline_id=$CiPipeline
47+
else
48+
if [ $DevtronEnv ];then
49+
if is_number $DevtronEnv;then
50+
ci_pipeline_id=$(echo "$app_workflow" | jq -r --argjson DevtronEnv $DevtronEnv \'.result.cdConfig.pipelines[] | select(.environmentId == $DevtronEnv) | .ciPipelineId\')
51+
else
52+
ci_pipeline_id=$(echo "$app_workflow" | jq -r --arg DevtronEnv "$DevtronEnv" \'.result.cdConfig.pipelines[] | select(.environmentName == $DevtronEnv) | .ciPipelineId\')
53+
fi
54+
elif [ $CiPipeline ];then
55+
ci_pipeline_id=$(echo "$app_workflow" | jq -r --arg CiPipeline "$CiPipeline" \'.result.ciConfig.ciPipelines[] | select(.name == $CiPipeline) | .id\')
56+
else
57+
echo "You must provide one of the fields: DevtronEnv or CiPipeline"
58+
echo "Error: DevtronEnv or ciPipelineId not provided"
59+
exit 1
60+
fi
61+
fi
62+
if [ ! $ci_pipeline_id ];then
63+
echo "Please check the CI Pipeline Name or DevtronEnv"
64+
exit 1
65+
fi
66+
git_material_id=$(echo "$app_workflow" | jq -r --argjson ci_pipeline_id "$ci_pipeline_id" \'.result.ciConfig.ciPipelines[] | select(.id == $ci_pipeline_id) | .ciMaterial[0].gitMaterialId\')
67+
if [ $GitCommitHash ];then
68+
curl_req=$(curl -s "$DevtronEndpoint/orchestrator/app/ci-pipeline/trigger" \\
69+
-H "token: $DevtronApiToken" \\
70+
--data-raw \'{"triggered_from_app":\'"${triggeredFromAppName}"\',"triggered_from_pipeline":\'"${triggeredFromPipelineName}"\',"pipelineId":\'"$ci_pipeline_id"\',"ciPipelineMaterials":[{"Id":\'"$git_material_id"\',"GitCommit":{"Commit":"\'"$GitCommitHash"\'"}}],"pipelineType":"CI_BUILD"}\')
71+
code=$(echo "$curl_req" | jq -r \'.code\')
72+
if [ $code -ne 200 ];then
73+
error=$(echo "$curl_req" | jq -r \'.errors[]\')
74+
echo "$error"
75+
echo "CI Pipeline details could not be found. Please check!"
76+
exit 1
77+
fi
78+
echo "The build with CI pipeline ID $ci_pipeline_id of application $DevtronApp is triggered using the commit: $GitCommitHash"
79+
else
80+
git_material=$(curl -s "$DevtronEndpoint/orchestrator/app/ci-pipeline/$ci_pipeline_id/material" \\
81+
-H "token: $DevtronApiToken")
82+
result=$(echo $git_material | jq -r \'.result[]\')
83+
history=$(echo $result | jq -r \'.history[0]\')
84+
GitCommitHash=$(echo $history | jq -r \'.Commit\')
85+
curl_req=$(curl -s "$DevtronEndpoint/orchestrator/app/ci-pipeline/trigger" \\
86+
-H "token: $DevtronApiToken" \\
87+
--data-raw \'{"triggered_from_app":\'"${triggeredFromAppName}"\',"triggered_from_pipeline":\'"${triggeredFromPipelineName}"\',"pipelineId":\'"$ci_pipeline_id"\',"ciPipelineMaterials":[{"Id":\'"$git_material_id"\',"GitCommit":{"Commit":"\'"$GitCommitHash"\'"}}],"pipelineType":"CI_BUILD"}\')
88+
code=$(echo "$curl_req" | jq -r \'.code\')
89+
if [ $code -ne 200 ];then
90+
error=$(echo "$curl_req" | jq -r \'.errors[]\')
91+
echo "$error"
92+
echo "CI Pipeline details could not be found. Please check!"
93+
exit 1
94+
fi
95+
echo "The build with CI pipeline ID $ci_pipeline_id of application $DevtronApp is triggered using the latest commit: $GitCommitHash"
96+
fi
97+
if [ $Timeout -eq -1 ] || [ $Timeout -eq 0 ];then
98+
echo "Pipeline has been Triggered"
99+
else
100+
sleep 1
101+
fetch_status() {
102+
curl --silent "$DevtronEndpoint/orchestrator/app/workflow/status/$app_id/v2" \\
103+
-H "token: $DevtronApiToken"
104+
}
105+
num=$(fetch_status)
106+
ci_status=$(echo $num | jq -r --argjson ci_pipeline_id $ci_pipeline_id \'.result.ciWorkflowStatus[] | select(.ciPipelineId == $ci_pipeline_id) | .ciStatus\');
107+
echo "The current status of the build is: $ci_status";
108+
echo "Maximum waiting time is : $Timeout seconds"
109+
echo "Waiting for the process to complete......"
110+
start_time=$(date +%s)
111+
job_completed=false
112+
while [ "$ci_status" != "Succeeded" ]; do
113+
if [ "$ci_status" == "Failed" ]; then
114+
echo "The build has been Failed"
115+
exit 1
116+
elif [ "$ci_status" == "CANCELLED" ];then
117+
echo "Build has been Cancelled"
118+
exit 1
119+
fi
120+
current_time=$(date +%s)
121+
elapsed_time=$((current_time - start_time))
122+
if [ "$elapsed_time" -ge "$Timeout" ]; then
123+
echo "Timeout reached. Terminating the current process...."
124+
exit 1
125+
break
126+
fi
127+
num=$(fetch_status)
128+
ci_status=$(echo $num | jq -r --argjson ci_pipeline_id $ci_pipeline_id \'.result.ciWorkflowStatus[] | select(.ciPipelineId == $ci_pipeline_id) | .ciStatus\')
129+
sleep $sleep_time
130+
done
131+
if [ "$ci_status" = "Succeeded" ]; then
132+
echo "The final status of the build is: $ci_status"
133+
job_completed=true
134+
elif [ "$ci_status" = "Failed" ]; then
135+
echo "The final status of the Build is: $ci_status"
136+
else
137+
echo "The final status of the Build is: $ci_status (Timeout)"
138+
fi
139+
140+
if [ "$job_completed" = true ]; then
141+
echo "The triggered Build is Scuccessfully completed"
142+
else
143+
exit 1
144+
fi
145+
fi',
146+
'SHELL',
147+
'f',
148+
'now()',
149+
1,
150+
'now()',
151+
1
152+
);
153+
154+
155+
--Plugin Step
156+
157+
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 CI 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);
158+
159+
160+
-- Input Variables
161+
162+
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
163+
(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 CI 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),
164+
(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 CI 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),
165+
(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 CI Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'DevtronApp','STRING','Enter the name or ID of the Application whose build is to be triggered.','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
166+
(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 CI Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'DevtronEnv','STRING','Enter the name or ID of the Environment to which the CI is attached. Required if CiPipeline is not given.','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
167+
(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 CI Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'CiPipeline','STRING','Enter the name or ID of the CI 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),
168+
(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 CI Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'GitCommitHash','STRING','Enter the commit hash from which the build 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),
169+
(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 CI Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'Timeout','NUMBER','Enter the maximum time to wait for the build status.', 't','t',-1,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);

0 commit comments

Comments
 (0)