Last active
June 21, 2025 23:07
-
-
Save yokawasa/8f3d62bdd156396c6c249e389306ea6e to your computer and use it in GitHub Desktop.
GitHub Actions that update Postman Collection based upon OpenAPI Spec using openapi-to-postman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update Postman Collection | |
on: | |
pull_request: | |
branches: [main] | |
types: [opened, synchronize] | |
env: | |
# OpenAPI file path | |
OPENAPI_FILE: oas/openapi.yaml | |
# Postman Collection file path | |
COLLECTION_FILE: postman/collections/36962762-584eb5cd-551c-4b2a-8bcc-d4ae56f2db0e.json | |
jobs: | |
generate-postman-collection: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout PR branch | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ github.event.pull_request.head.ref }} | |
- name: Check for changes in openapi.yaml | |
id: detect_change | |
run: | | |
git fetch origin main | |
CHANGED=$(git diff --name-only origin/main HEAD | grep "$OPENAPI_FILE" || true) | |
echo "changed_files=$CHANGED" >> $GITHUB_OUTPUT | |
- name: Exit if no changes detected | |
if: steps.detect_change.outputs.changed_files == '' | |
run: | | |
echo "No changes detected in oas/openapi.yaml. Exiting workflow." | |
exit 0 | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '18' | |
- name: Install openapi-to-postmanv2 | |
run: npm install -g openapi-to-postmanv2 | |
- name: Get postman collection ID | |
id: get_collection_id | |
run: | | |
if [ -f "$COLLECTION_FILE" ]; then | |
COLLECTION_ID=$(jq -r '.info._postman_id' "$COLLECTION_FILE") | |
echo "collection_id=$COLLECTION_ID" >> $GITHUB_OUTPUT | |
fi | |
- name: Generate postman collection | |
run: | | |
openapi2postmanv2 -s $OPENAPI_FILE \ | |
-o $COLLECTION_FILE -p \ | |
-O folderStrategy=Tags,parametersResolution=Example,enableOptionalParameters=false | |
# Update collection ID if it exists | |
if [ -n "${{ steps.get_collection_id.outputs.collection_id }}" ]; then | |
jq --arg id "${{ steps.get_collection_id.outputs.collection_id }}" \ | |
'.info._postman_id = $id' "$COLLECTION_FILE" > tmp.json | |
mv tmp.json "$COLLECTION_FILE" | |
fi | |
- name: Commit and push generated collection | |
run: | | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
git add $COLLECTION_FILE | |
git commit -m "chore: auto-generate postman collection from OpenAPI" | |
git push origin HEAD:${{ github.event.pull_request.head.ref }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Required Settings on Repository
このGHAは生成されたPostmanコレクションをリポジトリにPushするため、リポジトリへ書き込みするための設定が必要です。もし書き込み権限がない場合は、下記手順を参考に設定してください
Option 1. GitHub Actionsに書き込み権限を付与する
リポジトリの設定で、GitHub Actionsに書き込み権限を付与する必要があります。
Option 2. Personal Access Token (PAT) を使用する
GitHub Actionsがリポジトリにプッシュできるようにするために、Personal Access Token (PAT) を使用する方法もあります。
repo
)を選択してトークンを生成GH_PAT
以下のように、PATを使用して認証するようにGitHub Actionsのワークフローを修正します。