Adding automation scripts

Use postDeploy and preUndeploy scripts to automate configurations for Composable Commerce and external systems.

Overview

It is common for any integration to require the creation of specific resources during the deployment lifecycle in the Composable Commerce API, such as Types, API Extensions, or Subscriptions.

You can define scripts in connect.yaml that run on different deployment stages, which allows Connect to create and modify those resources.

JavaScript/TypeScript

postDeploy

postDeploy runs after a successful deployment of the Connect application and is useful when you need to set up an API Extension or Subscription required to trigger your Connector endpoint.

postDeploy has access to extra environment variables depending on the application type.

Environment variableApplication typeDescription
CONNECT_PROVIDERAllThe Cloud provider of the Connect Deployment. Should be used when setting up a Subscription in the Composable Commerce API. Supported value is GCP.
CONNECT_SERVICE_URLServiceThe public URL of the Connect application, should be used when setting up an API Extension in the Composable Commerce API.
CONNECT_GCP_PROJECT_IDEventGoogle Cloud Platform (GCP) project ID. Should be used when setting up a Subscription in the Composable Commerce API.
CONNECT_GCP_TOPIC_NAMEEventGCP Pub/Sub topic name. Should be used when setting up a Subscription in the Composable Commerce API .

preUndeploy

preUndeploy runs before the Connector is undeployed and is useful when deleting unused resources from the Composable Commerce API.

Example service application

# connect.yaml
# ...
applicationType: service
scripts:
postDeploy: node post-deploy.js
preUndeploy: node pre-undeploy.js
# ...
// post-deploy.js
async function run() {
try {
const EXTENSION_KEY = 'myconnector-extension-key';
const serviceUrl = process.env.CONNECT_SERVICE_URL;
// ...
const result = await apiRoot
.extensions()
.post({
body: {
key: EXTENSION_KEY,
destination: {
type: 'HTTP',
url: serviceUrl,
},
triggers: [
{
resourceTypeId: 'cart',
actions: ['Update'],
},
],
},
})
.execute();
} catch (error) {
process.stderr.write(`Post-deploy failed: ${error.message}\n`);
process.exitCode = 1;
}
}
run();
// pre-undeploy.js
async function run() {
try {
const EXTENSION_KEY = 'myconnector-extension-key';
const serviceUrl = process.env.CONNECT_SERVICE_URL;
// ...
const result = await apiRoot
.extensions()
.delete({
body: {
key: EXTENSION_KEY,
},
})
.execute();
} catch (error) {
process.stderr.write(`Post-deploy failed: ${error.message}\n`);
process.exitCode = 1;
}
}
run();

Example event application

# connect.yaml
# ...
applicationType: event
scripts:
postDeploy: node post-deploy.js
preUndeploy: node pre-undeploy.js
# ...
// post-deploy.js
async function run() {
try {
const connectProvider = process.env.CONNECT_PROVIDER;
switch (connectProvider) {
case 'AZURE': {
await createAzureServiceBusSubscription();
break;
}
default: {
await createGcpPubSubSubscription();
}
}
} catch (error) {
process.stderr.write(`Post-deploy failed: ${error.message}\n`);
process.exitCode = 1;
}
}
async function createGcpPubSubSubscription() {
const SUBSCRIPTION_KEY = 'myconnector-subscription-key';
const topicName = process.env.CONNECT_GCP_TOPIC_NAME;
const projectId = process.env.CONNECT_GCP_PROJECT_ID;
// ...
const result = await apiRoot
.subscriptions()
.post({
body: {
key: SUBSCRIPTION_KEY,
destination: {
type: 'GoogleCloudPubSub',
topic: topicName,
projectId,
},
messages: [
{
resourceTypeId: 'customer',
types: ['CustomerCreated'],
},
],
},
})
.execute();
}
async function createAzureServiceBusSubscription() {
const SUBSCRIPTION_KEY = 'myconnector-subscription-key';
const connectionString = process.env.CONNECT_AZURE_CONNECTION_STRING;
// ...
const result = await apiRoot
.subscriptions()
.post({
body: {
key: SUBSCRIPTION_KEY,
destination: {
type: 'AzureServiceBus',
connectionString: connectionString,
},
messages: [
{
resourceTypeId: 'customer',
types: ['CustomerCreated'],
},
],
},
})
.execute();
}
run();
// pre-undeploy.js
async function run() {
try {
const SUBSCRIPTION_KEY = 'myconnector-subscription-key';
// ...
const result = await apiRoot
.subscriptions()
.delete({ body: { key: SUBSCRIPTION_KEY } })
.execute();
} catch (error) {
process.stderr.write(`Post-deploy failed: ${error.message}\n`);
process.exitCode = 1;
}
}
run();