Skip to content

Commit e4b3b64

Browse files
Update release workflow
1 parent b14dc5a commit e4b3b64

1 file changed

Lines changed: 95 additions & 35 deletions

File tree

.github/workflows/release.yaml

Lines changed: 95 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ permissions:
1616

1717
env:
1818
OPENAPI_ENTRY: specification/gridtariffapi.json
19-
OUT_DIR: dist
19+
WORK_DIR: dist
2020
BUNDLED_JSON: dist/openapi.json
2121
REDOC_HTML: dist/redoc.html
2222
REDOCLY_VERSION: 2.0.8
23+
TAG: "${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }}"
2324

2425
jobs:
25-
release:
26+
tag:
2627
runs-on: ubuntu-latest
27-
env:
28-
TAG: "${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }}"
29-
3028
steps:
3129
- name: Validate tag format
3230
id: validate
@@ -37,14 +35,39 @@ jobs:
3735
exit 1
3836
fi
3937
40-
- name: Check out repository with full history
38+
- if: ${{ github.event_name == 'workflow_dispatch' }}
39+
name: Check out repository with full history
4140
uses: actions/checkout@v4
4241
with:
4342
fetch-depth: 0 # we need full history to create/push a tag cleanly
4443

44+
- if: ${{ github.event_name == 'workflow_dispatch' }}
45+
name: Create and push tag
46+
env:
47+
GH_TOKEN: ${{ github.token }}
48+
run: |
49+
if git ls-remote --tags origin | grep -q "refs/tags/${TAG}$"; then
50+
echo "Tag ${TAG} already exists on origin. Using ${TAG} to create new release."
51+
else
52+
echo "Pushing tag ${TAG} to repo and using it to create new release."
53+
git config user.name "github-actions[bot]"
54+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
55+
git tag -a "${TAG}" -m "Release ${TAG}" "${GITHUB_SHA}"
56+
git push "https://x-access-token:${GH_TOKEN}@github.com/${{ github.repository }}.git" "refs/tags/${TAG}"
57+
fi
58+
59+
build:
60+
runs-on: ubuntu-latest
61+
needs: tag
62+
steps:
63+
- name: Check out tag ref
64+
uses: actions/checkout@v4
65+
with:
66+
ref: ${{ env.TAG }}
67+
4568
- name: Bundle OpenAPI
4669
run: |
47-
mkdir -p "$OUT_DIR"
70+
mkdir -p "$WORK_DIR"
4871
npx --yes @redocly/cli@$REDOCLY_VERSION bundle "$OPENAPI_ENTRY" \
4972
--output "$BUNDLED_JSON" \
5073
--ext json
@@ -64,30 +87,22 @@ jobs:
6487
exit 1
6588
fi
6689
67-
- if: ${{ github.event_name == 'workflow_dispatch' }}
68-
name: Create and push tag
69-
env:
70-
GH_TOKEN: ${{ github.token }}
71-
run: |
72-
if git ls-remote --tags origin | grep -q "refs/tags/${TAG}$"; then
73-
echo "Tag ${TAG} already exists on origin. Using ${TAG} to create new release."
74-
else
75-
echo "Pushing tag ${TAG} to repo and using it to create new release."
76-
git config user.name "github-actions[bot]"
77-
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
78-
git tag -a "${TAG}" -m "Release ${TAG}" "${GITHUB_SHA}"
79-
git push "https://x-access-token:${GH_TOKEN}@github.com/${{ github.repository }}.git" "refs/tags/${TAG}"
80-
fi
81-
82-
- name: Check out tag ref
83-
uses: actions/checkout@v4
90+
- name: Upload build artifacts
91+
uses: actions/upload-artifact@v4
8492
with:
85-
ref: ${{ env.TAG }}
93+
name: openapi-release-${{ TAG }}
94+
path: |
95+
dist/openapi.json
96+
dist/redoc.html
8697
87-
- name: Set up Node
88-
uses: actions/setup-node@v4
98+
release:
99+
runs-on: ubuntu-latest
100+
needs: build
101+
steps:
102+
- uses: actions/download-artifact@v4
89103
with:
90-
node-version: "20"
104+
name: openapi-release-${{ TAG }}
105+
path: dist
91106

92107
- name: Create GitHub Release
93108
uses: softprops/action-gh-release@v2
@@ -106,11 +121,56 @@ jobs:
106121
env:
107122
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
108123

109-
# (Nice to have) Upload the files as workflow run artifacts as well
110-
- name: Upload build artifacts
111-
uses: actions/upload-artifact@v4
124+
publish:
125+
runs-on: ubuntu-latest
126+
needs: release
127+
steps:
128+
- uses: actions/download-artifact@v4
112129
with:
113-
name: openapi-release-${{ steps.validate.outputs.tag }}
114-
path: |
115-
dist/openapi.json
116-
dist/redoc.html
130+
name: openapi-release-${{ TAG }}
131+
path: dist
132+
133+
- name: Checkout gh-pages
134+
uses: actions/checkout@v4
135+
with:
136+
ref: gh-pages
137+
path: site
138+
139+
- name: Copy versioned files
140+
env:
141+
TAG: ${{ steps.resolve.outputs.tag }}
142+
run: |
143+
set -e
144+
release_dir="site/releases/$TAG"
145+
mkdir -p "$release_dir"
146+
cp dist/openapi.json "$release_dir/openapi.json"
147+
cp dist/redoc.html "$release_dir/redoc.html"
148+
149+
- name: Update versions.json
150+
env:
151+
TAG: ${{ steps.resolve.outputs.tag }}
152+
COMMIT_DATE: ${{ steps.resolve.outputs.commit_date }}
153+
run: |
154+
set -e
155+
cd site
156+
test -f versions.json || echo "[]" > versions.json
157+
158+
# Remove previous entry for this tag (idempotent)
159+
jq --arg tag "$TAG" 'map(select(.tag != $tag))' versions.json > .tmp.json && mv .tmp.json versions.json
160+
161+
jq --arg tag "$TAG" \
162+
--arg date "$COMMIT_DATE" \
163+
'. + [{tag:$tag, date:$date}]' versions.json > .tmp.json && mv .tmp.json versions.json
164+
165+
- name: Commit & push
166+
run: |
167+
cd site
168+
git config user.name "github-actions[bot]"
169+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
170+
git add -A
171+
if git diff --staged --quiet; then
172+
echo "No changes to commit."
173+
else
174+
git commit -m "Publish ${{ steps.resolve.outputs.tag }}"
175+
git push origin gh-pages
176+
fi

0 commit comments

Comments
 (0)