Skip to content

Commit 626ee68

Browse files
authored
Merge branch 'develop' into registry-fix
2 parents 7dbf1df + 1da9a50 commit 626ee68

File tree

5 files changed

+312
-1
lines changed

5 files changed

+312
-1
lines changed

pkg/appStore/installedApp/repository/InstalledAppRepository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ func (impl *InstalledAppRepositoryImpl) GetInstalledAppByAppIdAndDeploymentType(
815815
var installedApps InstalledApps
816816
queryString := `select * from installed_apps
817817
left join deployment_config dc on dc.active=true and dc.app_id = installed_apps.app_id and dc.environment_id=installed_apps.environment_id
818-
where active=? and app_id=? and (installed_apps.deployment_app_type=? or dc.deployment_app_type=?);`
818+
where installed_apps.active=? and installed_apps.app_id=? and (installed_apps.deployment_app_type=? or dc.deployment_app_type=?);`
819819
_, err := impl.dbConnection.Query(&installedApps, queryString, true, appId, deploymentAppType, deploymentAppType)
820820
if err != nil {
821821
impl.Logger.Errorw("error in fetching InstalledApp", "err", err)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE from devtron_resource_searchable_key ds where ds."name" in ('BASE_CONFIGURATION');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT INTO devtron_resource_searchable_key(name, is_removed, created_on, created_by, updated_on, updated_by)
2+
VALUES ('BASE_CONFIGURATION', false, now(), 1, now(), 1);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DELETE FROM plugin_step_variable WHERE plugin_step_id=(SELECT id FROM plugin_metadata WHERE name='Devtron CD Trigger' and plugin_version='1.3.0');
2+
DELETE FROM plugin_step where plugin_id=(SELECT id FROM plugin_metadata WHERE name='Devtron CD Trigger' and plugin_version='1.3.0');
3+
DELETE FROM plugin_pipeline_script where id=(SELECT id FROM plugin_metadata WHERE name='Devtron CD Trigger');
4+
DELETE FROM plugin_stage_mapping where plugin_id=(SELECT id from plugin_metadata where name='Devtron CD Trigger');
5+
DELETE FROM plugin_metadata where name='Devtron CD Trigger';
6+
UPDATE plugin_metadata SET is_latest = true WHERE id = (SELECT id FROM plugin_metadata WHERE name= 'Devtron CD Trigger' and is_latest= false and plugin_version='1.2.0');
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
-- update the is_latest flag to false, for the plugin version 1.0.0
2+
UPDATE plugin_metadata SET is_latest = false WHERE id = (SELECT id FROM plugin_metadata WHERE name= 'Devtron CD Trigger' and is_latest= true);
3+
4+
-- insert the new plugin version 1.3.0
5+
INSERT INTO "plugin_metadata" ("id", "name", "description","deleted", "created_on", "created_by", "updated_on", "updated_by","plugin_parent_metadata_id","plugin_version","is_deprecated","is_latest")
6+
VALUES (nextval('id_seq_plugin_metadata'), 'Devtron CD Trigger','Triggers the CD pipeline of Devtron Application','f', 'now()', 1, 'now()', 1, (SELECT id FROM plugin_parent_metadata WHERE identifier='devtron-cd-trigger-v1-0-0'),'1.3.0', false, true);
7+
8+
INSERT INTO "plugin_stage_mapping" ("plugin_id","stage_type","created_on", "created_by", "updated_on", "updated_by")
9+
VALUES ((SELECT id FROM plugin_metadata WHERE plugin_version='1.3.0' and name='Devtron CD Trigger' and deleted= false),0,'now()', 1, 'now()', 1);
10+
11+
INSERT INTO "plugin_pipeline_script" ("id", "script","type","deleted","created_on", "created_by", "updated_on", "updated_by")VALUES (
12+
nextval('id_seq_plugin_pipeline_script'),
13+
E'#!/bin/bash
14+
pipeline_type=$(echo $CI_CD_EVENT | jq -r \'.type\')
15+
if [[ "$pipeline_type" == "CI" ]]; then
16+
triggeredFromAppName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.appName\')
17+
triggeredFromPipelineName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.pipelineName\')
18+
elif [[ "$pipeline_type" == "CD" ]]; then
19+
triggeredFromAppName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.extraEnvironmentVariables.APP_NAME\')
20+
triggeredFromPipelineName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.Pipeline.Name\')
21+
fi
22+
# Convert CD Workflow Type to uppercase to make it case-insensitive
23+
TargetTriggerStage=$(echo "$TargetTriggerStage" | tr \'[:lower:]\' \'[:upper:]\')
24+
25+
# Set default value for TargetTriggerStage to DEPLOY if not provided or invalid
26+
case $TargetTriggerStage in
27+
"PRE")
28+
;;
29+
"POST")
30+
;;
31+
"DEPLOY")
32+
;;
33+
"")
34+
TargetTriggerStage="DEPLOY" # Set to DEPLOY if no input provided
35+
;;
36+
*)
37+
echo "Error: Invalid CD Workflow Type. Please enter PRE/DEPLOY/POST. Exiting..."
38+
exit 1
39+
;;
40+
esac
41+
42+
# Set default value for StatusTimeOutSeconds to 0 if not provided or not an integer
43+
if ! [[ "$StatusTimeOutSeconds" =~ ^[0-9]+$ ]]; then
44+
StatusTimeOutSeconds=0
45+
fi
46+
47+
DevtronEndpoint=$(echo "$DevtronEndpoint" | sed \'s:/*$::\')
48+
49+
# Determine sleep interval based on StatusTimeOutSeconds
50+
if [ "$StatusTimeOutSeconds" -lt "60" ]; then
51+
sleepInterval=$(($StatusTimeOutSeconds / 2))
52+
else
53+
sleepInterval=30
54+
fi
55+
56+
verify(){
57+
local response=$(curl -s -H "token: $DevtronApiToken" "$DevtronEndpoint/orchestrator/devtron/auth/verify")
58+
echo $response
59+
}
60+
verify_response=$(verify)
61+
verify_status=$( echo "$verify_response" | jq \'.code\')
62+
# echo $verify_status
63+
if [[ "$verify_status" == "401" ]]; then
64+
echo "Enter the valid DevtronApiToken. Exiting..."
65+
exit 1
66+
elif [[ -z "$verify_status" ]]; then
67+
echo "Enter the valid DevtronEndpoint. Exiting..."
68+
exit 1
69+
fi
70+
71+
fetch_app_id() {
72+
# Check if DevtronApp is numeric, if yes, use it directly as App ID
73+
if [[ "$DevtronApp" =~ ^[0-9]+$ ]]; then
74+
echo "$DevtronApp"
75+
else
76+
local api_response=$(curl -s -H "token: $DevtronApiToken" "$DevtronEndpoint/orchestrator/app/autocomplete")
77+
local app_id=$(echo "$api_response" | jq -r --arg app_name "$DevtronApp" \'.result[] | select(.name == $app_name) | .id\')
78+
79+
if [[ -z "$app_id" || "$app_id" == "null" ]]; then
80+
echo "Error: Application \'$DevtronApp\' not found. Please verify the DevtronApp."
81+
exit 1
82+
fi
83+
echo "$app_id"
84+
fi
85+
}
86+
87+
fetch_env_id() {
88+
# Check if DevtronEnv is numeric, if yes, use it directly as Env ID
89+
if [[ "$DevtronEnv" =~ ^[0-9]+$ ]]; then
90+
echo "$DevtronEnv"
91+
else
92+
local api_response=$(curl -s -H "token: $DevtronApiToken" "$DevtronEndpoint/orchestrator/env/autocomplete")
93+
local env_id=$(echo "$api_response" | jq -r --arg env_name "$DevtronEnv" \'.result[] | select(.environment_name == $env_name) | .id\')
94+
95+
if [[ -z "$env_id" || "$env_id" == "null" ]]; then
96+
echo "Error: Environment \'$DevtronEnv\' not found. Please verify the DevtronEnv."
97+
exit 1
98+
fi
99+
echo "$env_id"
100+
fi
101+
}
102+
103+
fetch_pipeline_id() {
104+
local app_id=$1
105+
local env_id=$2
106+
local api_response=$(curl -s -H "token: $DevtronApiToken" "$DevtronEndpoint/orchestrator/app/app-wf/view/$app_id")
107+
local pipeline_id=$(echo "$api_response" | jq -r --arg env_id "$env_id" \'.result.cdConfig.pipelines[] | select(.environmentId == ($env_id | tonumber)) | .id\')
108+
109+
if [[ -z "$pipeline_id" || "$pipeline_id" == "null" ]]; then
110+
echo "Error: Pipeline not found for the provided Environment. Please verify the Environment ID."
111+
echo "Environment ID: $env_id"
112+
echo "API Response: $api_response"
113+
exit 1
114+
fi
115+
echo "$pipeline_id"
116+
}
117+
118+
multigit_hash_id(){
119+
120+
local len=$1
121+
local artifact_id=""
122+
local apiResponse=$2
123+
i=0
124+
while [[ "$i" -lt "$len" ]]; do
125+
id2=$(echo "$apiResponse" | jq -r --argjson index "$i" \'.result.ci_artifacts[$index].material_info[].revision \')
126+
if [[ "$hash" == "$id2" ]];then
127+
id=$(echo "$apiResponse" | jq -r --argjson index "$i" \'.result.ci_artifacts[$index].id \')
128+
artifact_id=$id
129+
break
130+
fi
131+
i=$((i + 1))
132+
done
133+
echo "$artifact_id"
134+
}
135+
136+
fetch_ci_artifact_id() {
137+
local pipeline_id=$1
138+
local apiUrl="$DevtronEndpoint/orchestrator/app/cd-pipeline/$pipeline_id/material?offset=0&size=20&stage=$TargetTriggerStage"
139+
local apiResponse=$(curl -s -H "token: $DevtronApiToken" "$apiUrl")
140+
141+
local ciArtifactId=""
142+
if [[ -n "$GitCommitHash" ]]; then
143+
ciArtifactId=$(echo "$apiResponse" | jq -r --arg hash "$GitCommitHash" \'.result.ci_artifacts[] | select(.material_info[].revision == $hash) | .id\' | head -n 1)
144+
if [[ -z "$ciArtifactId" || "$ciArtifactId" == "null" || "$ciArtifactId" == "" ]]; then
145+
echo "Error: CI Artifact ID for the provided commit hash \'$GitCommitHash\' not found. Please verify the commit hash."
146+
exit 1
147+
fi
148+
elif [[ "$TriggerFromSameHash" == "true" ]]; then
149+
len=$(echo "$apiResponse" | jq -r \'.result.ci_artifacts\' | jq \'length\')
150+
image=$(echo $CI_CD_EVENT | jq -r \'.commonWorkflowRequest.ciArtifactDTO.image\')
151+
hash=$(echo $CI_CD_EVENT | jq -r \'.commonWorkflowRequest.ciProjectDetails[].commitHash\')
152+
id1=$(echo "$apiResponse" | jq -r --arg image "$image" \'.result.ci_artifacts[] | select(.image == $image) | .id\' | head -n 1)
153+
if [[ -z "$id" || "$id" == "null" || "id" == "" ]]; then
154+
id1=$(multigit_hash_id "$len" "$apiResponse")
155+
fi
156+
ciArtifactId=$id1
157+
if [[ -z "$ciArtifactId" || "$ciArtifactId" == "null" || "$ciArtifactId" == "" ]]; then
158+
echo "Error: CI Artifact ID for the provided commit hash \'$GitCommitHash\' not found. Please verify the commit hash."
159+
exit 1
160+
fi
161+
else
162+
ciArtifactId=$(echo "$apiResponse" | jq -r \'.result.ci_artifacts[0].id\')
163+
if [[ -z "$ciArtifactId" || "$ciArtifactId" == "null" ]]; then
164+
echo "Error: CI Artifact ID not found."
165+
exit 1
166+
fi
167+
fi
168+
169+
echo "$ciArtifactId"
170+
}
171+
172+
# Fetch the app ID. Exit the script if the app name is incorrect.
173+
app_id=$(fetch_app_id)
174+
if [ $? -ne 0 ]; then
175+
echo "Error: Application \'$DevtronApp\' not found. Enter the correct App Name. Exiting...."
176+
exit 1
177+
fi
178+
179+
# Fetch the env ID. Exit the script if the environment name or ID is incorrect.
180+
env_id=$(fetch_env_id)
181+
if [ $? -ne 0 ]; then
182+
echo "Error: Environment \'$DevtronEnv\' not found. Enter the correct Environment Name. Exiting..."
183+
exit 1
184+
fi
185+
186+
# Fetch the pipeline ID using the env ID. Exit the script if the environment is incorrect.
187+
pipeline_id=$(fetch_pipeline_id "$app_id" "$env_id")
188+
if [ $? -ne 0 ]; then
189+
echo "Verify your App Name/ID and Env Name/ID. Exiting..."
190+
exit 1
191+
fi
192+
193+
# Fetch the CI Artifact ID based on the commit hash.
194+
ciArtifactId=$(fetch_ci_artifact_id "$pipeline_id")
195+
if [ $? -ne 0 ]; then
196+
echo "Enter the correct GitCommitHash. Exiting..."
197+
exit 1
198+
fi
199+
200+
trigger_cd_pipeline() {
201+
local pipeline_id=$1
202+
local app_id=$2
203+
local ciArtifactId=$3
204+
local jsonPayload=$(jq -n \\
205+
--arg pipelineId "$pipeline_id" \\
206+
--arg appId "$app_id" \\
207+
--arg ciArtifactId "$ciArtifactId" \\
208+
--arg TargetTriggerStage "$TargetTriggerStage" \\
209+
--arg deploymentWithConfig "LAST_SAVED_CONFIG" \\
210+
--arg triggeredFromAppName "$triggeredFromAppName" \\
211+
--arg triggeredFromPipelineName "$triggeredFromPipelineName" \\
212+
\'{
213+
pipelineId: ($pipelineId | tonumber),
214+
appId: ($appId | tonumber),
215+
ciArtifactId: ($ciArtifactId | tonumber),
216+
cdWorkflowType: $TargetTriggerStage,
217+
deploymentWithConfig: $deploymentWithConfig,
218+
triggeredFromAppName: $triggeredFromAppName,
219+
triggeredFromPipelineName: $triggeredFromPipelineName
220+
}\')
221+
222+
curl -sS -X POST "$DevtronEndpoint/orchestrator/app/cd-pipeline/trigger" \\
223+
-H "Content-Type: application/json" \\
224+
-H "token: $DevtronApiToken" \\
225+
--data "$jsonPayload" \\
226+
--compressed
227+
}
228+
229+
echo "Triggering CD Pipeline for App ID: $app_id, Pipeline ID: $pipeline_id, CI Artifact ID: $ciArtifactId, and CD Workflow Type: $TargetTriggerStage"
230+
cd_pipeline=$(trigger_cd_pipeline "$pipeline_id" "$app_id" "$ciArtifactId")
231+
232+
check_deploy_status() {
233+
if [ "$StatusTimeOutSeconds" -le "0" ]; then
234+
echo "Skipping deployment status check. Taking StatusTimeOutSecond0 as a default input"
235+
return
236+
fi
237+
238+
local appId=$1
239+
local pipelineId=$2
240+
local max_wait=$StatusTimeOutSeconds
241+
local statusKey="deploy_status" # Default status key
242+
243+
if [[ "$TargetTriggerStage" == "PRE" ]]; then
244+
statusKey="pre_status"
245+
elif [[ "$TargetTriggerStage" == "POST" ]]; then
246+
statusKey="post_status"
247+
fi
248+
249+
local start_time=$(date +%s)
250+
251+
while :; do
252+
local current_time=$(date +%s)
253+
local elapsed_time=$((current_time - start_time))
254+
255+
if [ "$elapsed_time" -ge "$max_wait" ]; then
256+
echo "Timeout reached without success. Exiting..."
257+
exit 1
258+
fi
259+
260+
local statusUrl="$DevtronEndpoint/orchestrator/app/workflow/status/$appId/v2"
261+
local response=$(curl -s -H "token: $DevtronApiToken" "$statusUrl")
262+
local code=$(echo "$response" | jq -r \'.code\')
263+
264+
if [ "$code" != "200" ]; then
265+
echo "Error: Received response - $response. Exiting..."
266+
exit 1
267+
fi
268+
local status=$(echo "$response" | jq -r --arg pipelineId "$pipelineId" --arg statusKey "$statusKey" \'.result.cdWorkflowStatus[] | select(.pipeline_id == ($pipelineId | tonumber)) | .[$statusKey]\')
269+
270+
echo "Current $TargetTriggerStage status: $status"
271+
272+
if [[ "$status" == "Succeeded" ]]; then
273+
echo "Deployment succeeded."
274+
break
275+
elif [[ "$status" == "Failed" ]]; then
276+
echo "$TargetTriggerStage workflow failed."
277+
exit 1
278+
fi
279+
280+
sleep $sleepInterval
281+
done
282+
}
283+
284+
# Optionally check the deployment status based on the CD workflow type
285+
check_deploy_status "$app_id" "$pipeline_id"' ,'SHELL','f','now()',1,'now()',1);
286+
287+
288+
INSERT INTO "plugin_step" ("id", "plugin_id","name","description","index","step_type","script_id","deleted", "created_on", "created_by", "updated_on", "updated_by")
289+
VALUES (nextval('id_seq_plugin_step'), (SELECT id FROM plugin_metadata WHERE name='Devtron CD Trigger' AND plugin_version='1.3.0' AND deleted= false),'Step 1','Runnig the plugin','1','INLINE',(SELECT last_value FROM id_seq_plugin_pipeline_script),'f','now()', 1, 'now()', 1);
290+
291+
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)
292+
VALUES (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 CD Trigger' and p.plugin_version='1.3.0' and ps."index"=1 and ps.deleted=false),'DevtronApiToken','STRING','Enter Devtron API Token','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
293+
(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 CD Trigger' and p.plugin_version='1.3.0' and ps."index"=1 and ps.deleted=false),'DevtronEndpoint','STRING','Enter URL of Devtron','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
294+
(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 CD Trigger' and p.plugin_version='1.3.0' and ps."index"=1 and ps.deleted=false),'DevtronApp','STRING','Enter the Devtron Application name/Id','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
295+
(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 CD Trigger' and p.plugin_version='1.3.0' and ps."index"=1 and ps.deleted=false),'DevtronEnv','STRING','Enter the Environment name/Id','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
296+
(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 CD Trigger' and p.plugin_version='1.3.0' and ps."index"=1 and ps.deleted=false),'StatusTimeOutSeconds','STRING','Enter the maximum time (in seconds) a user can wait for the application to deploy.Enter a postive integer value','t','t',0,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
297+
(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 CD Trigger' and p.plugin_version='1.3.0' and ps."index"=1 and ps.deleted=false),'GitCommitHash','STRING','Enter the git hash from which user wants to deploy its application. By deault it takes latest Artifact ID to deploy the application','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
298+
(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 CD Trigger' and p.plugin_version='1.3.0' and ps."index"=1 and ps.deleted=false),'TargetTriggerStage','STRING','Enter Trigger Stage PRE/DEPLOY/POST, Default DEPLOY','t','t','DEPLOY',null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
299+
(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 CD Trigger' and p.plugin_version='1.3.0' and ps."index"=1 and ps.deleted=false),'TriggerFromSameHash','STRING','Provide value true if want to trigger CD with same hash','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);
300+
301+
302+

0 commit comments

Comments
 (0)