-
Notifications
You must be signed in to change notification settings - Fork 0
148 lines (127 loc) · 4.26 KB
/
staging.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
name: Staging
on:
pull_request:
push:
branches:
- main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
actions: read
pull-requests: write
checks: write
jobs:
test:
name: Run tests
runs-on: ubuntu-latest
outputs:
publish_cf_pages: ${{ steps.check_cf_pages.outputs.publish_cf_pages }}
steps:
- name: Checkout git repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0
- name: Setup Node with caching
uses: actions/setup-node@v4
with:
node-version: "lts/*"
cache: "pnpm"
- name: NX cache
uses: actions/cache@v3
with:
path: node_modules/.cache/nx
key: nx-${{ github.ref }}
- name: Derive HEAD and BASE shas for 'nx affected'
uses: nrwl/nx-set-shas@v4
- name: Install dependencies
run: pnpm install --frozen-lockfile --strict-peer-dependencies
- name: Test
run: pnpm nx affected --parallel --targets=lint,test:cov
- name: Check if can publish code coverage
id: check_codecov
run: |
if [ "$CODECOV_TOKEN" == "" ]; then
echo "publish_codecov=0" >> $GITHUB_OUTPUT
else
echo "publish_codecov=1" >> $GITHUB_OUTPUT
fi
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Publish code coverage
if: steps.check_codecov.outputs.publish_codecov == '1'
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Check if can publish to cloudflare
id: check_cf_pages
run: |
if [ "$CLOUDFLARE_API_TOKEN" == "" ] \
|| [ "$CLOUDFLARE_ACCOUNT_ID" == "" ] \
|| [ "$CLOUDFLARE_PAGES_PROJECT" == "" ]; then
echo "publish_cf_pages=0" >> $GITHUB_OUTPUT
else
echo "publish_cf_pages=1" >> $GITHUB_OUTPUT
fi
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_PAGES_PROJECT: ${{ vars.CLOUDFLARE_PAGES_PROJECT }}
publish:
name: Publish to CF pages
if: needs.test.outputs.publish_cf_pages == '1'
runs-on: ubuntu-latest
needs:
- test
environment:
name: Staging
url: ${{ steps.publish-client.outputs.url }}
steps:
- name: Checkout git repository
uses: actions/checkout@v4
- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0
- name: Setup Node with caching
uses: actions/setup-node@v4
with:
node-version: "lts/*"
cache: "pnpm"
- name: Install dependencies
run: |
pnpm install --frozen-lockfile --strict-peer-dependencies
pnpm install -g wrangler
- name: Build
run: pnpm --filter ./apps/client build
- name: Publish client to cloudflare
id: publish-client
working-directory: apps/client/
run: |
if [[ "${{ github.ref }}" =~ refs/pull/.+ ]]; then
# Pull requests deploy to their own staging environments
: "${{ github.ref }}"
: ${_%%\/merge} # Remove 'merge' at the end
: ${_//refs\/pull\/} # Remove 'refs' at the beginning
TARGET="PR-${_}"
else
TARGET="${{ github.ref_name }}"
fi
# Use wrangler to manually deploy to cloudflare
wrangler pages publish www/ \
--project-name=$CLOUDFLARE_PAGES_PROJECT \
--branch="$TARGET" \
--commit-dirty=true | tee w.out
# Parse the output URL from the wrangler output
TARGET_URL=$(grep -oE "https?://.+\.pages\.dev" w.out | tail -n1)
if [[ "$TARGET_URL" ]]; then
echo "url=$TARGET_URL" >> $GITHUB_OUTPUT
else
echo "Failed to find URL for deployment"
exit 1
fi
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_PAGES_PROJECT: ${{ vars.CLOUDFLARE_PAGES_PROJECT }}