Skip to content

Commit 83fb540

Browse files
docs: use combined build action in examples (#206)
* docs: use combined build action in examples * update example readme * Apply suggestions from code review Co-authored-by: Justin Michael <justin@stainless.com> --------- Co-authored-by: Justin Michael <justin@stainless.com>
1 parent ac924a5 commit 83fb540

20 files changed

Lines changed: 243 additions & 529 deletions

README.md

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ workflow run that can be validated by Stainless.
2525
**API keys:** Generate an API key from your Stainless organization dashboard and add it as a `STAINLESS_API_KEY` secret. This works well for getting started or when you don't have admin permissions to install the GitHub App. See [pull_request_api_key.yml](./examples/pull_request_api_key.yml) for the workflow setup.
2626

2727
> [!NOTE]
28-
> **GitLab CI:** OIDC isn't yet supported. Use the API key method and set the `STAINLESS_API_KEY` environment variable. See the template files in `build/gitlab-ci.yml`, `merge/gitlab-ci.yml`, and `preview/gitlab-ci.yml`.
28+
> **GitLab CI:** OIDC isn't yet supported. Use the API key method and set the `STAINLESS_API_KEY` environment variable. See the template files in `build/gitlab-ci.yml`.
2929
3030
## Usage
3131

@@ -35,18 +35,17 @@ Add a workflow file to the repository that contains your OpenAPI spec:
3535
<summary><code>.github/workflows/stainless.yml</code></summary>
3636

3737
```yml
38-
name: Build SDKs for pull request
38+
name: Build Stainless SDKs
3939

4040
on:
4141
pull_request:
42-
types:
43-
- opened
44-
- synchronize
45-
- reopened
46-
- closed
42+
types: [opened, synchronize, reopened]
43+
push:
44+
branches: [main]
45+
workflow_dispatch:
4746

4847
concurrency:
49-
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
48+
group: ${{ github.workflow }}-${{ github.ref }}
5049
cancel-in-progress: true
5150

5251
env:
@@ -55,41 +54,20 @@ env:
5554
OAS_PATH: YOUR_OAS_PATH
5655

5756
jobs:
58-
preview:
59-
if: github.event.action != 'closed'
57+
build:
6058
runs-on: ubuntu-latest
6159
permissions:
6260
contents: read
6361
pull-requests: write
6462
id-token: write
6563
steps:
6664
- name: Checkout repository
67-
uses: actions/checkout@v4
65+
uses: actions/checkout@v6
6866
with:
6967
fetch-depth: 2
7068

71-
- name: Run preview builds
72-
uses: stainless-api/upload-openapi-spec-action/preview@v1
73-
with:
74-
org: ${{ env.STAINLESS_ORG }}
75-
project: ${{ env.STAINLESS_PROJECT }}
76-
oas_path: ${{ env.OAS_PATH }}
77-
78-
merge:
79-
if: github.event.action == 'closed' && github.event.pull_request.merged == true
80-
runs-on: ubuntu-latest
81-
permissions:
82-
contents: read
83-
pull-requests: write
84-
id-token: write
85-
steps:
86-
- name: Checkout repository
87-
uses: actions/checkout@v4
88-
with:
89-
fetch-depth: 2
90-
91-
- name: Run merge build
92-
uses: stainless-api/upload-openapi-spec-action/merge@v1
69+
- name: Run builds
70+
uses: stainless-api/upload-openapi-spec-action/build@v1
9371
with:
9472
org: ${{ env.STAINLESS_ORG }}
9573
project: ${{ env.STAINLESS_PROJECT }}
@@ -101,11 +79,8 @@ jobs:
10179
Then, pull requests to your GitHub repository that update OpenAPI spec or
10280
Stainless config will build your SDKs and make a comment with the results.
10381
104-
Note: the `merge` job depends on the `preview` job, so you can't use just
105-
the `merge` job alone. See [our docs](https://www.stainless.com/docs/guides/automate-updates) for more details.
106-
10782
For more details about the input parameters, see the
108-
[example workflow](./examples/pull_request.yml) file.
83+
[example workflow](./examples/push.yml) file.
10984
11085
For more examples of usage, including push-based workflows, using code samples,
11186
integration with docs platforms, and testing preview builds, see the [examples
@@ -116,11 +91,11 @@ directory](./examples).
11691
11792
The workflows require the following [permissions](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#jobsjob_idpermissions):
11893
119-
- **`id-token: write`** - Required for GitHub OIDC authentication. Allows the workflow to request an OIDC token from GitHub.
94+
- **`contents: read`** - Required for checking out the repository code to read the OpenAPI spec and config files.
12095

121-
- **`pull-requests: write`** - Required for posting comments on pull requests with build results. If you don't need comments, you can set `make_comment: false` and remove this permission.
96+
- **`pull-requests: write`** - Required for posting comments on pull requests with build results. If you don't need comments, you can set `make_comment: false` and set the permission to `pull-requests: read`.
12297

123-
- **`contents: read`** - Required for checking out the repository code to read the OpenAPI spec and config files.
98+
- **`id-token: write`** - Required for GitHub OIDC authentication. Allows the workflow to request an OIDC token from GitHub.
12499

125100
</details>
126101

@@ -142,10 +117,6 @@ This repository provides several GitHub actions:
142117

143118
- `stainless-api/upload-openapi-spec-action/build` - Build SDKs for a Stainless project. See the [action definition](./build/action.yml) for input parameters.
144119

145-
- `stainless-api/upload-openapi-spec-action/preview` - Preview SDK changes from a pull request. See the [action definition](./preview/action.yml) for input parameters.
146-
147-
- `stainless-api/upload-openapi-spec-action/merge` - Merge SDK changes from a pull request. See the [action definition](./merge/action.yml) for input parameters.
148-
149120
- `stainless-api/upload-openapi-spec-action/checkout-pr-ref` - Checkout the base or head commit for previewing changes. See the [action definition](./checkout-pr-ref/action.yml) for input parameters.
150121

151122
### Preparation Tools
@@ -156,7 +127,7 @@ This repository provides several GitHub actions:
156127

157128
All except `checkout-pr-ref` work in GitLab CI.
158129

159-
The `preview` and `merge` actions output an `install_url` for each SDK language. You can use this to test builds directly from the Stainless package server before merging. See the [SDK usage example](./examples/pull_request_sdk_usage.yml).
130+
The `build` action outputs an `install_url` for each SDK language. You can use this to test builds directly from the Stainless package server before merging. See the [SDK usage example](./examples/pull_request_sdk_usage.yml).
160131

161132
## Versioning
162133

build/action.yml

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,15 @@ outputs:
124124
"typescript": {
125125
"install_url": "https://pkg.stainless.com/...",
126126
"commit": {
127-
"status": "completed",
128-
"completed": {
129-
"conclusion": "success",
130-
"commit": {
131-
"sha": "...",
132-
"repo": {
133-
"branch": "...",
134-
"name": "...",
135-
"owner": "...",
136-
...
137-
},
127+
"conclusion": "success",
128+
"commit": {
129+
"sha": "...",
130+
"repo": {
131+
"branch": "...",
132+
"name": "...",
133+
"owner": "...",
134+
...
138135
},
139-
...
140136
}
141137
...
142138
},
@@ -155,19 +151,15 @@ outputs:
155151
"typescript": {
156152
"install_url": "https://pkg.stainless.com/...",
157153
"commit": {
158-
"status": "completed",
159-
"completed": {
160-
"conclusion": "success",
161-
"commit": {
162-
"sha": "...",
163-
"repo": {
164-
"branch": "...",
165-
"name": "...",
166-
"owner": "...",
167-
...
168-
},
154+
"conclusion": "success",
155+
"commit": {
156+
"sha": "...",
157+
"repo": {
158+
"branch": "...",
159+
"name": "...",
160+
"owner": "...",
161+
...
169162
},
170-
...
171163
}
172164
...
173165
},

examples/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Example workflows
22

3-
There's two kinds of workflows, depending on how you manage your GitHub repo. Both workflows require you to provide your Stainless org and project names.
3+
All workflows require you to provide your Stainless org and project names.
44

55
For authentication setup, see the [Authentication section](../README.md#authentication) in the main README.
66

7-
## Pull request workflows
7+
## GitHub Actions
88

9-
The main kind of workflows are the pull-request-based workflows, such as [pull_request.yml](./pull_request.yml). If all changes to your OpenAPI spec are made via pull requests, we recommend using this workflow. It has two jobs:
9+
We recommend you use [push.yml](./push.yml). It has a single `build` job that runs when you create or update pull requests and when you push to your main branch. It can also be triggered manually if needed. For pull requests, the `build` job creates a build of your SDK using the changes that will be introduced by the pull request, then it adds a comment to the pull request with links to a GitHub diff and installation commands. For pushes to your main branch, the `build` job creates a build of the SDK with the pushed changes.
1010

11-
- `preview`, which runs when a pull request is opened or updated. The job creates a build of your SDK which includes only the changes introduced by the pull request. It also makes a comment on the pull request, with links to a GitHub diff, and installation commands for trying out your SDK.
11+
By default, the pull request workflow uses the title of the pull request as the commit message, but you can edit the comment on the pull request to change the commit message.
1212

13-
- `merge`, which runs when a pull request is merged. The job creates a build of the SDK with the changes from the pull request, along with any [custom code](https://app.stainless.com/docs/guides/patch-custom-code#project-branches) added to the preview build.
13+
### API key authentication
1414

15-
By default, the pull request workflow uses the title of the pull request as the commit message, but you can edit the comment on the pull request to change the commit message.
15+
If you prefer to authenticate with an API key instead of OIDC, use [pull_request_api_key.yml](./pull_request_api_key.yml).
1616

1717
### Fork pull requests
1818

@@ -26,23 +26,25 @@ The workflow explicitly checks out the PR's code to read the OpenAPI spec. This
2626

2727
If your OpenAPI spec is generated from your GitHub repo, via a shell script or other GitHub action, you will need to do some extra setup. This is because the action needs access to both the old OpenAPI spec and the new OpenAPI spec. See the example at [pull_request_generated.yml](./pull_request_generated.yml).
2828

29-
Here, `checkout-pr-ref` will checkout the relevant base Git commit. The first command runs against the base Git commit, generating the old OpenAPI spec. Then, `checkout-pr-ref` will checkout the relevant head Git commit. The second command runs against the head Git commit, generating the new OpenAPI spec.
29+
Here, `checkout-pr-ref` will checkout the relevant base Git commit. The first command runs against the base Git commit, generating the old OpenAPI spec. Then, `checkout-pr-ref` will checkout the relevant head Git commit. The second command runs against the head Git commit, generating the new OpenAPI spec. These steps are conditioned on pull request events; on pushes to your main branch, only the current spec is needed.
30+
31+
### Using SDK build outputs
3032

31-
## Push workflows
33+
If you want to install and test SDK builds as part of your workflow, see [pull_request_sdk_usage.yml](./pull_request_sdk_usage.yml). This example demonstrates how to extract the `install_url` from the build outputs and use it to install the SDK for integration testing.
3234

33-
The other kind of workflows are the push-based workflows, such as [push.yml](./push.yml). If for some reason you can't use the pull-request-based workflows, you can use this workflow. It has one job:
35+
## GitLab CI
3436

35-
- `build`, which runs when a commit is pushed to a branch you specify. The job creates a build of your SDK against the latest commit on that branch.
37+
For GitLab projects, add the configuration from [push_gitlab.yml](./push_gitlab.yml) to your `.gitlab-ci.yml` file. The workflow uses a single `build-sdk` job that runs on merge requests and pushes to your main branch.
3638

37-
In the examples, the push workflow is configured to use a generic commit message. You can change this to the message of the pushed commit by using `${{ github.event.head_commit.message }}`.
39+
If your OpenAPI spec is in a GitLab repo, see [merge_request_gitlab_generated.yml](./merge_request_gitlab_generated.yml) for additional instructions.
3840

3941
## Integration with docs platforms
4042

41-
If your Stainless config has code samples configured, the `preview`, `merge`, and `build` actions also output a `documented_spec_path` containing a path to your OpenAPI spec with SDK code samples.
43+
If your Stainless config has code samples configured, the `build` action also outputs a `documented_spec_path` containing a path to a version of your OpenAPI spec with SDK code samples included.
4244

43-
If you sync your OpenAPI spec with a [ReadMe API Reference](https://readme.com/), you can use the [Sync to ReadMe](https://github.com/marketplace/actions/rdme-sync-to-readme) GitHub action to upload the documented spec to ReadMe. You can see examples of this in the [pull_request_readme.yml](./pull_request_readme.yml) and [push_readme.yml](./push_readme.yml) files.
45+
If you sync your OpenAPI spec with a [ReadMe API Reference](https://readme.com/), use the [Sync to ReadMe](https://github.com/marketplace/actions/rdme-sync-to-readme) GitHub action to upload the documented spec to ReadMe. We have examples in the [push_readme.yml](./push_readme.yml) file.
4446

45-
If you use [Mintlify's OpenAPI support](https://mintlify.com/docs/api-playground/openapi-setup#in-the-repo) for your API reference documentation, you can copy the documented spec to your Mintlify docs repo to update it. You can see examples of this in the [pull_request_mintlify.yml](./pull_request_mintlify.yml) and [push_mintlify.yml](./push_mintlify.yml) files.
47+
If you use [Mintlify's OpenAPI support](https://mintlify.com/docs/api-playground/openapi-setup#in-the-repo) for your API reference documentation, copy the documented spec to your Mintlify docs repo to update it. We have examples in the [push_mintlify.yml](./push_mintlify.yml) file.
4648

4749
## Spec preparation
4850

examples/merge_request_gitlab.yml

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
# Add this to your .gitlab-ci.yml file.
2+
13
include:
2-
- remote: "https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/preview/gitlab-ci.yml"
3-
- remote: "https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/merge/gitlab-ci.yml"
4+
- remote: "https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/build/gitlab-ci.yml"
45

5-
.common:
6+
build-sdk:
7+
extends: .build-sdk
8+
only:
9+
- merge_requests
10+
# Set this to the branch of the repository that the `main` Stainless branch
11+
# of your project gets its OpenAPI spec from. This is usually the default
12+
# branch of your repository.
13+
- main
614
variables:
15+
# A Stainless API key, which you can generate on the Stainless organization
16+
# dashboard. Add this to your CI/CD variables:
17+
# https://docs.gitlab.com/ci/variables/#add-a-cicd-variable-to-a-project
18+
STAINLESS_API_KEY: $STAINLESS_API_KEY
19+
20+
# A GitLab token with permissions to read and write merge request comments.
21+
# Add this to your CI/CD variables:
22+
# https://docs.gitlab.com/ci/variables/#add-a-cicd-variable-to-a-project
23+
GITLAB_TOKEN: $GITLAB_TOKEN
24+
725
# Stainless organization name.
826
ORG: YOUR_ORG
927

@@ -17,22 +35,14 @@ include:
1735
# to maintain the ground truth Stainless config in your own repo.
1836
CONFIG_PATH: YOUR_CONFIG_PATH
1937

38+
# The commit message to use for the SDK builds. Use a commit message in the
39+
# conventional commits format: https://www.conventionalcommits.org/en/v1.0.0/
40+
COMMIT_MESSAGE: "feat(api): update api"
41+
2042
# When to fail the job based on build conclusion.
2143
# Options: "never" | "note" | "warning" | "error" | "fatal".
2244
FAIL_ON: error
2345

24-
# A Stainless API key, which you can generate on the Stainless organization dashboard
25-
STAINLESS_API_KEY: $STAINLESS_API_KEY
26-
27-
# A GitLab token with permissions to read and write merge request comments.
28-
GITLAB_TOKEN: $GITLAB_TOKEN
29-
30-
preview:
31-
extends: [.common, .preview]
32-
only:
33-
- merge_requests
34-
35-
merge:
36-
extends: [.common, .merge]
37-
rules:
38-
- if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
46+
# Path to write a documented OpenAPI spec to. Optional; will only write if
47+
# you code samples configured in your Stainless config.
48+
DOCUMENTED_SPEC_PATH: YOUR_DOCUMENTED_SPEC_PATH

examples/merge_request_gitlab_generated.yml

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
include:
2-
- remote: "https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/preview/gitlab-ci.yml"
3-
- remote: "https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/merge/gitlab-ci.yml"
2+
- remote: "https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/build/gitlab-ci.yml"
43
- remote: "https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/checkout-pr-ref/gitlab-ci.yml"
54

65
.common:
@@ -28,26 +27,29 @@ include:
2827
# A GitLab token with permissions to read and write merge request comments.
2928
GITLAB_TOKEN: $GITLAB_TOKEN
3029

31-
preview:
32-
extends: [.common, .preview]
30+
build-sdk:
31+
extends: [.common, .build-sdk]
3332
script:
3433
- apk add --no-cache git
35-
- wget https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/dist/checkoutPRRef.js
3634

37-
- REF="base" node checkoutPRRef.js
38-
# generate oas and save to OAS_PATH
39-
# { insert your code here }
35+
- |
36+
if [ "$CI_PIPELINE_SOURCE" = "merge_request_event" ]; then
37+
wget https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/dist/checkoutPRRef.js
4038
41-
- REF="head" node checkoutPRRef.js
42-
# generate oas and save to OAS_PATH
43-
# { insert your code here }
39+
REF="base" node checkoutPRRef.js
40+
# generate oas and save to OAS_PATH
41+
# { insert your code here }
4442
45-
- wget https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/dist/preview.js
46-
- node preview.js
43+
REF="head" node checkoutPRRef.js
44+
# generate oas and save to OAS_PATH
45+
# { insert your code here }
46+
else
47+
# generate oas and save to OAS_PATH
48+
# { insert your code here }
49+
fi
50+
51+
- wget https://raw.githubusercontent.com/stainless-api/upload-openapi-spec-action/v1/dist/build.js
52+
- node build.js
4753
only:
4854
- merge_requests
49-
50-
merge:
51-
extends: [.common, .merge]
52-
rules:
53-
- if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
55+
- main

examples/prepare_combine.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
# IMPORTANT: Always checkout the repository first!
4848
# Without this step, the action cannot access your spec files.
4949
- name: Checkout repository
50-
uses: actions/checkout@v4
50+
uses: actions/checkout@v6
5151

5252
- name: Combine OpenAPI specs
5353
id: combine

examples/prepare_swagger.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
# IMPORTANT: Always checkout the repository first!
2929
# Without this step, the action cannot access your spec files.
3030
- name: Checkout code
31-
uses: actions/checkout@v4
31+
uses: actions/checkout@v6
3232
with:
3333
# Use the pull request head ref for PRs, otherwise use the default branch
3434
ref: ${{ github.head_ref || github.ref }}

0 commit comments

Comments
 (0)