forked from igorek17000/prepo-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
177 lines (154 loc) · 5.57 KB
/
deploy-preview-apps.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
name: Deploy previews
on:
pull_request:
types: [opened, synchronize]
workflow_run:
workflows: [Checks]
types:
- completed
jobs:
get-branch-name:
name: Get current branch name
runs-on: ubuntu-latest
outputs:
branch_name: ${{ steps.set-branch-name.outputs.branch_name }}
steps:
- name: Extract branch name
id: set-branch-name
if: github.event_name == 'pull_request'
run: |
branch_name=${GITHUB_HEAD_REF}
echo $branch_name
echo "::set-output name=branch_name::$branch_name"
generate-artifact-key:
name: Generate artifact key
runs-on: ubuntu-latest
outputs:
artifact: ${{ steps.set-artifact-key.outputs.artifact }}
steps:
- name: Generate artifact key
id: set-artifact-key
# Replace possible / on the string to -
run: |
github_head_ref="${{ github.head_ref }}"
echo $github_head_ref
artifact=$(echo "${github_head_ref}" | sed 's/\//\-/g')
echo $artifact
echo "::set-output name=artifact::$artifact"
get-github-context:
name: Get Github Context
runs-on: ubuntu-latest
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
steps:
- name: Print pull request sha
run: echo ${{ github.event.pull_request.head.sha }}
- name: Print github context
run: echo "$GITHUB_CONTEXT"
get-changed-apps:
name: Get changed apps
needs: [get-branch-name, get-github-context, generate-artifact-key]
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: 'Checkout'
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: 'Restore node_modules cache'
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
- name: 'Use NodeJS 16'
uses: actions/setup-node@v2
with:
node-version: '16'
- name: 'Setup npm'
run: |
npm set @prepo-io:registry=https://npm.pkg.github.com/prepo-io
npm set "//npm.pkg.github.com/:_authToken=${{ secrets.PREPO_PACKAGES_TOKEN }}"
- name: Install Dependencies
run: |
yarn install --frozen-lockfile
- name: Turbo Cache
id: turbo-cache
uses: actions/cache@v2
with:
path: node_modules/.cache
key: turbo-${{ github.job }}-${{ github.ref_name }}-${{ github.sha }}
restore-keys: |
turbo-${{ github.job }}-${{ github.ref_name }}-
- name: Run changed apps script
run: FILTER_BRANCH=${GITHUB_HEAD_REF} yarn run changed:ci
- name: Upload changed apps artifact
uses: actions/upload-artifact@v2
with:
name: changed-apps-${{ needs.generate-artifact-key.outputs.artifact }}
path: changed_apps.json
- name: Set matrix value
id: set-matrix
run: |
matrix="{\"include\":$(cat changed_apps.json | jq -c -r .)}"
echo $matrix
echo $matrix | jq .
echo "::set-output name=matrix::$matrix"
check-matrix-and-cache:
runs-on: ubuntu-latest
needs: [get-changed-apps, generate-artifact-key]
if: github.event_name == 'pull_request'
steps:
- name: Install json2yaml
run: |
sudo npm install -g json2yaml
- name: Check matrix definition
run: |
matrix='${{ needs.get-changed-apps.outputs.matrix }}'
echo $matrix
echo $matrix | jq .
echo $matrix | json2yaml
if [[ $matrix != '' ]] ; then echo "Matrix is not empty string!" ; else echo "Matrix is empty string!" ; fi
if [[ $matrix != '[]' ]] ; then echo "Matrix is not empty array!" ; else echo "Matrix is empty array!" ; fi
if [[ (needs.get-changed-apps.outputs.matrix != '' && needs.get-changed-apps.outputs.matrix != '[]') ]] ; then echo "Should run pipeline" ; else echo "Should not run pipeline" ; fi
- name: Download changed apps artifact
uses: actions/download-artifact@v2
with:
name: changed-apps-${{ needs.generate-artifact-key.outputs.artifact }}
- name: Print changed apps cache
run: cat changed_apps.json | jq -c -r .
deploy-apps:
name: Deploy apps
needs: get-changed-apps
runs-on: ubuntu-latest
if: |
(needs.get-changed-apps.outputs.matrix != '' && needs.get-changed-apps.outputs.matrix != '[]')
strategy:
fail-fast: false
matrix: ${{fromJson(needs.get-changed-apps.outputs.matrix)}}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Deploy to Vercel Action
id: vercel-deploy
uses: BetaHuhn/deploy-to-vercel-action@v1
with:
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ matrix.projectId }}
CREATE_COMMENT: false
- uses: phulsechinmay/rewritable-pr-comment@v0.3.0
if: ${{ steps.vercel-deploy.outputs.DEPLOYMENT_CREATED }}
with:
message: |
App <strong>${{ matrix.name }}</strong> has been deployed to Vercel.
<table>
<tr>
<td><strong>✅ Preview:</strong></td>
<td><a href='${{ steps.vercel-deploy.outputs.PREVIEW_URL }}'>${{ steps.vercel-deploy.outputs.PREVIEW_URL }}</a></td>
</tr>
</table>
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
COMMENT_IDENTIFIER: ${{ matrix.projectId }}