|
| 1 | +UPDATE plugin_metadata SET is_latest = false WHERE id = (SELECT id FROM plugin_metadata WHERE name= 'Devtron CD Trigger v1.0.0' and is_latest= true); |
| 2 | + |
| 3 | +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") |
| 4 | +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.1.0', false, true); |
| 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 plugin_version='1.1.0' and name='Devtron CD Trigger' and deleted= false),0,'now()', 1, 'now()', 1); |
| 8 | + |
| 9 | +INSERT INTO "plugin_pipeline_script" ("id", "script","type","deleted","created_on", "created_by", "updated_on", "updated_by")VALUES ( |
| 10 | + nextval('id_seq_plugin_pipeline_script'), |
| 11 | + E'#!/bin/bash |
| 12 | +pipeline_type=$(echo $CI_CD_EVENT | jq -r \'.type\') |
| 13 | +if [[ "$pipeline_type" == "CI" ]]; then |
| 14 | + triggeredFromAppName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.appName\') |
| 15 | + triggeredFromPipelineName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.pipelineName\') |
| 16 | +elif [[ "$pipeline_type" == "CD" ]]; then |
| 17 | + triggeredFromAppName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.extraEnvironmentVariables.APP_NAME\') |
| 18 | + triggeredFromPipelineName=$(echo $CI_CD_EVENT | jq \'.commonWorkflowRequest.Pipeline.Name\') |
| 19 | +fi |
| 20 | +# Convert CD Workflow Type to uppercase to make it case-insensitive |
| 21 | +TargetTriggerStage=$(echo "$TargetTriggerStage" | tr \'[:lower:]\' \'[:upper:]\') |
| 22 | +
|
| 23 | +# Set default value for TargetTriggerStage to DEPLOY if not provided or invalid |
| 24 | +case $TargetTriggerStage in |
| 25 | +"PRE") |
| 26 | + ;; |
| 27 | +"POST") |
| 28 | + ;; |
| 29 | +"DEPLOY") |
| 30 | + ;; |
| 31 | +"") |
| 32 | + TargetTriggerStage="DEPLOY" # Set to DEPLOY if no input provided |
| 33 | + ;; |
| 34 | +*) |
| 35 | + echo "Error: Invalid CD Workflow Type. Please enter PRE/DEPLOY/POST. Exiting..." |
| 36 | + exit 1 |
| 37 | + ;; |
| 38 | +esac |
| 39 | +
|
| 40 | +# Set default value for StatusTimeOutSeconds to 0 if not provided or not an integer |
| 41 | +if ! [[ "$StatusTimeOutSeconds" =~ ^[0-9]+$ ]]; then |
| 42 | + StatusTimeOutSeconds=0 |
| 43 | +fi |
| 44 | +
|
| 45 | +DevtronEndpoint=$(echo "$DevtronEndpoint" | sed \'s:/*$::\') |
| 46 | +
|
| 47 | +# Determine sleep interval based on StatusTimeOutSeconds |
| 48 | +if [ "$StatusTimeOutSeconds" -lt "60" ]; then |
| 49 | + sleepInterval=$(($StatusTimeOutSeconds / 2)) |
| 50 | +else |
| 51 | + sleepInterval=30 |
| 52 | +fi |
| 53 | +
|
| 54 | +verify(){ |
| 55 | + local response=$(curl -s -H "token: $DevtronApiToken" "$DevtronEndpoint/orchestrator/devtron/auth/verify") |
| 56 | + echo $response |
| 57 | + exit 1 |
| 58 | +} |
| 59 | +verify_response=$(verify) |
| 60 | +verify_status=$( echo "$verify_response" | jq \'.code\') |
| 61 | +# echo $verify_status |
| 62 | +if [[ "$verify_status" == "401" ]]; then |
| 63 | + echo "Enter the valid DevtronApiToken. Exiting..." |
| 64 | + exit 1 |
| 65 | +elif [[ -z "$verify_status" ]]; then |
| 66 | + echo "Enter the valid DevtronEndpoint. Exiting..." |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | +
|
| 70 | +fetch_app_id() { |
| 71 | + # Check if DevtronApp is numeric, if yes, use it directly as App ID |
| 72 | + if [[ "$DevtronApp" =~ ^[0-9]+$ ]]; then |
| 73 | + echo "$DevtronApp" |
| 74 | + else |
| 75 | + local api_response=$(curl -s -H "token: $DevtronApiToken" "$DevtronEndpoint/orchestrator/app/autocomplete") |
| 76 | + local app_id=$(echo "$api_response" | jq -r --arg app_name "$DevtronApp" \'.result[] | select(.name == $app_name) | .id\') |
| 77 | +
|
| 78 | + if [[ -z "$app_id" || "$app_id" == "null" ]]; then |
| 79 | + echo "Error: Application \'$DevtronApp\' not found. Please verify the DevtronApp." |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + echo "$app_id" |
| 83 | + fi |
| 84 | +} |
| 85 | +
|
| 86 | +fetch_env_id() { |
| 87 | + # Check if DevtronEnv is numeric, if yes, use it directly as Env ID |
| 88 | + if [[ "$DevtronEnv" =~ ^[0-9]+$ ]]; then |
| 89 | + echo "$DevtronEnv" |
| 90 | + else |
| 91 | + local api_response=$(curl -s -H "token: $DevtronApiToken" "$DevtronEndpoint/orchestrator/env/autocomplete") |
| 92 | + local env_id=$(echo "$api_response" | jq -r --arg env_name "$DevtronEnv" \'.result[] | select(.environment_name == $env_name) | .id\') |
| 93 | +
|
| 94 | + if [[ -z "$env_id" || "$env_id" == "null" ]]; then |
| 95 | + echo "Error: Environment \'$DevtronEnv\' not found. Please verify the DevtronEnv." |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | + echo "$env_id" |
| 99 | + fi |
| 100 | +} |
| 101 | +
|
| 102 | +fetch_pipeline_id() { |
| 103 | + local app_id=$1 |
| 104 | + local env_id=$2 |
| 105 | + local api_response=$(curl -s -H "token: $DevtronApiToken" "$DevtronEndpoint/orchestrator/app/app-wf/view/$app_id") |
| 106 | + local pipeline_id=$(echo "$api_response" | jq -r --arg env_id "$env_id" \'.result.cdConfig.pipelines[] | select(.environmentId == ($env_id | tonumber)) | .id\') |
| 107 | +
|
| 108 | + if [[ -z "$pipeline_id" || "$pipeline_id" == "null" ]]; then |
| 109 | + echo "Error: Pipeline not found for the provided Environment. Please verify the Environment ID." |
| 110 | + echo "Environment ID: $env_id" |
| 111 | + echo "API Response: $api_response" |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | + echo "$pipeline_id" |
| 115 | +} |
| 116 | +
|
| 117 | +fetch_ci_artifact_id() { |
| 118 | + local pipeline_id=$1 |
| 119 | + local apiUrl="$DevtronEndpoint/orchestrator/app/cd-pipeline/$pipeline_id/material?offset=0&size=20&stage=$TargetTriggerStage" |
| 120 | + local apiResponse=$(curl -s -H "token: $DevtronApiToken" "$apiUrl") |
| 121 | +
|
| 122 | + local ciArtifactId="" |
| 123 | + if [[ -n "$GitCommitHash" ]]; then |
| 124 | + ciArtifactId=$(echo "$apiResponse" | jq -r --arg hash "$GitCommitHash" \'.result.ci_artifacts[] | select(.material_info[].revision == $hash) | .id\' | head -n 1) |
| 125 | + if [[ -z "$ciArtifactId" || "$ciArtifactId" == "null" || "$ciArtifactId" == "" ]]; then |
| 126 | + echo "Error: CI Artifact ID for the provided commit hash \'$GitCommitHash\' not found. Please verify the commit hash." |
| 127 | + exit 1 |
| 128 | + fi |
| 129 | + else |
| 130 | + ciArtifactId=$(echo "$apiResponse" | jq -r \'.result.ci_artifacts[0].id\') |
| 131 | + if [[ -z "$ciArtifactId" || "$ciArtifactId" == "null" ]]; then |
| 132 | + echo "Error: CI Artifact ID not found." |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | + fi |
| 136 | +
|
| 137 | + echo "$ciArtifactId" |
| 138 | +} |
| 139 | +
|
| 140 | +# Fetch the app ID. Exit the script if the app name is incorrect. |
| 141 | +app_id=$(fetch_app_id) |
| 142 | +if [ $? -ne 0 ]; then |
| 143 | + echo "Error: Application \'$DevtronApp\' not found. Enter the correct App Name. Exiting...." |
| 144 | + exit 1 |
| 145 | +fi |
| 146 | +
|
| 147 | +# Fetch the env ID. Exit the script if the environment name or ID is incorrect. |
| 148 | +env_id=$(fetch_env_id) |
| 149 | +if [ $? -ne 0 ]; then |
| 150 | + echo "Error: Environment \'$DevtronEnv\' not found. Enter the correct Environment Name. Exiting..." |
| 151 | + exit 1 |
| 152 | +fi |
| 153 | +
|
| 154 | +# Fetch the pipeline ID using the env ID. Exit the script if the environment is incorrect. |
| 155 | +pipeline_id=$(fetch_pipeline_id "$app_id" "$env_id") |
| 156 | +if [ $? -ne 0 ]; then |
| 157 | + echo "Verify your App Name/ID and Env Name/ID. Exiting..." |
| 158 | + exit 1 |
| 159 | +fi |
| 160 | +
|
| 161 | +# Fetch the CI Artifact ID based on the commit hash. |
| 162 | +ciArtifactId=$(fetch_ci_artifact_id "$pipeline_id") |
| 163 | +if [ $? -ne 0 ]; then |
| 164 | + echo "Enter the correct GitCommitHash. Exiting..." |
| 165 | + exit 1 |
| 166 | +fi |
| 167 | +
|
| 168 | +trigger_cd_pipeline() { |
| 169 | + local pipeline_id=$1 |
| 170 | + local app_id=$2 |
| 171 | + local ciArtifactId=$3 |
| 172 | + local jsonPayload=$(jq -n \\ |
| 173 | + --arg pipelineId "$pipeline_id" \\ |
| 174 | + --arg appId "$app_id" \\ |
| 175 | + --arg ciArtifactId "$ciArtifactId" \\ |
| 176 | + --arg TargetTriggerStage "$TargetTriggerStage" \\ |
| 177 | + --arg deploymentWithConfig "LAST_SAVED_CONFIG" \\ |
| 178 | + --arg triggeredFromAppName "$triggeredFromAppName" \\ |
| 179 | + --arg triggeredFromPipelineName "$triggeredFromPipelineName" \\ |
| 180 | + \'{ |
| 181 | + pipelineId: ($pipelineId | tonumber), |
| 182 | + appId: ($appId | tonumber), |
| 183 | + ciArtifactId: ($ciArtifactId | tonumber), |
| 184 | + cdWorkflowType: $TargetTriggerStage, |
| 185 | + deploymentWithConfig: $deploymentWithConfig, |
| 186 | + triggeredFromAppName: $triggeredFromAppName, |
| 187 | + triggeredFromPipelineName: $triggeredFromPipelineName |
| 188 | + }\') |
| 189 | +
|
| 190 | + curl -sS -X POST "$DevtronEndpoint/orchestrator/app/cd-pipeline/trigger" \\ |
| 191 | + -H "Content-Type: application/json" \\ |
| 192 | + -H "token: $DevtronApiToken" \\ |
| 193 | + --data "$jsonPayload" \\ |
| 194 | + --compressed |
| 195 | +} |
| 196 | +
|
| 197 | +echo "Triggering CD Pipeline for App ID: $app_id, Pipeline ID: $pipeline_id, CI Artifact ID: $ciArtifactId, and CD Workflow Type: $TargetTriggerStage" |
| 198 | +cd_pipeline=$(trigger_cd_pipeline "$pipeline_id" "$app_id" "$ciArtifactId") |
| 199 | +
|
| 200 | +check_deploy_status() { |
| 201 | + if [ "$StatusTimeOutSeconds" -le "0" ]; then |
| 202 | + echo "Skipping deployment status check. Taking StatusTimeOutSecond0 as a default input" |
| 203 | + return |
| 204 | + fi |
| 205 | +
|
| 206 | + local appId=$1 |
| 207 | + local pipelineId=$2 |
| 208 | + local max_wait=$StatusTimeOutSeconds |
| 209 | + local statusKey="deploy_status" # Default status key |
| 210 | +
|
| 211 | + if [[ "$TargetTriggerStage" == "PRE" ]]; then |
| 212 | + statusKey="pre_status" |
| 213 | + elif [[ "$TargetTriggerStage" == "POST" ]]; then |
| 214 | + statusKey="post_status" |
| 215 | + fi |
| 216 | +
|
| 217 | + local start_time=$(date +%s) |
| 218 | +
|
| 219 | + while :; do |
| 220 | + local current_time=$(date +%s) |
| 221 | + local elapsed_time=$((current_time - start_time)) |
| 222 | +
|
| 223 | + if [ "$elapsed_time" -ge "$max_wait" ]; then |
| 224 | + echo "Timeout reached without success. Exiting..." |
| 225 | + exit 1 |
| 226 | + fi |
| 227 | +
|
| 228 | + local statusUrl="$DevtronEndpoint/orchestrator/app/workflow/status/$appId/v2" |
| 229 | + local response=$(curl -s -H "token: $DevtronApiToken" "$statusUrl") |
| 230 | + local code=$(echo "$response" | jq -r \'.code\') |
| 231 | +
|
| 232 | + if [ "$code" != "200" ]; then |
| 233 | + echo "Error: Received response - $response. Exiting..." |
| 234 | + exit 1 |
| 235 | + fi |
| 236 | + local status=$(echo "$response" | jq -r --arg pipelineId "$pipelineId" --arg statusKey "$statusKey" \'.result.cdWorkflowStatus[] | select(.pipeline_id == ($pipelineId | tonumber)) | .[$statusKey]\') |
| 237 | +
|
| 238 | + echo "Current $TargetTriggerStage status: $status" |
| 239 | +
|
| 240 | + if [[ "$status" == "Succeeded" ]]; then |
| 241 | + echo "Deployment succeeded." |
| 242 | + break |
| 243 | + elif [[ "$status" == "Failed" ]]; then |
| 244 | + echo "$TargetTriggerStage workflow failed." |
| 245 | + exit 1 |
| 246 | + fi |
| 247 | +
|
| 248 | + sleep $sleepInterval |
| 249 | + done |
| 250 | +} |
| 251 | +
|
| 252 | +# Optionally check the deployment status based on the CD workflow type |
| 253 | +check_deploy_status "$app_id" "$pipeline_id"','SHELL','f','now()',1,'now()',1); |
| 254 | + |
| 255 | + |
| 256 | +INSERT INTO "plugin_step" ("id", "plugin_id","name","description","index","step_type","script_id","deleted", "created_on", "created_by", "updated_on", "updated_by") |
| 257 | +VALUES (nextval('id_seq_plugin_step'), (SELECT id FROM plugin_metadata WHERE name='Devtron CD Trigger'),'Step 1','Runnig the plugin','1','INLINE',(SELECT last_value FROM id_seq_plugin_pipeline_script),'f','now()', 1, 'now()', 1); |
| 258 | + |
| 259 | +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) |
| 260 | +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 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), |
| 261 | +(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 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), |
| 262 | +(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 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), |
| 263 | +(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 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), |
| 264 | +(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 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), |
| 265 | +(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 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), |
| 266 | +(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 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); |
0 commit comments