Installing
Activity scripts are configured as integrations. That means they’re at the project level, not at the level of an individual environment. While you can store the scripts in your Git repository for access, they have no effect there. To install a new activity script, useintegration:add command from the Upsun CLI.
my_script.js file as an activity script on the current project.
Don’t run the integration:add command a second time,
or it installs a second integration with the same code.
Updating
To update an existing activity script, follow these steps:-
Get the activity script’s ID by running the following command:
This returns something like the following:
-
Update the integration by running the following command:
This
integration:updatecommand updates the integration in place, permanently overwriting the previous version. -
Test the activity script update by triggering a redeployment with the following command:
Removing
To disable an activity script, follow these steps:-
Get the activity script’s ID by running the following command:
This returns something like the following:
-
Delete the integration by running the following command:
Debugging
Get activity logs by running the following command:console.log is available in the activity log, which is the recommended way to debug scripts.
See the activity log documentation for further details.
To get a more readable output of a variable you’re trying to debug, you can make JSON.stringify use human-friendly formatting.
Configuring scripts
There are many types of activity to which a script could respond. By default, it will activate only after a successfulgit push operation.
That trigger is configurable via command line switches when adding or updating a script.
For example, to have a script trigger any time an environment is activated or deactivated, run:
--events=event1,event2,... option.
Scripts can also trigger only when an action reaches a given state, such as pending, in_progress, complete, cancelled, or scheduled.
The default is only when they reach complete.
To have a script execute when a synchronize action first starts, for example, you would run:
production environment:
Activity script variables
Some activities don’t have access to project and environment variables. In this case, to avoid hardcoding sensitive variables (such as API tokens) and therefore prevent security breaches, add a variable to your activity script. You can add activity script variables through the Upsun CLI. Activity script variables are only visible in the activity script itself, inside thevariables variable.
Add an activity script variable
To add a variable to your activity script at the integration level, use the followingPOST request:
Delete or patch an activity script variable
To delete an activity script variable, use the followingDELETE request:
PATCH method instead of the DELETE one.
List an activity script variable
To list all your activity script variables at the integration level, use the followingGET request:
Available APIs
Activity scripts can be written in ES2021 and don’t support installing additional packages. There are a series of utility functions you can reuse as well as the following libraries, APIs, and global variables to facilitate building out custom functionality.underscore.js
Underscore.js is available out-of-the-box to make writing Activity scripts more pleasant.
See Underscore’s documentation for available functions and utilities.
activity
Every activity script has a global variable activity that contains detailed information about the activity,
including embedded, JSON-ified versions of the routes configuration and relevant .upsun/config.yaml files.
The activity variable is the same as the webhook payload.
See the documentation there for details and a complete example.
Several of the utility functions below work by pulling out common portions of the activity object.
project
The project global variable includes information about the project subscription itself.
That includes its ID and name, how many users are associated with the project, its SSH public key, and various other values.
An example of this object is below:
Storage API
Activity scripts have access to a limited key/value storage API to persist values from one execution to another. The API is similar to the JavaScriptLocalStorage API.
Fetch API
Activity scripts support a modified version of the browser “Fetch API” for issuing HTTP requests. Unlike the typical browser version, however, they only support synchronous requests. That means the return value offetch() is a Response, not a Promise for one.
The returned Response is also a bit different: only the ok, status and statusText properties
as well as the text and json methods are available.
Note that because of the synchronous nature of our fetch implementation,
the Response.text and Response.json methods are also synchronous,
so they directly return a string and an object, respectively.
The API is otherwise essentially the same as that in the MDN Web Docs.
For instance, this example sends a GET request every time it executes:
fetch() options, see the MDN Web Docs.
Cryptographic API
A minimalist cryptographic API is also available to activity scripts. Its main use is for signing requests to 3rd party APIs. Thecrypto.createHmac() function allows you to create a secure HMAC hash and digest.
- The available hashing functions are
'sha256','sha1'and'md5'as hashing functions. - The available digest formats are
'base64','hex'or''(empty). An empty digest will yield a byte string.
Example taken from the AWS documentation for signing API requests.