Skip to main content

CI/CD integration

GitBook can work with any CI/CD pipeline you already have for managing your OpenAPI specification. By using the GitBook CLI, you can automate updates to your API reference.

Upload a specification file

If your OpenAPI spec is generated during your CI process, you can upload it directly from your build environment:
Parameters:
  • --spec - The name of your specification in GitBook
  • --organization - Your GitBook organization ID
  • Final argument - Path to your OpenAPI spec file (YAML or JSON)
The spec name should match an existing specification in GitBook, or a new one will be created automatically.

Set a new source URL or trigger a refresh

If your OpenAPI specification is hosted at a URL, GitBook automatically checks for updates. To force an update (for example, after a release), run:
This command works the same way whether you’re setting a URL for the first time or refreshing an existing URL source.

Update your spec with GitHub Actions

If you’re setting up a workflow to publish your OpenAPI spec, complete these steps in your repository:
1

Add secrets and variables

In your repository, go to Settings → Secrets and variables → Actions.Add a secret:
  • GITBOOK_TOKEN - Your GitBook API token
Add variables (or hardcode them in the workflow):
  • GITBOOK_SPEC_NAME - Your spec’s name in GitBook
  • GITBOOK_ORGANIZATION_ID - Your GitBook organization ID
2

Create the workflow file

Save the workflow file as .github/workflows/gitbook-openapi-publish.yml in your repository.
3

Push changes

Commit and push changes to your main branch (or run the workflow manually).

GitHub Actions workflow

.github/workflows/gitbook-openapi-publish.yml
Replace <path_to_spec> with the actual path to your OpenAPI file, such as ./openapi.yaml or ./docs/api/openapi.json.

Advanced CI/CD patterns

Publish different specs for different branches

.github/workflows/gitbook-openapi-publish.yml

Generate OpenAPI spec before publishing

.github/workflows/gitbook-openapi-publish.yml

Validate spec before publishing

.github/workflows/gitbook-openapi-publish.yml

Other CI/CD platforms

GitLab CI

.gitlab-ci.yml

CircleCI

.circleci/config.yml

Jenkins

Jenkinsfile

Best practices

1

Validate before publishing

Always validate your OpenAPI spec before publishing to catch errors early in your CI pipeline.
2

Use environment-specific specs

Maintain separate specifications for production, staging, and development environments.
3

Secure your API token

Never commit your GitBook API token to version control. Always use CI/CD secrets or environment variables.
4

Trigger on spec changes

Configure your CI to run only when OpenAPI files change to avoid unnecessary builds.
5

Add notifications

Set up notifications (Slack, email, etc.) to alert your team when API documentation is updated.
For more information on obtaining a GitBook API token, visit your GitBook organization settings under Integrations → API tokens.