Automate your code updates
Back to home
On this page
Upsun allows you to update your dependencies through source operations.
Before you start
You need:
1. Define a source operation to update your dependencies
To facilitate updating your dependencies in your project,
define a source operation in your .upsun/config.yaml
file
depending on your dependency manager:
applications:
# The name of the app container. Must be unique within a project.
myapp:
# The location of the application's code.
source:
root: "myapp"
source:
operations:
update:
command: |
set -e
composer update
git add composer.lock
git add -A
git diff-index --quiet HEAD || git commit --allow-empty -m "Update Composer dependencies"
applications:
# The name of the app container. Must be unique within a project.
myapp:
# The location of the application's code.
source:
root: "myapp"
source:
operations:
update:
command: |
set -e
npm update
git add package.json package-lock.json
git add -A
git diff-index --quiet HEAD || git commit --allow-empty -m "Update npm dependencies"
applications:
# The name of the app container. Must be unique within a project.
myapp:
# The location of the application's code.
source:
root: "myapp"
source:
operations:
update:
command: |
set -e
yarn upgrade
git add yarn.lock
git add -A
git diff-index --quiet HEAD || git commit --allow-empty -m "Update yarn dependencies"
applications:
# The name of the app container. Must be unique within a project.
myapp:
# The location of the application's code.
source:
root: "myapp"
source:
operations:
update:
command: |
set -e
go get -u
go mod tidy
git add go.mod go.sum
git add -A
git diff-index --quiet HEAD || git commit --allow-empty -m "Update Go dependencies"
applications:
# The name of the app container. Must be unique within a project.
myapp:
# The location of the application's code.
source:
root: "myapp"
source:
operations:
update:
command: |
set -e
pipenv update
git add Pipfile Pipfile.lock
git add -A
git diff-index --quiet HEAD || git commit --allow-empty -m "Update Python dependencies"
applications:
# The name of the app container. Must be unique within a project.
myapp:
# The location of the application's code.
source:
root: "myapp"
source:
operations:
update:
command: |
set -e
bundle update --all
git add Gemfile Gemfile.lock
git add -A
git diff-index --quiet HEAD || git commit --allow-empty -m "Update Ruby dependencies"
2. Automate your dependency updates with a cron job
After you’ve defined a source operation to update your dependencies on your project, you can automate it using a cron job.
Note that it’s best not to run source operations on your production environment, but rather on a dedicated environment where you can test changes.
Make sure you have the Upsun CLI installed and an API token so you can run a cron job in your app container.
- Set your API token as a top-level environment variable:
Run the following command:
upsun variable:create --environment main --level environment --prefix 'env' --name UPSUN_CLI_TOKEN --sensitive true --value 'YOUR_UPSUN_CLI_TOKEN' --inheritable false --visible-build true --json false --enabled true --visible-runtime true
- Open the environment where you want to add the variable.
- Click Settings.
- Click Variables.
- Click + Add variable.
- In the Variable name field, enter
env:UPSUN_CLI_TOKEN
. - In the Value field, enter your API token.
- Make sure the Available at runtime and Sensitive variable options are selected.
- Click Add variable.
Note
Once you add the API token as an environment variable, anyone with SSH access can read its value. Make sure you carefully check your user access on this project.
- Add a build hook to your app configuration to install the CLI as part of the build process:
applications:
myapp:
hooks:
build: |
set -e
echo "Installing Upsun CLI"
curl -fsSL https://raw.githubusercontent.com/platformsh/cli/main/installer.sh | bash
echo "Testing Upsun CLI"
upsun
- Then, to configure a cron job to automatically update your dependencies once a day, use a configuration similar to the following:
applications:
myapp:
# ...
crons:
update:
# Run the code below every day at midnight.
spec: '0 0 * * *'
commands:
start: |
set -e
upsun sync -e development code data --no-wait --yes
upsun source-operation:run update --no-wait --yes
The example above synchronizes the development
environment with its parent
and then runs the update
source operation defined previously.
3. Configure notifications about dependency updates
To get notified every time a source operation is triggered and therefore every time a dependency is updated, you can configure activity scripts or webhooks.
Notifications through an activity script
After you’ve defined a source operation to update your dependencies on your project, you can configure an activity script to receive notifications every time a dependency update is triggered.
Example
You want to get notified of every dependency update through a message posted on a Slack channel. To do so, follow these steps:
-
In your Slack administrative interface, create a new Slack webhook. You get a URL starting with
https://hooks.slack.com/
. -
Replace
SLACK_URL
in the following.js
script with your webhook URL. -
Add the following code to a
.js
file:/** * Sends a color-coded formatted message to Slack. * * To control what events trigger it, use the --events switch in * the Upsun CLI. * * Replace SLACK_URL in the following script with your Slack webhook URL. * Get one here: https://api.slack.com/messaging/webhooks * You should get something like: const url = 'https://hooks.slack.com/...'; * * activity.text: a brief, one-line statement of what happened. * activity.log: the complete build and deploy log output, as it would be seen in the Console log screen. */ function sendSlackMessage(title, message) { const url = 'SLACK_URL'; const messageTitle = title; const color = activity.result === "success" ? "#66c000" : "#ff0000"; const body = { attachments: [ { title: messageTitle, text: message, color: color, }, ], }; const resp = fetch(url, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(body), }); if (!resp.ok) { console.log("Sending slack message failed: " + resp.body.text()); } } sendSlackMessage(activity.text, activity.log);
-
Run the following Upsun CLI command:
upsun integration:add --type script --file ./my_script.js --events=environment.source-operation
Optional: to only get notifications about specific environments, add the following flag to the command:
--environments=your_environment_name
.
Anytime a dependency is updated via a source operation, the activity script now reports it to Slack.
Notifications through a webhook
After you’ve defined a source operation to update your dependencies on your project, you can configure a webhook to receive notifications every time a dependency update is triggered.
Webhooks allow you to host a script yourself externally. This script receives the same payload as an activity script and responds to the same events, but can be hosted on your own server and in your own language.
To configure the integration between your webhook and your source operation, run the following Upsun CLI command:
upsun integration:add --type=webhook --url=URL_TO_RECEIVE_JSON --events=environment.source-operation
Optional: to only get notifications about specific environments,
add the following flag to the command: --environments=your_environment_name
.
To test the integration and the JSON response,
you can generate a URL from a service such as webhook.site
and use the generated URL as URL_TO_RECEIVE_JSON
.
This URL then receives the JSON response when a source operation is triggered.
Anytime a dependency is updated via a source operation, the webhook now receives a POST message. This POST message contains complete information about the entire state of the project at that time.