Skip to main content

github-deployments

Shows Massdriver deployments as GitHub Deployments in the repository that holds the code of an instance.

The repository comes from the github_repo attribute, in the form owner/name. Massdriver attributes are set on projects, environments, and components, and every instance below them gets them. Declare github_repo as a custom attribute at the component scope, with one value for each repository, and set it on each component.

For each Massdriver deployment, the automation creates one GitHub deployment and adds a status at each step:

Massdriver eventGitHub deployment status
deployment.startedin_progress
deployment.completed (PROVISION)success
deployment.completed (DECOMMISSION)inactive
deployment.failedfailure
deployment.abortederror

PLAN deployments are skipped.

  • The GitHub environment is the Massdriver environment, such as prod. Set github.environment_name to project-environment to use api-prod instead.
  • The ref is the value of the github_ref attribute. Without it, the ref is the default branch of the repository.
  • The GitHub deployment has the task massdriver and a payload with massdriver_deployment_id, instance, bundle, and action.
  • Each status links to the deployment in the instance's history in Massdriver.
  • GitHub does not merge the default branch into the ref and does not wait for commit checks, because the deployment already ran.

Results in the stash​

With MASSDRIVER_API_KEY set, the automation writes the commit that GitHub resolved the ref to, for each Massdriver deployment:

KeyStashLabelValue
md/v1/sourceThe deploymentSource{"repo": "acme/api", "ref": "main", "commit": "a41c9e2...", "url": "https://github.com/acme/api/commit/a41c9e2..."}. url is left out when github.api_url is neither github.com nor a GitHub Enterprise Server /api/v3 URL.

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.

Without MASSDRIVER_API_KEY, the automation does not call the Massdriver API.

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, except github.token_env: the bundle always passes the token as GITHUB_TOKEN. The full commented file is config.example.yaml.

KeyDefault
github.api_urlhttps://api.github.comFor GitHub Enterprise Server, https://<host>/api/v3.
github.token_envGITHUB_TOKENThe variable that holds the GitHub token.
github.repo_attributegithub_repoThe instance attribute that names the repository, as owner/name.
github.ref_attributegithub_refThe instance attribute that names the ref.
github.environment_nameenvironmentenvironment (such as prod) or project-environment (such as api-prod).
massdriver.app_urlhttps://app.massdriver.cloudThe Massdriver UI, for the status links.
results.prefixmd/v1The start of the result keys.

Secrets​

Variable
GITHUB_TOKENRequired, or the variable that github.token_env names. A GitHub token with the Deployments: write and Metadata: read repository permissions (fine-grained), or the repo_deployment scope (classic).

MASSDRIVER_SIGNING_SECRET is required. MASSDRIVER_API_KEY turns on the stash results and needs MASSDRIVER_ORGANIZATION_ID. These and the other shared variables are in the common settings.

Permissions​

Without MASSDRIVER_API_KEY, none. With it, the service account needs stash:write on the instances. The bundle always sets MASSDRIVER_API_KEY, so deployed with the bundle, the service account needs stash:write and 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.started, deployment.completed, deployment.failed, and deployment.aborted, with a condition that drops PLAN deployments and instances without the repository attribute. The condition uses github.repo_attribute.

To create it yourself:

shell
mass event destination create \
--name github-deployments \
--url https://github-deployments.example.com/hooks/massdriver \
--event-type deployment.started \
--event-type deployment.completed \
--event-type deployment.failed \
--event-type deployment.aborted \
--condition '.data.metadata.deployment_action != "PLAN" and (.data.attributes.github_repo // "") != ""'

If you change github.repo_attribute, change github_repo in the condition too.

Copy the signing secret from the destination into MASSDRIVER_SIGNING_SECRET.

Run​

shell
docker run -p 8080:8080 \
-v "$PWD/config.yaml:/etc/massdriver-automations/config.yaml:ro" \
-e MASSDRIVER_SIGNING_SECRET=whsec_... \
-e GITHUB_TOKEN=github_pat_... \
ghcr.io/massdriver-cloud/massdriver-automations:0.3 run github-deployments

Limits​

  • The automation handles one event at a time, so a started and a completed event that arrive together make one GitHub deployment.
  • It remembers the GitHub deployment of each Massdriver deployment in memory. After a restart, it finds the deployment in the 100 newest massdriver deployments of the environment. Run one replica.
  • A started event that arrives after the deployment ended adds no status.
  • A GitHub error returns 500, so Massdriver sends the event again.
  • Custom attribute keys allow only letters, digits, and underscores, so the keys use underscores. A custom attribute has a fixed list of values. Add each repository and ref to that list.
  • GitHub marks older successful deployments in the same environment as inactive. Two components that use one repository and one environment replace each other's active deployment. Use separate repositories, or set github.environment_name: project-environment when the components are in different projects.
  • Authentication is by token. GitHub App authentication is not supported.