-
Notifications
You must be signed in to change notification settings - Fork 2
144 lines (129 loc) · 7.35 KB
/
deploy_preview.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
name: Deploy Preview
on:
pull_request:
branches: [main]
types: [opened, synchronize, labeled]
jobs:
build_preview:
# require 'deploy preview' label
# but do not run if already labeled with 'deploy preview' and a different label was just added
# eg. a PR that already has a preview has the label "released" applied by semantic release bot
if: (!github.event.label && contains(github.event.pull_request.labels.*.name, 'deploy preview')) || github.event.label.name == 'deploy preview'
runs-on: ubuntu-latest
steps:
- name: Checkout branch head (not merged head)
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
# it is important to remove the default token credentials from git in this step
# otherwise they will get in the way of fetching the private TradingView dependency
persist-credentials: false
- name: Set Deploy Preview alias name (combine short SHA and files hash) (deploy alias max length is 37)
run: |
SHORT_SHA=$(node -p "'${{ github.event.pull_request.head.sha }}'.slice(0,8)")
SHORT_HASH=$(node -p "'${{ env.all-files-hash }}'.slice(0,8)")
echo "::set-output name=DEPLOY_PREVIEW_ALIAS::$SHORT_SHA-$SHORT_HASH"
env:
all-files-hash: ${{ hashFiles('**/*') }}
id: get-deploy-alias
- name: Set Node version
# use basic regex (first found digits and dots) to get Node engine version from package.json
# will fail if not correctly specified
run: |
NODE_VERSION=$(node -p "require('./package.json').engines.node.match(/[\d.]+/)[0]")
echo "::set-output name=NODE_VERSION::$NODE_VERSION"
id: get-node-version
- name: Use Node.js ${{ steps.get-node-version.outputs.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ steps.get-node-version.outputs.NODE_VERSION }}
cache: npm
- name: Install dependencies
# note: from https://github.com/actions/setup-node/issues/49#issuecomment-1293249466
# security consideration:
# Skip post-install scripts here, as a malicious script could steal NODE_AUTH_TOKEN.
# use variable to read in the exact chart library dependency specified in package.json
run: |
TVC_LOCATION=$( node -p "require('./package.json').dependencies['charting_library'].replace('github:','github.com/')" );
npm install --ignore-scripts https://dib542:$TOKEN@$TVC_LOCATION
env:
# to install TradingView private dependency, private GitHub credentials are needed
# secret tokens are added in Github project settings under [project]/settings/secrets/actions
TOKEN: ${{ secrets.GH_TOKEN }}
# `npm rebuild` will run all those post-install scripts for us.
- name: Run post-install scripts
run: npm rebuild && npm run prepare --if-present
# run build with .env.testnet file (instead of .env.production)
# and add Netlify settings
- name: Build testnet
run: npm run build -- --mode testnet && cp netlify.toml build/netlify.toml
env:
REACT_APP__BUILD_NUMBER: ${{ github.run_id }}
- name: Deploy testnet preview to Netlify
id: netlify_deploy_testnet_preview
# see: https://github.com/marketplace/actions/netlify-deploy
uses: jsmrcaga/action-netlify-deploy@v1.7.2
with:
# secret tokens are added in Github project settings under [project]/settings/secrets/actions
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_TESTNET_SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: Preview deploy ${{ github.ref }} # github.ref looks like `refs/pull/[PR#]/merge`
NETLIFY_DEPLOY_TO_PROD: false
deploy_alias: ${{ steps.get-deploy-alias.outputs.DEPLOY_PREVIEW_ALIAS }}
build_directory: build
# we skip the build steps in this custom step because if we allow the project to build inside this step
# and specify the node version for that, this action will first download the latest node version,
# then switch back to the specified node version, then build the project.
# this wastes build minutes, so we instead pre-build the "build" folder
build_command: echo "already built app"
install_command: echo "already installed dependencies"
- uses: chrnorm/deployment-action@v2
name: Create GitHub Preview deployment
id: deployment
with:
token: '${{ secrets.GIT_PAT }}'
environment-url: ${{ steps.netlify_deploy_testnet_preview.outputs.NETLIFY_PREVIEW_URL }}
environment: Preview
initial-status: success
- name: Clear build folder
run: rm -rf build
# run build with .env.beta file (instead of .env.production)
# and add Netlify settings
- name: Build beta
run: npm run build -- --mode beta && cp netlify.toml build/netlify.toml
env:
REACT_APP__BUILD_NUMBER: ${{ github.run_id }}
- name: Deploy beta preview to Netlify
id: netlify_deploy_beta_preview
# see: https://github.com/marketplace/actions/netlify-deploy
uses: jsmrcaga/action-netlify-deploy@v1.7.2
with:
# secret tokens are added in Github project settings under [project]/settings/secrets/actions
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_BETA_SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: Preview deploy ${{ github.ref }} # github.ref looks like `refs/pull/[PR#]/merge`
NETLIFY_DEPLOY_TO_PROD: false
deploy_alias: ${{ steps.get-deploy-alias.outputs.DEPLOY_PREVIEW_ALIAS }}
build_directory: build
# we skip the build steps in this custom step because if we allow the project to build inside this step
# and specify the node version for that, this action will first download the latest node version,
# then switch back to the specified node version, then build the project.
# this wastes build minutes, so we instead pre-build the "build" folder
build_command: echo "already built app"
install_command: echo "already installed dependencies"
- name: Add comment to PR
if: github.ref != 'refs/heads/main' #github.ref looks like `refs/pull/[PR#]/merge`
env:
# use GitHub action token to post comment to PR
GITHUB_TOKEN: ${{ github.token }}
# get deploy outputs: see https://github.com/marketplace/actions/netlify-deploy#outputs
# get PR number: see https://github.com/actions/checkout/issues/58#issuecomment-812259610
run: |
echo "## Deploy Previews" > netlify.txt
echo "### Testnet" >> netlify.txt
echo "Preview URL: ${{ steps.netlify_deploy_testnet_preview.outputs.NETLIFY_PREVIEW_URL }}" >> netlify.txt
echo "Logs: ${{ steps.netlify_deploy_testnet_preview.outputs.NETLIFY_LOGS_URL }}" >> netlify.txt
echo "### Beta" >> netlify.txt
echo "Preview URL: ${{ steps.netlify_deploy_beta_preview.outputs.NETLIFY_PREVIEW_URL }}" >> netlify.txt
echo "Logs: ${{ steps.netlify_deploy_beta_preview.outputs.NETLIFY_LOGS_URL }}" >> netlify.txt
gh pr comment ${{ github.event.pull_request.number }} --body-file netlify.txt || true