-
Notifications
You must be signed in to change notification settings - Fork 2
220 lines (204 loc) · 9.74 KB
/
ci.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
name: Run CI Suite
on:
push:
branches: [main]
pull_request:
branches: [main]
concurrency:
group: ci-${{ github.ref_name=='main' && github.sha || github.ref_name }} # Cancel runs for previous version of PR, but not main (after merge). For hacked ternary, see https://github.com/actions/runner/issues/409#issuecomment-727565588
cancel-in-progress: true
jobs:
eslint:
runs-on: ubuntu-latest
timeout-minutes: 40
steps:
- uses: actions/checkout@v3
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
- uses: ./.github/workflows/setup-workspace
- run: pnpm run lint
- uses: EndBug/add-and-commit@v9
with:
message: 'chore: linter autofixes'
default_author: github_actions
percy-instructions:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Comment with percy instructions
uses: bubkoo/auto-comment@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
pullRequestOpened: >
# Percy Screenshots
In order to conserve our percy screenshot allowance, percy is not configured to run automatically. Please make sure the PR is ready and all other checks are passing, then start it manually:
1. Visit https://github.com/UCLALibrary/library-website-nuxt/actions/workflows/percy.yml
2. Click the 'Run workflow' button in the blue bar.
3. Select the correct branch for this PR and click 'Run workflow' again to confirm.
nuxt:
name: Nuxt build / Cypress tests / Netlify deploy
runs-on: ubuntu-latest
timeout-minutes: 40
steps:
- uses: actions/checkout@v3
- uses: ./.github/workflows/setup-workspace
- name: Cache built static site
uses: actions/cache@v3
with:
path: .output/public
key: nuxt-site-${{ github.sha }}
- name: Sets env vars for PR preview
run: |
echo "SITEMAP_HOST=deploy-preview-${{ github.event.pull_request.number }}-uclalibrary-test.netlify.app" >> $GITHUB_ENV
echo "ES_INDEX=${{secrets.ES_INDEX_TEST}}-deploy-preview" >> $GITHUB_ENV
echo "ES_ALIAS=${{secrets.ES_ALIAS_TEST}}-deploy-preview" >> $GITHUB_ENV
if: github.ref_name!='main'
- name: Sets env vars for main merge
run: |
echo "SITEMAP_HOST=${{ secrets.SITEMAP_URL_TEST }}" >> $GITHUB_ENV
echo "ES_INDEX=${{secrets.ES_INDEX_TEST}}" >> $GITHUB_ENV
echo "ES_ALIAS=${{secrets.ES_ALIAS_TEST}}" >> $GITHUB_ENV
if: github.ref_name=='main'
- run: pnpm run generate
env:
CRAFT_ENDPOINT: ${{ secrets.CRAFT_ENDPOINT }}
LIBCAL_ENDPOINT: ${{ secrets.LIBCAL_ENDPOINT }}
S3_BUCKET: 'https://static.library.ucla.edu/'
SITEMAP_HOST: ${{ env.SITEMAP_HOST }}
ES_URL: ${{ secrets.ES_URL }}
ESApiKey: ${{ secrets.ESApiKey }}
ES_READ_KEY: ${{ secrets.ES_READ_KEY_TEST }}
ES_WRITE_KEY: ${{ secrets.ES_WRITE_KEY_TEST }}
ES_ALIAS: ${{ env.ES_ALIAS }}-${{ github.event.pull_request.number }}
ES_INDEX_PREFIX: ${{secrets.ES_INDEX_PREFIX_TEST}}
ES_TEMP_INDEX_PREFIX_LIBGUIDES: ${{secrets.ES_TEMP_INDEX_PREFIX_LIBGUIDES_TEST}}
LIBGUIDES_ES_INDEX: ${{secrets.LIBGUIDES_ES_INDEX_TEST}}
# Step to generate the current date and time
- name: Set deployment date
id: set-deploy-date
run: echo "DEPLOY_DATE=$(date '+%Y-%m-%d %H:%M:%S %Z')" >> $GITHUB_ENV
- name: Install Netlify CLI globally
run: pnpm add -g netlify-cli
- name: Deploy to Netlify (preview)
if: "github.ref_name != 'main'"
run: |
for i in {1..3}; do
echo "Attempt $i to deploy to Netlify..."
deployOutput=$(netlify deploy --debug --dir=.output/public --site=$NETLIFY_SITE_ID --auth=$NETLIFY_AUTH_TOKEN --alias=deploy-preview-${{ github.event.pull_request.number }} --message="Date: ${{ env.DEPLOY_DATE }} | PR: #${{ github.event.pull_request.number }} | Commit: ${{ github.event.pull_request.head.sha }}")
if [ $? -eq 0 ]; then
echo "Deployment succeeded on attempt $i"
echo "Netlify deploy output: $deployOutput"
# Extract the deploy URL
deployUrl=$(echo "$deployOutput" | grep -o 'https://deploy-preview-[^ ]*')
echo "NETLIFY_DEPLOY_URL=$deployUrl" >> $GITHUB_ENV
# Construct a success message
deployMessage="🚀 Preview deployment available at: [Netlify Preview]($deployUrl)"
echo "$deployMessage"
# Set the deploy message as output for future steps
echo "deploy-message=$deployMessage" >> $GITHUB_OUTPUT
break
else
echo "Deployment failed on attempt $i with error: $deployOutput"
if [ $i -eq 3 ]; then
echo "Deployment failed after 3 attempts. Exiting..."
exit 1
fi
echo "Retrying in 10 seconds..."
sleep 10
fi
done
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
- name: Create Pull Request GitHub Deployment and Set Pull Request Deployment Status to Success
if: github.ref_name!='main'
id: create_deployment
uses: actions/github-script@v6
with:
script: |
const { data: deployment } = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.sha,
environment: 'pull_request-${{ github.event.pull_request.number }}', // Ensures each PR gets a unique environment
description: 'Pull Request deployment',
required_contexts: [],
auto_inactive: true // Ensure only one active deployment
});
const deployUrl = process.env.NETLIFY_DEPLOY_URL;
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.id,
state: 'success',
environment_url: deployUrl,
description: 'Deployment successful'
});
- name: Comment on Pull Request with Netlify URL
if: github.event_name == 'pull_request'
uses: actions/github-script@v6
with:
script: |
const deployUrl = process.env.NETLIFY_DEPLOY_URL;
const siteId = '${{ secrets.NETLIFY_SITE_ID }}';
const markdownComment = `🚀 Preview deployment available at: [Netlify Preview](${deployUrl})`;
// Function to find an existing comment with a specific identifier
async function findIssueComment(githubClient, siteId) {
const { data: comments } = await githubClient.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const commentIdentifier = `<!-- NETLIFY DEPLOY COMMENT GENERATED BY ACTIONS_NETLIFY - SITE ID: ${siteId} -->`;
return comments.find(comment => comment.body.includes(commentIdentifier));
}
const existingComment = await findIssueComment(github, siteId);
if (existingComment) {
// Update the existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: `${markdownComment}\n<!-- NETLIFY DEPLOY COMMENT GENERATED BY ACTIONS_NETLIFY - SITE ID: ${siteId} -->`
});
} else {
// Create a new comment
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `${markdownComment}\n<!-- NETLIFY DEPLOY COMMENT GENERATED BY ACTIONS_NETLIFY - SITE ID: ${siteId} -->`
});
}
- uses: cypress-io/github-action@v5
with:
start: pnpm dlx serve .output/public
wait-on: http://127.0.0.1:3000
wait-on-timeout: 110
env:
CRAFT_ENDPOINT: ${{ secrets.CRAFT_ENDPOINT }}
LIBCAL_ENDPOINT: ${{ secrets.LIBCAL_ENDPOINT }}
S3_BUCKET: 'https://static.library.ucla.edu/'
SITEMAP_HOST: ${{ env.SITEMAP_HOST }}
ESApiKey: ${{ secrets.ESApiKey }}
ES_READ_KEY: ${{ secrets.ES_READ_KEY_TEST }}
ES_WRITE_KEY: ${{ secrets.ES_WRITE_KEY_TEST }}
ES_ALIAS: ${{ secrets.ES_ALIAS_TEST }}
ES_INDEX_PREFIX: ${{ secrets.ES_INDEX_PREFIX_TEST }}
ES_TEMP_INDEX_PREFIX_LIBGUIDES: ${{secrets.ES_TEMP_INDEX_PREFIX_LIBGUIDES_TEST}}
LIBGUIDES_ES_INDEX: ${{secrets.LIBGUIDES_ES_INDEX_TEST}}
- name: Deploy to Netlify (merged)
if: github.ref_name=='main'
uses: nwtgck/actions-netlify@v3
with:
production-deploy: true
deploy-message: "Deploy to test on merge to main for commit ${{ github.sha }}: https://github.com/UCLALibrary/library-website-nuxt/commit/${{ github.sha }}"
github-token: ${{ secrets.GITHUB_TOKEN }}
publish-dir: .output/public
fails-without-credentials: true
github-deployment-environment: testing
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}