DEV Community

Cover image for Breaking change alerts from Azure DevOps, Bitbucket Pipelines and Jenkins
Graphwake
Graphwake

Posted on Edited on Originally published at graphwake.com AI-assisted

Breaking change alerts from Azure DevOps, Bitbucket Pipelines and Jenkins

GitHub Actions and GitLab CI get a ready-made action and template from us. Azure DevOps, Bitbucket Pipelines and Jenkins get a snippet you paste into the pipeline, so here are those three, copy-paste ready.

We build Graphwake, which tells the teams that consume an API when a change in its OpenAPI spec is about to break them. Here's how to send it your spec from these three CI systems.

The idea: add one step to the pipeline that builds your API. It runs our open-source CI agent, which sends the service's OpenAPI spec to Graphwake with the build's context. On a pull request build the spec is stored as a draft, so the teams subscribed to a changed endpoint can hear about a breaking change before the merge. On a build of the live branch it becomes the live spec.

You need three things: the service's ingest key stored as a CI secret named GRAPHWAKE_KEY, the snippet for your platform, and the right live branch.

Before you start

You need a service in Graphwake that takes its spec from CI. In Graphwake, add a service and choose CI pipeline under "Where the spec comes from". Graphwake shows the service's ingest key (dsk_...) only once, so copy it straight away.

You also need a build step that writes the spec. The agent sends the spec as it is, YAML or JSON. It doesn't generate it. The snippets below expect openapi.json in the working directory. Recipes for ASP.NET Core, Spring Boot, FastAPI, NestJS and Go are in the agent README.

Keys are per service. A repository with several services runs the step once per service, each with its own spec and its own key.

If you're on GitHub Actions or GitLab CI, the same agent has a ready-made action and template, described in inside the Graphwake CI agent.

Step 1: store the ingest key as a secret

Every snippet reads the key from the GRAPHWAKE_KEY environment variable. Keep it in your CI's secret store and pass it as that variable. The agent also accepts --key, but command-line arguments show up in process lists, so use the variable.

  • Azure DevOps: add a pipeline variable named GRAPHWAKE_KEY and mark it secret. Azure doesn't hand secret variables to scripts on its own, which is why the snippet maps it in env.
  • Bitbucket Pipelines: add a secured repository variable named GRAPHWAKE_KEY.
  • Jenkins: add a credential of kind Secret text with the ID graphwake-key. The snippet binds it to GRAPHWAKE_KEY with withCredentials.

The agent hides the key in everything it prints. If the key leaks or is lost, rotate it from the service's Source tab in Graphwake and update the secret. The old key keeps working for 24 hours, which gives you time to update every pipeline.

Step 2: add the step

Azure DevOps

Add this script step to the pipeline's steps, after the step that writes openapi.json. It downloads the agent release, checks it against checksums.txt and runs it. This is azure-pipelines.yml, from the agent README:

  - bash: |
      VERSION=0.3.0
      BASE=https://github.com/graphwake/agent/releases/download/v$VERSION
      ARCHIVE=graphwake_${VERSION}_linux_amd64.tar.gz
      curl -fsSLO "$BASE/$ARCHIVE" && curl -fsSLO "$BASE/checksums.txt" &&
        sha256sum --check --ignore-missing checksums.txt &&
        tar -xzf "$ARCHIVE" graphwake &&
        ./graphwake upload --spec openapi.json || echo "graphwake: agent not run, spec not sent"
    displayName: Upload spec to Graphwake
    continueOnError: true
    env:
      GRAPHWAKE_KEY: $(GRAPHWAKE_KEY)
Enter fullscreen mode Exit fullscreen mode

Run it on pull requests and on main. The agent treats a build with BUILD_REASON=PullRequest as a pull request build. For Azure Repos, pull request builds come from a build validation branch policy on the target branch; for a GitHub repository, from the pipeline's pr trigger.

If your organisation has the Graphwake extension installed, the GraphwakeUpload@0 task does the same with two inputs, spec: openapi.json and key: $(GRAPHWAKE_KEY).

One Azure quirk: it doesn't report a pull request closed without merging. Drafts of a pull request that never merges close on their own after 14 days.

Bitbucket Pipelines

Define the step once with an anchor and run it for every pull request and for builds of main. Replace the first script line with your own spec build, or put the step after the one that produces openapi.json. The step's image needs curl, sha256sum and tar. This is bitbucket-pipelines.yml, from the agent README:

definitions:
  steps:
    - step: &graphwake
        name: API spec
        script:
          - ./build-openapi.sh   # produce openapi.json
          - >-
            VERSION=0.3.0; BASE=https://github.com/graphwake/agent/releases/download/v$VERSION;
            ARCHIVE=graphwake_${VERSION}_linux_amd64.tar.gz;
            (curl -fsSLO "$BASE/$ARCHIVE" && curl -fsSLO "$BASE/checksums.txt" &&
            sha256sum --check --ignore-missing checksums.txt && tar -xzf "$ARCHIVE" graphwake &&
            ./graphwake upload --spec openapi.json) || echo "graphwake: agent not run, spec not sent"
pipelines:
  pull-requests:
    '**':
      - step: *graphwake
  branches:
    main:
      - step: *graphwake
Enter fullscreen mode Exit fullscreen mode

The agent reads BITBUCKET_PR_ID on pull request builds and BITBUCKET_BRANCH on branch builds. If your live branch isn't main or master, rename the branches entry and set the live branch (step 3).

Jenkins

Use a multibranch pipeline, so Jenkins sets CHANGE_ID, CHANGE_URL and CHANGE_AUTHOR on pull request builds and BRANCH_NAME on branch builds. That's how the agent tells them apart, so a plain pipeline job won't do. Add this stage to the Jenkinsfile after the stage that writes openapi.json. The agent runs from its image, so the Jenkins agent needs Docker. The -e flags pass the build's variables into the container. From the agent README:

stage('API spec') {
  steps {
    withCredentials([string(credentialsId: 'graphwake-key', variable: 'GRAPHWAKE_KEY')]) {
      sh 'docker run --rm -e GRAPHWAKE_KEY -e BRANCH_NAME -e CHANGE_ID -e CHANGE_URL -e CHANGE_AUTHOR -e GIT_COMMIT -e JENKINS_URL -v "$PWD:/w" -w /w ghcr.io/graphwake/agent:0.3.0 upload --spec openapi.json || true'
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The image has no git. On Jenkins that means the agent can't check that the build is still the branch tip, and it can't find the merged pull request, so a merge shows up in Graphwake as a separate live change. To get both, download the binary in an sh step instead, with the checksum-checked script from the README's Install section; it then uses the git in your workspace. The same script works if the Jenkins agent has no Docker. If you keep the image, pin it by digest (ghcr.io/graphwake/agent@sha256:...).

Step 3: set the live branch

A build of a live branch publishes the live spec, a pull request build stores a draft, and any other build sends nothing. None of these three platforms tells the agent its default branch, so the agent treats main and master as live unless you configure something else.

Flow diagram of what the step does with each build: a pull request build stores a draft spec and consumer teams hear before the merge; a live-branch build sends the live spec and notifications go out on merge; anything else sends nothing.

Only pull request builds and live-branch builds send anything.

Platform Pull request build Live build Set the live branch with
Azure DevOps BUILD_REASON=PullRequest Other builds of a live branch A pipeline variable GRAPHWAKE_LIVE_BRANCH, or the task's liveBranch input
Bitbucket Pipelines BITBUCKET_PR_ID BITBUCKET_BRANCH is a live branch A repository variable GRAPHWAKE_LIVE_BRANCH
Jenkins (multibranch) CHANGE_ID BRANCH_NAME (or GIT_BRANCH) is a live branch GRAPHWAKE_LIVE_BRANCH in the stage's environment, passed into the container with -e GRAPHWAKE_LIVE_BRANCH

The value is a comma-separated list and globs are allowed: main,release/* publishes from main and from every release/ branch. Before a live send the agent checks that the build is still the tip of its branch, where it can, so an old re-run doesn't replace a newer live spec.

Step 4: confirm it worked

Run the pipeline on a pull request that touches the API, then on main. The step prints one line, which tells you what happened:

Line in the CI log Meaning
graphwake: spec sent (pull request #42) Draft stored for the pull request.
graphwake: spec sent (pull request #42 merged, live) The spec is live and the pull request's notifications are settled.
graphwake: spec sent (live) The spec is live.
graphwake: not a pull request or live-branch build, nothing sent A feature branch or a tag. On main, check the live branch setting.
invalid or missing ingest key Check that GRAPHWAKE_KEY reaches the step. A rotated key stops working after 24 hours.
this service takes its spec from a URL, not from CI The service is set up to poll a URL. Use a service whose source is CI pipeline.
spec rejected: ... The file isn't valid OpenAPI, or the service no longer exists.

Then open the service in Graphwake. Its Source tab shows the CI platform the agent detected and the time of the last upload.

To see an alert end to end, turn on Alerts on open pull requests under Settings → Alerts, subscribe a test team to an endpoint, and open a pull request that removes a response field from it. The team hears about it in its Slack, Microsoft Teams or email channel, and the impact score shows up on the change in Graphwake.

How Graphwake routes the alert

Example Graphwake alert in Slack for pull request #1284 on orders-api: a breaking change to GET /orders/{id}, owned by payments-team, where total was removed and currency is now required. The alert reaches the three subscribed teams (checkout, mobile-app, reporting); search is not subscribed and stays quiet.

Example: what a consumer team sees once the step is in place.

Once the step is in place, Graphwake works the same as on GitHub or GitLab: only the teams subscribed to a changed endpoint are told, nothing goes to a company-wide channel, and the step sends the OpenAPI file and the build's context. Your source code is not sent. Each change set gets an impact score with public rules.


Originally published on graphwake.com.

Which CI does your API build on? If it's not GitHub Actions, what's the integration you miss most?


About Graphwake. We build Graphwake, a dependency map for your APIs. It watches your OpenAPI specs, knows which teams consume each endpoint, and tells only those teams in Slack, Microsoft Teams or email when a change is about to break them. Start a 14-day free trial at graphwake.com

Top comments (0)