-
Notifications
You must be signed in to change notification settings - Fork 4
179 lines (156 loc) · 5.47 KB
/
Copy pathrelease.yaml
File metadata and controls
179 lines (156 loc) · 5.47 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
name: Release
on:
push:
tags:
- v*.*.*
workflow_dispatch:
inputs:
tag:
description: New or existing tag (e.g. v1.2.3)
required: true
type: string
permissions:
contents: write # needed to push tag and/or create release
env:
OPENAPI_ENTRY: specification/gridtariffapi.json
WORK_DIR: dist
BUNDLED_JSON: dist/openapi.json
REDOC_HTML: dist/redoc.html
REDOCLY_VERSION: 2.0.8
TAG: "${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }}"
jobs:
tag:
runs-on: ubuntu-latest
steps:
- name: Validate tag format
id: validate
shell: bash
run: |
if [[ ! "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Error: version must match vX.Y.Z (e.g., v1.2.3). Got: $TAG"
exit 1
fi
- if: ${{ github.event_name == 'workflow_dispatch' }}
name: Check out repository with full history
uses: actions/checkout@v4
with:
fetch-depth: 0 # we need full history to create/push a tag cleanly
- if: ${{ github.event_name == 'workflow_dispatch' }}
name: Create and push tag
env:
GH_TOKEN: ${{ github.token }}
run: |
if git ls-remote --tags origin | grep -q "refs/tags/${TAG}$"; then
echo "Tag ${TAG} already exists on origin. Using ${TAG} to create new release."
else
echo "Pushing tag ${TAG} to repo and using it to create new release."
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag -a "${TAG}" -m "Release ${TAG}" "${GITHUB_SHA}"
git push "https://x-access-token:${GH_TOKEN}@github.com/${{ github.repository }}.git" "refs/tags/${TAG}"
fi
build:
runs-on: ubuntu-latest
needs: tag
steps:
- name: Check out tag ref
uses: actions/checkout@v4
with:
ref: ${{ env.TAG }}
- name: Bundle OpenAPI
run: |
mkdir -p "$WORK_DIR"
npx --yes @redocly/cli@$REDOCLY_VERSION bundle "$OPENAPI_ENTRY" \
--output "$BUNDLED_JSON" \
--ext json
- name: Lint OpenAPI (bundled)
run: npx --yes @redocly/cli@$REDOCLY_VERSION lint "$BUNDLED_JSON"
- name: Build Redoc HTML
run: npx --yes @redocly/cli@$REDOCLY_VERSION build-docs "$BUNDLED_JSON" --output="$REDOC_HTML"
- name: Verify info.version matches tag
run: |
version_in_tag=${TAG#v}
version_in_file=$(jq -r '.info.version' $BUNDLED_JSON)
if [ "$version_in_file" != "$version_in_tag" ]; then
echo "info.version ($version_in_file) does not match input version ($version_in_tag)"
exit 1
fi
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: openapi-release-${{ env.TAG }}
path: |
${{ env.BUNDLED_JSON }}
${{ env.REDOC_HTML }}
release:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/download-artifact@v4
with:
name: openapi-release-${{ env.TAG }}
path: ${{ env.WORK_DIR }}
- name: Append assets to release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ env.TAG }}
files: |
${{ env.BUNDLED_JSON }}
${{ env.REDOC_HTML }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish:
runs-on: ubuntu-latest
needs: release
steps:
- name: Check out tag ref
uses: actions/checkout@v4
with:
ref: ${{ env.TAG }}
- name: Get commit date
id: tag_info
run: |
commit_date="$(git show -s --format=%ai "$TAG^{commit}" | sed -E 's/([0-9-]+) ([0-9:]+) .*/\1T\2/')"
echo "commit_date=$commit_date" >> "$GITHUB_OUTPUT"
echo "Resolved tag $TAG with commit date $COMMIT_DATE"
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
path: site
- uses: actions/download-artifact@v4
with:
name: openapi-release-${{ env.TAG }}
path: ${{ env.WORK_DIR }}
- name: Copy versioned files
run: |
set -e
release_dir="site/releases/$TAG"
mkdir -p "$release_dir"
ls -la
cp "$BUNDLED_JSON" "$release_dir/openapi.json"
cp "$REDOC_HTML" "$release_dir/redoc.html"
- name: Update versions.json
env:
COMMIT_DATE: ${{ steps.tag_info.outputs.commit_date }}
run: |
set -e
cd site
test -f versions.json || echo "[]" > versions.json
# Remove previous entry for this tag (idempotent)
jq --arg tag "$TAG" 'map(select(.tag != $tag))' versions.json > .tmp.json && mv .tmp.json versions.json
jq --arg tag "$TAG" \
--arg date "$COMMIT_DATE" \
'. + [{tag:$tag, date:$date}]' versions.json > .tmp.json && mv .tmp.json versions.json
- name: Commit & push
run: |
cd site
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
if git diff --staged --quiet; then
echo "No changes to commit."
else
git commit -m "Publish ${{ steps.resolve.outputs.tag }}"
git push origin gh-pages
fi