-
Notifications
You must be signed in to change notification settings - Fork 0
115 lines (111 loc) · 5.34 KB
/
standalone-build-artifact.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
on:
workflow_call:
inputs:
deployTo:
description: Directory where to deploy to (if empty that home dir is used)
type: string
required: false
ecosystemFile:
description: Override for which file to use for ecosystem.config.js
type: string
required: false
default: "ecosystem.config.js"
applicationSuffixId:
description: Suffix used to differentiate between applications. This is meant to be used when a single deployment to a single environment requires multiple builds (i.e. b2c + b2b). Defaults to `main`.
type: string
required: false
default: "main"
environment:
description: The github environment to inherit secrets from
type: string
required: true
additionalEnv:
description: Additional environment variables to append to the .env file (be sure to escape double-quotes)
type: string
required: false
nodeVersion:
description: Version of node used by yarn to build the application
type: string
required: false
default: "20"
limitSsg:
description: Limit static site generation
type: boolean
required: false
default: false
checkoutRef:
description: Reference to use in checkout (see 'ref' parameter in https://github.com/actions/checkout)
type: string
required: false
default: ""
runner:
description: Github Action runner to use. Defaults to 'ubuntu-latest'
type: string
required: false
default: "ubuntu-latest"
jobs:
build-artifact:
name: Build deployment artifact
runs-on: ${{ inputs.runner }}
environment: ${{ inputs.environment }}
steps:
- uses: actions/[email protected]
with:
ref: ${{ inputs.checkoutRef }}
- name: Node.js
uses: actions/[email protected]
with:
node-version: ${{ inputs.nodeVersion }}
cache: "yarn"
- name: Restore cache
uses: actions/[email protected]
with:
path: ${{ github.workspace }}/.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('yarn.lock') }}
restore-keys: ${{ runner.os }}-nextjs-${{ hashFiles('yarn.lock') }}-
- name: Dependencies
run: yarn install --frozen-lockfile --immutable
- name: Generate environment configuration
env:
ALL_VARS: ${{ toJSON(vars) }}
ALL_SECRETS: ${{ toJSON(secrets) }}
run: |
touch .env
# additionalEnv input is the legacy way to override configuration variables
echo "${{ inputs.additionalEnv }}" >> .env
echo "GC_LIMIT_SSG=${{ inputs.limitSsg }}" >> .env
# Read repository variables and secrets that are GraphCommerce specific, and write them to the .env file. We
# use the `toJSON` github action expression to fetch all variables in one go, and pipe them through `jq` to
# turn them back into a .env compatible format.
echo $ALL_VARS | { jq -r '.|with_entries(select(.key|startswith("GC_")))|to_entries|map("\(.key)=\"\(.value|tostring)\"")|.[]' || true; } >> .env
echo $ALL_SECRETS | { jq -r '.|with_entries(select(.key|startswith("GC_")))|to_entries|map("\(.key)=\"\(.value|tostring)\"")|.[]' || true; } >> .env
- name: Build
env:
GITHUB_SHA: ${{ github.sha }}
run: |
echo $(node -v)
export NODE_OPTIONS="--max_old_space_size=6144"
NEXT_PRIVATE_STANDALONE=1 yarn build
# Allow overriding bind address using VHOSTIP or HOSTNAME environment variables
sed -i '/server.listen(currentPort, (err) => {/c\server.listen(currentPort, process.env.HOSTNAME || process.env.VHOSTIP || "0.0.0.0", (err) => {' .next/standalone/server.js
# For newer versions of next, we need to override the bind address differently (HOSTNAME is now also supported by default)
sed -i 's/process.env.HOSTNAME ||/process.env.VHOSTIP || process.env.HOSTNAME ||/g' .next/standalone/server.js
# add http-graceful-shutdown and health check if exists
[ -d ./node_modules/http-graceful-shutdown ] && echo "" >> .next/standalone/server.js
[ -d ./node_modules/http-graceful-shutdown ] && echo "require('http-graceful-shutdown')(server)" >> .next/standalone/server.js
[ -d ./node_modules/http-graceful-shutdown ] && echo "require('./.next/server/pages/api/health').then((m) => m.default())" >> .next/standalone/server.js
[ -f ./pages/api/health.ts ] && echo "" >> .next/standalone/server.js
[ -f ./pages/api/health.ts ] && echo "Promise.resolve(require('./.next/server/pages/api/health')).then((m) => m.default())" >> .next/standalone/server.js
cp -R public .next/standalone/public
cp ecosystem.config.js .next/standalone/
cp ${{ inputs.ecosystemFile }} .next/standalone/
cp -R .next/static .next/standalone/.next/static
tar -czf "${GITHUB_SHA}_${{ inputs.applicationSuffixId }}".tar.gz -C .next/standalone .
- name: Node version
run: echo "$(node -v)"
shell: bash
- name: Upload artifact
uses: actions/[email protected]
with:
name: deployment-artifact-${{ inputs.applicationSuffixId }}
path: ${{ github.sha }}_${{ inputs.applicationSuffixId }}.tar.gz