-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview.yml
148 lines (123 loc) · 4.93 KB
/
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
144
145
146
147
148
name: Vercel Preview
permissions:
pull-requests: write
contents: read
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
on:
pull_request:
types: [opened, synchronize, reopened]
concurrency:
group: deploy-preview-${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
Deploy-Preview:
runs-on: ubuntu-latest
env:
VERCEL_[PROJECT_NAME]_PROJECT_ID: ${{ secrets.VERCEL_[PROJECT_NAME]_PROJECT_ID }}
VERCEL_[PROJECT_NAME_2]_PROJECT_ID: ${{ secrets.VERCEL_[PROJECT_NAME_2]_PROJECT_ID }}
strategy:
matrix:
include:
- name: "[PROJECT_NAME]"
env_var: "VERCEL_APP_PROJECT_ID"
path: '[path_to_project]'
- name: "[PROJECT_NAME_2]"
env_var: "VERCEL_KNOWS_PROJECT_ID"
path: '[path_to_project_2]'
outputs:
deployment_urls: ${{ toJSON(steps.collect-urls.outputs) }}
steps:
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9.11.0
run_install: false
- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v4
name: Restore pnpm store directory
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- uses: actions/checkout@v2
- name: Install Vercel CLI
run: pnpm install --global vercel@37.8.0
- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }} --scope=[org_name]
env:
VERCEL_PROJECT_ID: ${{ env[matrix.env_var] }}
- name: Build Project Artifacts
run: vercel build --token=${{ secrets.VERCEL_TOKEN }} --scope=[org_name]
env:
VERCEL_PROJECT_ID: ${{ env[matrix.env_var] }}
- name: Deploy Project Artifacts to Vercel
id: deploy
run: |
URL=$(vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }} --scope=[org_name] --skip-domain)
TIME=$(date -u "+%b %-d, %Y %-I:%M%P")
echo "url=$URL" >> $GITHUB_OUTPUT
echo "time=$TIME" >> $GITHUB_OUTPUT
env:
VERCEL_PROJECT_ID: ${{ env[matrix.env_var] }}
- name: Find Existing Comment
uses: peter-evans/find-comment@v2
id: find-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: Preview Deployments
token: ${{ secrets.GITHUB_TOKEN }}
- name: Download Existing Deployments
uses: actions/download-artifact@v3
continue-on-error: true
with:
name: deployment-data
path: ./existing-urls
- name: Update Deployment Data
id: update-data
run: |
mkdir -p ./merged-urls
# Copy existing data (if it exists)
if [ -f "./existing-urls/deployments.json" ]; then
cp "./existing-urls/deployments.json" "./merged-urls/deployments.json"
else
echo "{}" > "./merged-urls/deployments.json"
fi
# Update current deployment data
current_data=$(jq --arg name "${{ matrix.name }}" \
--arg url "${{ steps.deploy.outputs.url }}" \
--arg time "${{ steps.deploy.outputs.time }}" \
'. + {($name): {"url": $url, "time": $time}}' \
"./merged-urls/deployments.json")
echo "$current_data" > "./merged-urls/deployments.json"
# Generate table rows
{
echo "table_rows<<EOF"
echo "$current_data" | jq -r 'to_entries[] | "| **\(.key)** | \(.value.url) | \(.value.time) |"'
echo "EOF"
} >> $GITHUB_OUTPUT
# Reference: https://github.com/orgs/community/discussions/17245
# The better way to share outputs between jobs is to use artifacts
- name: Upload Updated Deployment Data
uses: actions/upload-artifact@v3
with:
name: deployment-data
path: ./merged-urls/deployments.json
retention-days: 1
- name: Update PR Comment
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
edit-mode: replace
body: |
## Preview Deployments 🚀
| Project | Deployment | Updated (UTC) |
|---------|------------|---------------|
${{ steps.update-data.outputs.table_rows }}
*Deployments are updated as they complete. Last update: ${{ steps.deploy.outputs.time }} UTC*