-
Notifications
You must be signed in to change notification settings - Fork 293
111 lines (99 loc) · 3.93 KB
/
add-preview-domain.yml
File metadata and controls
111 lines (99 loc) · 3.93 KB
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
name: Assign preview-domain
on:
# Trigger when a pull request is opened or reopened
pull_request:
types: [opened, reopened, ready_for_review, synchronize]
permissions:
contents: read
jobs:
comment-jira-ticket:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Comment with the jira ticket
uses: actions/github-script@v7.0.1
with:
script: |
const branch = `${{ github.head_ref }}`
const match = branch.match(/\b(MI|AS)-\d+\b/i);
const jiraTicket = match ? match[0].toUpperCase() : null;
if (!jiraTicket) {
console.log('No Jira ticket found')
return
}
console.log(`Found ticket ${jiraTicket}`)
const knownString = '### Jira ticket'
const pullRequest = await github.rest.pulls.get({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
})
const hasAlreadyCommented = pullRequest.data.body ? pullRequest.data.body.includes(knownString) : false
if (hasAlreadyCommented) {
console.log('Already commented once')
} else {
console.log('Creating comment for the first time')
const body = pullRequest.data.body
? `${pullRequest.data.body}\n\n${knownString}\n${jiraTicket}`
: `${knownString}\n${jiraTicket}`
await github.rest.pulls.update({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body,
})
}
assign-preview-domain:
runs-on: ubuntu-latest
permissions:
pull-requests: write
needs:
- comment-jira-ticket
steps:
- name: Prepare domain
id: prepare
uses: actions/github-script@v7.0.1
with:
script: |
const subdomain = `${{ github.head_ref }}`
.toLowerCase()
.slice(0, 32)
.replace(/[^a-z0-9-]/g, '-')
.replace(/^-+|-+$/g, '')
.replace(/-{2,}/g, '-')
const domain = `${subdomain}.${{ vars.PREVIEW_DOMAIN }}`
.toLowerCase()
.replaceAll("_","-")
core.setOutput('domain', domain)
- name: Comment with the assigned preview-domain
uses: actions/github-script@v7.0.1
with:
script: |
const knownString = '### Preview domain'
const pullRequest = await github.rest.pulls.get({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
})
const hasAlreadyCommented = pullRequest.data.body ? pullRequest.data.body.includes(knownString) : false
if (hasAlreadyCommented) {
console.log('Already commented once')
} else {
console.log('Creating comment for the first time')
const body = pullRequest.data.body
? `${pullRequest.data.body}\n\n${knownString}\nhttps://${{ steps.prepare.outputs.domain }}`
: `${knownString}\nhttps://${{ steps.prepare.outputs.domain }}`
await github.rest.pulls.update({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body,
})
}
- name: Assign preview-domain
run: |
curl -sX POST "https://vercel.com/api/v10/projects/${{ secrets.VERCEL_PROJECT_ID }}/domains?teamId=${{ secrets.VERCEL_TEAM_ID }}" \
-H "Authorization: Bearer ${{ secrets.VERCEL_BEARER_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{"name": "${{ steps.prepare.outputs.domain }}","gitBranch":"${{ github.head_ref }}"}'