-
Notifications
You must be signed in to change notification settings - Fork 1
110 lines (91 loc) · 4.78 KB
/
deploy-collection.yml
File metadata and controls
110 lines (91 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Deploy Postman collection
on:
push:
branches: [master]
paths:
- openapi/openapi.yaml
workflow_dispatch:
inputs:
postman_api_key:
description: Postman API key - For debugging purposes
default: ''
postman_workspace_name:
description: Postman Workspace name - For debugging purposes
default: 'Shoptet public API Workspace'
postman_collection_name:
description: Postman Collection name - For debugging purposes
default: 'Shoptet API'
env:
POSTMAN_API_KEY: ${{ github.event.inputs.postman_api_key || secrets.POSTMAN_API_KEY }}
POSTMAN_WORKSPACE_NAME: ${{ github.event.inputs.postman_workspace_name || 'Shoptet public API Workspace' }}
POSTMAN_COLLECTION_NAME: ${{ github.event.inputs.postman_collection_name || 'Shoptet API' }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install openapi-to-postmanv2
run: npm install -g openapi-to-postmanv2
- name: Generate Postman collection from OpenAPI
run: |
openapi2postmanv2 -s ./openapi/openapi.yaml -o postman_collection.json -p -O folderStrategy=Tags
- name: Cleanup empty Postman collection items
run: |
cp postman_collection.json postman_collection.json.tmp &&
jq 'del(.item[] | select(.name == "Batch" or .name == "Snapshot"))' < postman_collection.json.tmp > postman_collection.json &&
rm postman_collection.json.tmp
- name: Prepare Postman collection request body
run: |
jq '{ collection: . }' <postman_collection.json | jq --arg POSTMAN_COLLECTION_NAME "$POSTMAN_COLLECTION_NAME" '.collection.info.name |= $POSTMAN_COLLECTION_NAME' >postman_collection_request_body.json
- name: Postman API - Get Workspace ID
id: get_postman_workspace_id
run: |
RESPONSE_BODY=$(curl --fail --silent --show-error --location --request GET \
"https://api.getpostman.com/workspaces" \
--header "X-Api-Key: $POSTMAN_API_KEY" \
--header "Content-Type: application/json")
echo "::debug::$RESPONSE_BODY"
WORKSPACE_ID=$(echo $RESPONSE_BODY | jq -r --arg POSTMAN_WORKSPACE_NAME "$POSTMAN_WORKSPACE_NAME" '.workspaces[] | select(.name == $POSTMAN_WORKSPACE_NAME)'.id)
echo "::debug::$WORKSPACE_ID"
echo "workspace_id=$WORKSPACE_ID" >> $GITHUB_OUTPUT
if [[ $WORKSPACE_ID = '' ]]; then
echo "::error::No Workspace found. Please create a workspace with name $POSTMAN_WORKSPACE_NAME"
exit 1
fi
- name: Postman API - Get Collection ID
id: get_postman_collection_id
run: |
echo "::debug::${{ steps.get_postman_workspace_id.outputs.workspace_id }}"
RESPONSE_BODY=$(curl --fail --silent --show-error --location --request GET \
"https://api.getpostman.com/collections?workspace=${{ steps.get_postman_workspace_id.outputs.workspace_id }}" \
--header "X-Api-Key: $POSTMAN_API_KEY" \
--header "Content-Type: application/json")
echo "::debug::$RESPONSE_BODY"
COLLECTION_ID=$(echo $RESPONSE_BODY | jq -r --arg POSTMAN_COLLECTION_NAME "$POSTMAN_COLLECTION_NAME" '.collections[] | select(.name == $POSTMAN_COLLECTION_NAME)'.id)
echo "::debug::$COLLECTION_ID"
echo "collection_id=$COLLECTION_ID" >> $GITHUB_OUTPUT
- name: Postman API - Create Collection if not exists
if: steps.get_postman_collection_id.outputs.collection_id == ''
run: |
echo "::debug::${{ steps.get_postman_workspace_id.outputs.workspace_id }}"
RESPONSE_BODY=$(curl --fail --silent --show-error --location --request POST \
"https://api.getpostman.com/collections?workspace=${{ steps.get_postman_workspace_id.outputs.workspace_id }}" \
--header "X-Api-Key: $POSTMAN_API_KEY" \
--header "Content-Type: application/json" \
--data @postman_collection_request_body.json)
echo "::debug::$RESPONSE_BODY"
- name: Postman API - Update existing Collection
if: steps.get_postman_collection_id.outputs.collection_id != ''
run: |
echo "::debug::${{ steps.get_postman_collection_id.outputs.collection_id }}"
RESPONSE_BODY=$(curl --fail --silent --show-error --location --request PUT \
"https://api.getpostman.com/collections/${{ steps.get_postman_collection_id.outputs.collection_id }}" \
--header "X-Api-Key: $POSTMAN_API_KEY" \
--header "Content-Type: application/json" \
--data @postman_collection_request_body.json)
echo "::debug::$RESPONSE_BODY"