-
Notifications
You must be signed in to change notification settings - Fork 62
183 lines (165 loc) · 5.19 KB
/
ci-cd.yml
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
180
181
182
183
name: CI-CD
on:
push:
branches:
- main
- next
- 'v*'
paths-ignore:
- '.github/**'
- '.vscode/**'
# pull-request means to deploy preview
pull_request_target:
# workflow_dispatch means to manual deploy production
workflow_dispatch:
# workflow_call means to deploy production from external workflow
# it would be useful to trigger from fastify main repo instead of
# daily schedule
workflow_call:
jobs:
setup:
runs-on: ubuntu-latest
steps:
- name: Select environment
id: env-selector
run: |
echo "current_env=${{ github.event_name == 'pull_request_target' && 'staging' || 'production' }}" >> $GITHUB_OUTPUT
echo "website_url=${{ github.event_name == 'pull_request_target' && '/' || '/' }}" >> $GITHUB_OUTPUT
- name: Get latest Fastify tag
id: latest-fastify
uses: oprypin/find-latest-tag@v1
with:
repository: fastify/fastify
releases-only: true
outputs:
website_url: ${{ steps.env-selector.outputs.website_url }}
current_env: ${{ steps.env-selector.outputs.current_env }}
latest_fastify: ${{ steps.latest-fastify.outputs.tag }}
build-and-upload:
needs: setup
runs-on: ubuntu-latest
permissions:
contents: read
env:
BASE_URL: ${{ needs.setup.outputs.website_url }}
steps:
# we set the correct repository and reference sha for PR preview
- name: Checkout PR
if: ${{ github.event_name == 'pull_request_target' }}
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
# we set the correct reference sha for production
- name: Checkout
if: ${{ github.event_name != 'pull_request_target' }}
uses: actions/checkout@v4
with:
ref: main
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 18
cache: npm
node-version-file: '.nvmrc'
- name: Install Packages
run: npm ci
- name: Run Linting
run: npm run lint
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Get latest Fastify tag
id: latest-fastify
uses: oprypin/find-latest-tag@v1
with:
repository: fastify/fastify
releases-only: true
- run: echo "Fastify is at version ${{ steps.latest-fastify.outputs.tag }}"
- name: Cache Fastify documentation
id: release-cache
uses: actions/cache@v4
with:
path: |
scripts/releases
scripts/releases.tag
key: ${{ steps.latest-fastify.outputs.tag }}-release-cache
# build website
- name: Build website
run: npm run build:website
env:
GH_TOKEN: ${{ github.token }}
SKIP_DOWNLOADS: ${{ steps.release-cache.outputs.cache-hit }}
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: './build'
deploy-to-production:
if: ${{ needs.setup.outputs.current_env == 'production' }}
needs:
- setup
- build-and-upload
concurrency:
group: ${{ github.ref_name }}-production
cancel-in-progress: true
env:
BASE_URL: ''
permissions:
pages: write
id-token: write
environment:
name: ${{ needs.setup.outputs.current_env }}
url: ${{ steps.deployment.outputs.page_url }}
outputs:
deployment_url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
# with:
# enable preview when PR
# preview: ${{ github.event_name == 'pull_request_target' }}
deploy-to-staging:
if: ${{ needs.setup.outputs.current_env == 'staging' }}
needs:
- setup
- build-and-upload
concurrency:
group: ${{ github.ref_name }}-staging
cancel-in-progress: true
env:
BASE_URL: ''
permissions:
pull-requests: write
contents: write
environment:
name: ${{ needs.setup.outputs.current_env }}
url: ${{ steps.deployment.outputs.NETLIFY_URL }}
outputs:
deployment_url: ${{ steps.deployment.outputs.NETLIFY_URL }}
runs-on: ubuntu-latest
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: github-pages
path: website
- name: Extract artifact
run: tar -xvf artifact.tar
working-directory: website
shell: 'bash'
- name: Clean up before deploy
run: rm artifact.tar
working-directory: website
shell: 'bash'
- name: Display structure of downloaded files
run: ls -R
- name: Deploy to Netlify
uses: netlify/actions/cli@master
id: deployment
with:
# https://cli.netlify.com/commands/deploy/
args: deploy --json --dir="./website" --message="Deploy from branch ${{ github.ref }}"
env:
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}