Skip to main content

rollback-on-failure

When a PROVISION deployment of an instance with the attribute autorollback=true fails, rolls the instance back to its most recent COMPLETED PROVISION deployment.

The rollback has two steps:

  1. rollbackDeployment proposes a new PROVISION deployment with the bundle version, params, and dependency connections of the completed deployment.
  2. approveDeployment approves the proposal, so it runs.

Roll back a deployment describes what a rollback restores.

The automation skips the event when:

  • the failed deployment is itself a rollback. A failed rollback needs a person, so the automation posts a notice and stops.
  • a deployment of the instance is PENDING, APPROVED, or RUNNING.
  • the instance has no COMPLETED PROVISION deployment.
  • a PROVISION that started after the failed one has completed.

When the environment has separation of duty turned on, the service account cannot approve its own proposal. The proposal stays PROPOSED for a person to approve, and the notice says so.

Notices go to Slack with SLACK_BOT_TOKEN and a slack.channel, or with SLACK_WEBHOOK_URL. Without either, they go to the logs only. With the bot token, the result of each rollback, completed or failed, is a reply in the thread of its notice.

Opt in​

Attribute keys must match ^[A-Za-z_][A-Za-z0-9_]{0,63}$, and a key must be a custom attribute before you set it. Create a component-scope custom attribute with the key autorollback and the values true and false. Set Required to off, so that components without it stay valid. Then set autorollback=true on each component to roll back. The attribute applies to the instances of the component in every environment.

Results in the stash​

On each rollback deployment it creates, the automation writes:

KeyStashLabelValue
md/v1/step/rollbackThe rollback deploymentRollback{"tool": "rollback-on-failure", "action": "apply", "status": "succeeded", "finished_at": "...", "message": "Rolls back failed deployment <id> to deployment <id> (v1.2.3)"}. The message says when the rollback waits for approval.

Entries in a deployment's stash are immutable. When Massdriver sends an event again, the first value stays.

A failed write is logged at debug and never fails the event, so Massdriver does not send it again.

Settings​

Every key is optional. An unknown key stops the automation at start. The bundle's Settings params have the keys of the config file. The full commented file is config.example.yaml.

KeyDefault
attribute.keyautorollbackThe attribute key that opts an instance in. It must be a component custom attribute that allows the value.
attribute.valuetrueThe attribute value that opts an instance in.
slack.channelThe channel for notices, with SLACK_BOT_TOKEN. Empty posts nothing. Ignored with only SLACK_WEBHOOK_URL.
massdriver.app_urlhttps://app.massdriver.cloudThe Massdriver UI, for the links in notices.
results.prefixmd/v1The start of the result keys.

Secrets​

Variable
SLACK_BOT_TOKENThe xoxb- token of the Slack app. Posts to the channel in the settings.
SLACK_WEBHOOK_URLAn incoming webhook, when there is no Slack app. Posts every message to its one channel. Ignored when the bot token is set.

MASSDRIVER_SIGNING_SECRET, MASSDRIVER_ORGANIZATION_ID, and MASSDRIVER_API_KEY are required. They and the other shared variables are in the common settings.

Permissions​

The service account needs these permissions on the opted-in instances:

  • instance:propose, for rollbackDeployment
  • instance:deploy, for approveDeployment
  • project:view on their projects, to read the deployment history
  • stash:write, for the results

Deployed with the bundle, it also needs organization:manageEventDestinations.

Host​

The bundle serves the automation at Ingress > Host. /hooks/massdriver must be public. See Deploy for the platform details.

Event destination​

The bundle creates a destination for deployment.failed and deployment.completed, with a condition built from attribute.key and attribute.value. The condition drops PLAN and DECOMMISSION deployments, instances that did not opt in, and completed deployments that are not rollbacks, so Massdriver marks them SKIPPED and does not send them. A completed rollback becomes the reply in its notice's thread. Event attributes sets the destination's attribute filter, such as md-environment = prod to act on one environment only. Empty sends events of any attributes.

To create it yourself, use mass 2.6.0 or later:

shell
mass event destination create \
--name rollback-on-failure \
--url https://rollback-on-failure.example.com/hooks/massdriver \
--event-type deployment.failed \
--event-type deployment.completed \
--condition '.data.metadata.deployment_action == "PROVISION" and .data.attributes["autorollback"] == "true" and (.type == "deployment.failed" or ((.data.entity.message // "") | startswith("Rollback to deployment ")))'

If you change attribute.key or attribute.value, change the condition to match. To act on one environment only, add --attribute md-environment=prod.

Copy the signing secret from the destination's Overview tab into MASSDRIVER_SIGNING_SECRET.

Run​

shell
docker run -p 8080:8080 \
-v "$PWD/config.yaml:/etc/massdriver-automations/config.yaml:ro" \
-e MASSDRIVER_ORGANIZATION_ID=acme \
-e MASSDRIVER_API_KEY=md_... \
-e MASSDRIVER_SIGNING_SECRET=whsec_... \
-e SLACK_BOT_TOKEN=xoxb-... \
ghcr.io/massdriver-cloud/massdriver-automations:0.3 run rollback-on-failure

Limits​

  • Duplicate events are dropped by ID for one hour, in memory. Run one replica.
  • The rollback restores the bundle version, params, and dependency connections of the completed deployment. It does not restore data, or changes made outside Massdriver.
  • The approval pins the instance to the bundle version of the completed deployment and saves its params on the instance. To move forward again, set the instance's version and params.
  • When the API call fails, the automation responds 500 and Massdriver retries the event. A retry approves the proposal from the earlier attempt instead of proposing a second one.