-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
179 lines (162 loc) · 6.9 KB
/
action.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
178
179
name: "Publish to Cloudflare Pages"
description: "Publish a Pages project"
inputs:
build-script:
description: "The script to build your site (default: `build`) (joins with `yarn workspace $workspace-name`)"
required: false
default: "build"
cloudflare-account-id:
description: "Cloudflare Account ID"
required: true
cloudflare-api-token:
description: "Cloudflare API Token"
required: true
directory:
description: "The directory of static assets to upload (default: `dist`)"
required: true
default: "dist"
github-token:
description: "GitHub Token"
required: true
prebuild-script:
description: "The script to run before building your site"
required: false
production-branch:
description: "The branch to deploy to production"
required: true
default: "master"
project-name:
description: "The name of the Cloudflare Pages project"
required: true
workspace-name:
description: "The name of the yarn workspace being deployed"
required: true
deploy-to-branch:
description: "The Cloudflare Pages branch to deploy to"
required: false
runs:
using: "composite"
steps:
- name: Install node.js v18
uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
uses: trufflehq/truffle-packages/actions/yarn@main
- name: Get project directory
id: get-project-directory
shell: bash
run: |
set -ex
dir=$(yarn workspaces list --json | jq -r 'select(.name == "${{ inputs.workspace-name }}") | .location')
echo "Resolved project directory: $dir"
echo "dir=$dir" >> $GITHUB_OUTPUT
- name: Prebuild script
shell: bash
if: ${{ inputs.prebuild-script }}
run: ${{ inputs.prebuild-script }}
- name: Turbo prune
shell: bash
run: yarn turbo prune --scope=${{ inputs.workspace-name }}
- name: Build script
shell: bash
if: ${{ inputs.build-script }}
run: yarn turbo run ${{ inputs.build-script }} --filter=${{ inputs.workspace-name }}
- name: Check if Cloudflare Pages Project exists
shell: bash
id: check-project
run: |
set -ex
echo "Checking if Cloudflare Pages Project exists"
check=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/${{ inputs.cloudflare-account-id }}/pages/projects/${{ inputs.project-name }}" \
-H "Authorization: Bearer ${{ inputs.cloudflare-api-token }}" \
-H "Content-Type:application/json" | jq -r '.success')
echo "result=$check" >> $GITHUB_OUTPUT
- name: Create Cloudflare Pages Project (if it doesn't exist)
shell: bash
if: steps.check-project.outputs.result != 'true'
run: |
set -ex
echo "Creating Cloudflare Pages Project exists"
curl -s -X POST "https://api.cloudflare.com/client/v4/accounts/${{ inputs.cloudflare-account-id }}/pages/projects" \
-H "Authorization: Bearer ${{ inputs.cloudflare-api-token }}" \
-H "Content-Type:application/json" \
--data '{"name":"${{ inputs.project-name }}", "production_branch":"${{ inputs.production-branch }}"}'
- name: Create GitHub Deployment
uses: actions/github-script@v4
id: create_github_deployment
env:
PAGES_BRANCH: ${{ inputs.deploy-to-branch || inputs.production-branch }}
PROJECT_NAME: ${{ inputs.project-name }}
PRODUCTION_BRANCH: ${{ inputs.production-branch }}
with:
github-token: ${{ inputs.github-token }}
script: |
const githubBranch = process.env.GITHUB_HEAD_REF || process.env.GITHUB_REF_NAME;
const deployment = await github.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: githubBranch || context.ref,
auto_merge: false,
description: "Cloudflare Pages",
required_contexts: [],
environment: `${process.env.PROJECT_NAME} (${process.env.PAGES_BRANCH})`,
production_environment: process.env.PAGES_BRANCH === process.env.PRODUCTION_BRANCH,
});
if (deployment.status === 201) {
core.setOutput('id', deployment.data.id);
return deployment.data;
} else {
throw new Error(`Failed to create deployment: ${deployment.status}`);
}
- name: Deploy
shell: bash
env:
PAGES_PROJECT_NAME: ${{ inputs.project-name }}
PAGES_BRANCH: ${{ inputs.deploy-to-branch || inputs.production-branch }}
CLOUDFLARE_ACCOUNT_ID: ${{ inputs.cloudflare-account-id }}
CLOUDFLARE_API_TOKEN: ${{ inputs.cloudflare-api-token }}
WORKING_DIRECTORY: ${{ steps.get-project-directory.outputs.dir }}
DIRECTORY: ${{ inputs.directory }}
run: |
cd $WORKING_DIRECTORY
npx wrangler@3 pages deploy "$DIRECTORY" \
--project-name="$PAGES_PROJECT_NAME" \
--branch="$PAGES_BRANCH"
- name: Get Latest Deployment ID
shell: bash
id: get_deployment
env:
PAGES_PROJECT_NAME: ${{ inputs.project-name }}
CLOUDFLARE_ACCOUNT_ID: ${{ inputs.cloudflare-account-id }}
CLOUDFLARE_API_TOKEN: ${{ inputs.cloudflare-api-token }}
run: |
deployment=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$PAGES_PROJECT_NAME/deployments" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type:application/json" | jq -r '.result[0]')
echo "id=$(echo $deployment | jq -r .id)" >> $GITHUB_OUTPUT
echo "url=$(echo $deployment | jq -r .url)" >> $GITHUB_OUTPUT
- name: Create GitHub Deployment Status
uses: actions/github-script@v4
env:
PAGES_BRANCH: ${{ inputs.deploy-to-branch || inputs.production-branch }}
PROJECT_NAME: ${{ inputs.project-name }}
PRODUCTION_BRANCH: ${{ inputs.production-branch }}
CLOUDFLARE_ACCOUNT_ID: ${{ inputs.cloudflare-account-id }}
CLOUDFLARE_API_TOKEN: ${{ inputs.cloudflare-api-token }}
with:
github-token: ${{ inputs.github-token }}
script: |
const { CLOUDFLARE_ACCOUNT_ID, PAGES_PROJECT_NAME } = process.env;
let deploymentId = "${{ steps.get_deployment.outputs.id }}";
let environmentUrl = "${{ steps.get_deployment.outputs.url }}";
const deploymentStatus = await github.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: "${{ steps.create_github_deployment.outputs.id }}",
state: 'success',
log_url: `https://dash.cloudflare.com/${CLOUDFLARE_ACCOUNT_ID}/pages/view/${PAGES_PROJECT_NAME}/${deploymentId}`,
description: 'Cloudflare Pages',
environment_url: environmentUrl,
environment: `${process.env.PROJECT_NAME} (${process.env.PAGES_BRANCH})`,
});