Skip to content

Commit

Permalink
feat: add webhook trigger for CI pipeline, add git trigger for invent… (
Browse files Browse the repository at this point in the history
#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 <[email protected]>
  • Loading branch information
brendankellyibm and vburckhardt authored Apr 18, 2024
1 parent f637364 commit 37082c3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion common-dev-assets
1 change: 1 addition & 0 deletions cra-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ CRA_TARGETS:
TF_VAR_watson_machine_learning_instance_crn: "crn:v1:bluemix:public:pm-20:us-south:a/2e92ee435f984c5dbf970577b2ceec1f:6966b4da-de4c-4dfb-a1d2-8a635f7eeeae::"
TF_VAR_watson_machine_learning_instance_guid: "4e621b22-5802-4013-896a-777fc345f424"
TF_VAR_watson_machine_learning_instance_resource_name: "test-machine-learning-instance"
TF_VAR_inventory_repo_url: "https://us-south.git.cloud.ibm.com/yada.yada/04181-inventory-repo.git"
58 changes: 58 additions & 0 deletions solutions/banking/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,61 @@ resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_integration_id_pip
type = "text"
value = data.external.assistant_get_integration_id.result.assistant_integration_id
}

# Random string for webhook token
resource "random_string" "webhook_secret" {
length = 48
special = false
upper = false
}

# Create webhook for CI pipeline
resource "ibm_cd_tekton_pipeline_trigger" "ci_pipeline_webhook" {
depends_on = [random_string.webhook_secret]
type = "generic"
pipeline_id = var.ci_pipeline_id
name = "rag-webhook-trigger"
event_listener = "ci-listener-gitlab"
secret {
type = "token_matches"
source = "payload"
key_name = "webhook-token"
value = random_string.webhook_secret.result
}
}

# Create git trigger for CD pipeline - to run inventory promotion once CI pipeline is complete
resource "ibm_cd_tekton_pipeline_trigger" "cd_pipeline_inventory_promotion_trigger" {
count = var.inventory_repo_url != null ? 1 : 0
type = "scm"
pipeline_id = var.cd_pipeline_id
name = "git-inventory-promotion-trigger"
event_listener = "promotion-listener"
events = ["push"]
source {
type = "git"
properties {
url = var.inventory_repo_url
branch = "master"
}
}
}

# Trigger webhook to start CI pipeline run
resource "null_resource" "ci_pipeline_run" {
depends_on = [
ibm_cd_tekton_pipeline_trigger.ci_pipeline_webhook,
ibm_cd_tekton_pipeline_property.watsonx_assistant_integration_id_pipeline_property_ci,
ibm_cd_tekton_pipeline_property.watsonx_assistant_id_pipeline_property_ci,
ibm_resource_instance.cd_instance
]
triggers = {
always_run = timestamp()
}

provisioner "local-exec" {
command = "${path.module}/watson-scripts/webhook-trigger.sh \"${ibm_cd_tekton_pipeline_trigger.ci_pipeline_webhook.webhook_url}\" \"${random_string.webhook_secret.result}\""
interpreter = ["/bin/bash", "-c"]
quiet = true
}
}
6 changes: 6 additions & 0 deletions solutions/banking/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ variable "cd_pipeline_id" {
type = string
}

variable "inventory_repo_url" {
description = "URL of the inventory repository"
type = string
default = null
}

variable "watson_assistant_instance_id" {
description = "ID of the WatsonX Assistant service instance"
type = string
Expand Down
4 changes: 4 additions & 0 deletions solutions/banking/version.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ terraform {
source = "hashicorp/null"
version = ">= 3.2.1"
}
random = {
source = "hashicorp/random"
version = ">= 3.6.1"
}
restapi = {
source = "Mastercard/restapi"
version = ">= 1.19.1"
Expand Down
8 changes: 8 additions & 0 deletions solutions/banking/watson-scripts/webhook-trigger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -e

WEBHOOK_URL="$1"
WEBHOOK_SECRET="$2"

curl -X POST --header "Content-Type: application/json" --data "{\"webhook-token\":\"$WEBHOOK_SECRET\"}" "$WEBHOOK_URL"
7 changes: 6 additions & 1 deletion tests/scripts/pre-validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ TF_VARS_FILE="terraform.tfvars"
watson_machine_learning_instance_resource_name_var_name="watson_machine_learning_instance_resource_name"
use_existing_resource_group_var_name="use_existing_resource_group"
create_continuous_delivery_service_instance_var_name="create_continuous_delivery_service_instance"
inventory_repo_url_var_name="inventory_repo_url"

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

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

Expand Down Expand Up @@ -87,6 +89,8 @@ TF_VARS_FILE="terraform.tfvars"
--arg use_existing_resource_group_value "${use_existing_resource_group_value}" \
--arg create_continuous_delivery_service_instance_var_name "${create_continuous_delivery_service_instance_var_name}" \
--arg create_continuous_delivery_service_instance_value "${create_continuous_delivery_service_instance_value}" \
--arg inventory_repo_url_var_name "${inventory_repo_url_var_name}" \
--arg inventory_repo_url_value "${inventory_repo_url_value}" \
'. + {($prefix_var_name): $prefix_value,
($resource_group_name_var_name): $resource_group_name_value,
($toolchain_region_var_name): $toolchain_region_value,
Expand All @@ -101,7 +105,8 @@ TF_VARS_FILE="terraform.tfvars"
($watson_machine_learning_instance_guid_var_name): $watson_machine_learning_instance_guid_value,
($use_existing_resource_group_var_name): $use_existing_resource_group_value,
($create_continuous_delivery_service_instance_var_name): $create_continuous_delivery_service_instance_value,
($watson_machine_learning_instance_resource_name_var_name): $watson_machine_learning_instance_resource_name_value}' "${JSON_FILE}" > tmpfile && mv tmpfile "${JSON_FILE}" || exit 1
($watson_machine_learning_instance_resource_name_var_name): $watson_machine_learning_instance_resource_name_value,
($inventory_repo_url_var_name): $inventory_repo_url_value}' "${JSON_FILE}" > tmpfile && mv tmpfile "${JSON_FILE}" || exit 1

echo "Pre-validation complete successfully"
)

0 comments on commit 37082c3

Please sign in to comment.