-
Notifications
You must be signed in to change notification settings - Fork 38
134 lines (115 loc) · 4.55 KB
/
pull_request_deploy_preview.yaml
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
name: "[Pull Request] Deploy Vercel Preview on Comment"
on:
issue_comment:
types: [created]
env:
VERCEL_TEAM_NAME: "cloudforet"
PROJECT_NAME: console
VERCEL_TOKEN: ${{ secrets.VERCEL_CLOUDFORET_TOKEN }}
jobs:
deploy-preview:
if: startsWith(github.event.comment.body, '/preview')
runs-on: ubuntu-latest
steps:
- name: Verify context
id: verify
run: |
if [[ "${{ github.event.issue.pull_request.url }}" == "" ]]; then
echo "🚫 This action only supports comments on PRs."
exit 1
fi
COMMENTER=${{ github.event.comment.user.login }}
echo "Commenter: $COMMENTER"
- name: Check organization membership
id: check_membership
uses: actions/github-script@v6
with:
script: |
const commenter = context.payload.comment.user.login;
try {
const { status } = await github.rest.orgs.checkMembershipForUser({
org: 'cloudforet-io',
username: commenter
});
if (status !== 204) {
throw new Error("User is not a member of the organization");
}
core.setOutput('is_member', true);
} catch (error) {
console.log(error.message);
core.setOutput('is_member', false);
core.setOutput('error_message', error.message);
}
result-encoding: string
- name: Comment and stop if not a member
if: steps.check_membership.outputs.is_member == 'false'
uses: actions/github-script@v6
with:
script: |
const errorMessage = `🚫 Commenter \`${context.payload.comment.user.login}\` is not a member of the organization and cannot trigger a preview deployment.`;
console.error(errorMessage);
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: errorMessage
});
throw new Error(errorMessage);
- name: Get PR information
id: pr_info
uses: actions/github-script@v6
with:
script: |
const prNumber = context.issue.number;
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
core.setOutput('reponame', pullRequest.head.repo.full_name);
core.setOutput('branch', pullRequest.head.ref);
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0
repository: ${{ steps.pr_info.outputs.reponame }}
ref: ${{ steps.pr_info.outputs.branch }}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- name: Install Vercel CLI
run: npm install -g vercel
- name: Set up environment
run: sudo apt-get install jq
- name: Deploy to Vercel
id: deploy
run: |
BRANCH_NAME=${{ steps.pr_info.outputs.branch }}
echo "Deploying preview for branch $BRANCH_NAME"
echo "DEPLOY_URL=$DEPLOY_URL" >> $GITHUB_ENV
- name: Checkout Vercel Target Project
run: vercel link --yes --scope=${{ env.VERCEL_TEAM_NAME }} --project=${{ env.PROJECT_NAME }} --token=${{ env.VERCEL_TOKEN }}
- name: Pull Vercel Environment Information
run: vercel pull --yes --token=${{ env.VERCEL_TOKEN }}
- name: Pull Vercel Edge Config
run: vercel env pull ./apps/web/.env --yes --token=${{ env.VERCEL_TOKEN }}
- name: Build Project Artifacts
run: vercel build --yes --token=${{ env.VERCEL_TOKEN }}
- name: Deploy Project Artifacts to Vercel
run: |
DEPLOY_URL=$(vercel deploy --prebuilt --token=${{ env.VERCEL_TOKEN }})
echo "DEPLOY_URL=$DEPLOY_URL" >> $GITHUB_ENV
- name: Comment on PR
uses: actions/github-script@v6
with:
script: |
const url = process.env.DEPLOY_URL;
const commentBody = `🚀 Preview deployed for branch \`${{ steps.pr_info.outputs.reponame }}:${{ steps.pr_info.outputs.branch }}\`: [View Preview](${url})`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
})