Skip to content

Commit 37082c3

Browse files
feat: add webhook trigger for CI pipeline, add git trigger for invent… (#28)
* feat: add webhook trigger for CI pipeline, add git trigger for inventory pipeline * fix: added webhook trigger script * chore: add cra config * fix: make inventory repo input optional * fix: duplicate toolchain created in tests --------- Co-authored-by: Vincent Burckhardt <vincent.burckhardt@ie.ibm.com>
1 parent f637364 commit 37082c3

File tree

7 files changed

+84
-2
lines changed

7 files changed

+84
-2
lines changed

cra-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ CRA_TARGETS:
1818
TF_VAR_watson_machine_learning_instance_crn: "crn:v1:bluemix:public:pm-20:us-south:a/2e92ee435f984c5dbf970577b2ceec1f:6966b4da-de4c-4dfb-a1d2-8a635f7eeeae::"
1919
TF_VAR_watson_machine_learning_instance_guid: "4e621b22-5802-4013-896a-777fc345f424"
2020
TF_VAR_watson_machine_learning_instance_resource_name: "test-machine-learning-instance"
21+
TF_VAR_inventory_repo_url: "https://us-south.git.cloud.ibm.com/yada.yada/04181-inventory-repo.git"

solutions/banking/main.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,61 @@ resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_integration_id_pip
211211
type = "text"
212212
value = data.external.assistant_get_integration_id.result.assistant_integration_id
213213
}
214+
215+
# Random string for webhook token
216+
resource "random_string" "webhook_secret" {
217+
length = 48
218+
special = false
219+
upper = false
220+
}
221+
222+
# Create webhook for CI pipeline
223+
resource "ibm_cd_tekton_pipeline_trigger" "ci_pipeline_webhook" {
224+
depends_on = [random_string.webhook_secret]
225+
type = "generic"
226+
pipeline_id = var.ci_pipeline_id
227+
name = "rag-webhook-trigger"
228+
event_listener = "ci-listener-gitlab"
229+
secret {
230+
type = "token_matches"
231+
source = "payload"
232+
key_name = "webhook-token"
233+
value = random_string.webhook_secret.result
234+
}
235+
}
236+
237+
# Create git trigger for CD pipeline - to run inventory promotion once CI pipeline is complete
238+
resource "ibm_cd_tekton_pipeline_trigger" "cd_pipeline_inventory_promotion_trigger" {
239+
count = var.inventory_repo_url != null ? 1 : 0
240+
type = "scm"
241+
pipeline_id = var.cd_pipeline_id
242+
name = "git-inventory-promotion-trigger"
243+
event_listener = "promotion-listener"
244+
events = ["push"]
245+
source {
246+
type = "git"
247+
properties {
248+
url = var.inventory_repo_url
249+
branch = "master"
250+
}
251+
}
252+
}
253+
254+
# Trigger webhook to start CI pipeline run
255+
resource "null_resource" "ci_pipeline_run" {
256+
depends_on = [
257+
ibm_cd_tekton_pipeline_trigger.ci_pipeline_webhook,
258+
ibm_cd_tekton_pipeline_property.watsonx_assistant_integration_id_pipeline_property_ci,
259+
ibm_cd_tekton_pipeline_property.watsonx_assistant_id_pipeline_property_ci,
260+
ibm_resource_instance.cd_instance
261+
]
262+
triggers = {
263+
always_run = timestamp()
264+
}
265+
266+
provisioner "local-exec" {
267+
command = "${path.module}/watson-scripts/webhook-trigger.sh \"${ibm_cd_tekton_pipeline_trigger.ci_pipeline_webhook.webhook_url}\" \"${random_string.webhook_secret.result}\""
268+
interpreter = ["/bin/bash", "-c"]
269+
quiet = true
270+
}
271+
}

solutions/banking/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ variable "cd_pipeline_id" {
4747
type = string
4848
}
4949

50+
variable "inventory_repo_url" {
51+
description = "URL of the inventory repository"
52+
type = string
53+
default = null
54+
}
55+
5056
variable "watson_assistant_instance_id" {
5157
description = "ID of the WatsonX Assistant service instance"
5258
type = string

solutions/banking/version.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ terraform {
1212
source = "hashicorp/null"
1313
version = ">= 3.2.1"
1414
}
15+
random = {
16+
source = "hashicorp/random"
17+
version = ">= 3.6.1"
18+
}
1519
restapi = {
1620
source = "Mastercard/restapi"
1721
version = ">= 1.19.1"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
WEBHOOK_URL="$1"
6+
WEBHOOK_SECRET="$2"
7+
8+
curl -X POST --header "Content-Type: application/json" --data "{\"webhook-token\":\"$WEBHOOK_SECRET\"}" "$WEBHOOK_URL"

tests/scripts/pre-validation.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ TF_VARS_FILE="terraform.tfvars"
4141
watson_machine_learning_instance_resource_name_var_name="watson_machine_learning_instance_resource_name"
4242
use_existing_resource_group_var_name="use_existing_resource_group"
4343
create_continuous_delivery_service_instance_var_name="create_continuous_delivery_service_instance"
44+
inventory_repo_url_var_name="inventory_repo_url"
4445

4546
resource_group_name_value=$(terraform output -state=terraform.tfstate -raw resource_group_name)
4647
toolchain_resource_group_value=$(terraform output -state=terraform.tfstate -raw resource_group_name)
@@ -53,6 +54,7 @@ TF_VARS_FILE="terraform.tfvars"
5354
watson_machine_learning_instance_resource_name_value=$(terraform output -state=terraform.tfstate -raw watson_machine_learning_instance_resource_name)
5455
use_existing_resource_group_value=true
5556
create_continuous_delivery_service_instance_value=false
57+
inventory_repo_url_value="https://${REGION}.git.cloud.ibm.com/test-inventory-repo"
5658

5759
echo "Appending required input variable values to ${JSON_FILE}.."
5860

@@ -87,6 +89,8 @@ TF_VARS_FILE="terraform.tfvars"
8789
--arg use_existing_resource_group_value "${use_existing_resource_group_value}" \
8890
--arg create_continuous_delivery_service_instance_var_name "${create_continuous_delivery_service_instance_var_name}" \
8991
--arg create_continuous_delivery_service_instance_value "${create_continuous_delivery_service_instance_value}" \
92+
--arg inventory_repo_url_var_name "${inventory_repo_url_var_name}" \
93+
--arg inventory_repo_url_value "${inventory_repo_url_value}" \
9094
'. + {($prefix_var_name): $prefix_value,
9195
($resource_group_name_var_name): $resource_group_name_value,
9296
($toolchain_region_var_name): $toolchain_region_value,
@@ -101,7 +105,8 @@ TF_VARS_FILE="terraform.tfvars"
101105
($watson_machine_learning_instance_guid_var_name): $watson_machine_learning_instance_guid_value,
102106
($use_existing_resource_group_var_name): $use_existing_resource_group_value,
103107
($create_continuous_delivery_service_instance_var_name): $create_continuous_delivery_service_instance_value,
104-
($watson_machine_learning_instance_resource_name_var_name): $watson_machine_learning_instance_resource_name_value}' "${JSON_FILE}" > tmpfile && mv tmpfile "${JSON_FILE}" || exit 1
108+
($watson_machine_learning_instance_resource_name_var_name): $watson_machine_learning_instance_resource_name_value,
109+
($inventory_repo_url_var_name): $inventory_repo_url_value}' "${JSON_FILE}" > tmpfile && mv tmpfile "${JSON_FILE}" || exit 1
105110

106111
echo "Pre-validation complete successfully"
107112
)

0 commit comments

Comments
 (0)