Skip to content

Commit edfcad9

Browse files
authored
Merge pull request #202 from vechain/mike/preview-environments
Initial preview environments
2 parents 0951ae9 + da5d1b2 commit edfcad9

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

.github/workflows/deploy-cloudfront.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
NEXT_PUBLIC_PRIVY_CLIENT_ID: ${{ secrets.NEXT_PUBLIC_PRIVY_CLIENT_ID }}
3030
NEXT_PUBLIC_DELEGATOR_URL: ${{ secrets.NEXT_PUBLIC_DELEGATOR_URL }}
3131
NEXT_PUBLIC_NETWORK_TYPE: 'main'
32+
AWS_REGION: eu-west-1
3233
steps:
3334
- name: Checkout
3435
uses: actions/checkout@v4
@@ -50,7 +51,7 @@ jobs:
5051
uses: aws-actions/configure-aws-credentials@v4
5152
with:
5253
role-to-assume: ${{ secrets.AWS_ACC_ROLE }}
53-
aws-region: eu-west-1
54+
aws-region: ${{ env.AWS_REGION }}
5455

5556
- name: Deploy to S3
5657
run: |
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Deploy Preview Environment
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
paths: ['examples/homepage/**', 'packages/vechain-kit/**', 'yarn.lock']
8+
9+
permissions:
10+
contents: read
11+
id-token: write
12+
pull-requests: write
13+
14+
jobs:
15+
deploy:
16+
runs-on: ubuntu-latest
17+
env:
18+
BASE_PATH: '/${{ github.event.pull_request.head.ref }}'
19+
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID: ${{ secrets.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID }}
20+
NEXT_PUBLIC_PRIVY_APP_ID: ${{ secrets.NEXT_PUBLIC_PRIVY_APP_ID }}
21+
NEXT_PUBLIC_PRIVY_CLIENT_ID: ${{ secrets.NEXT_PUBLIC_PRIVY_CLIENT_ID }}
22+
NEXT_PUBLIC_DELEGATOR_URL: ${{ secrets.NEXT_PUBLIC_DELEGATOR_URL }}
23+
NEXT_PUBLIC_NETWORK_TYPE: 'main'
24+
AWS_REGION: eu-west-1
25+
steps:
26+
- name: Checkout
27+
uses: actions/checkout@v4
28+
29+
- name: Build App
30+
env:
31+
NODE_OPTIONS: '--max-old-space-size=8192'
32+
run: |
33+
yarn install:all
34+
yarn build
35+
36+
- name: Fix permissions
37+
run: |
38+
chmod -c -R +rX "./examples/homepage/dist" | while read line; do
39+
echo "::warning title=Invalid file permissions automatically fixed::$line"
40+
done
41+
42+
- name: Configure AWS credentials
43+
uses: aws-actions/configure-aws-credentials@v4
44+
with:
45+
role-to-assume: ${{ secrets.AWS_ACC_ROLE }}
46+
aws-region: ${{ env.AWS_REGION }}
47+
48+
- name: Process Branch Name
49+
id: process-branch-name
50+
run: |
51+
# Convert to lowercase, remove special characters, and truncate to 20 characters
52+
sanitized_branch_name=$(echo "${{ github.event.pull_request.head.ref }}" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]//g' | cut -c1-20)
53+
echo "processedBranchName=$sanitized_branch_name" >> $GITHUB_OUTPUT
54+
55+
- name: Deploy to S3
56+
run: |
57+
aws s3 sync ./examples/homepage/dist s3://${{ secrets.AWS_PREVIEW_BUCKET_NAME }}/${{ steps.process-branch-name.outputs.processedBranchName }} --delete
58+
59+
- name: Cloudfront Invalidation
60+
run: |
61+
AWS_MAX_ATTEMPTS=10 aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_PREVIEW_CLOUDFRONT_DISTRIBUTION_ID }} --paths '/' '/*'
62+
63+
64+
- name: Create Deployment Comment
65+
if: github.event.action == 'opened'
66+
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
67+
with:
68+
issue-number: ${{ github.event.pull_request.number }}
69+
body: |
70+
# 🚀 Preview environment deployed!
71+
Preview URL: https://preview.vechainkit.vechain.org/${{ steps.process-branch-name.outputs.processedBranchName }}
72+
edit-mode: replace
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Destroy Preview Environment
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
paths: ['examples/homepage/**', 'packages/vechain-kit/**', 'yarn.lock']
8+
types: [closed]
9+
10+
permissions:
11+
contents: read
12+
id-token: write
13+
pull-requests: write
14+
15+
jobs:
16+
deploy:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Configure AWS credentials
20+
uses: aws-actions/configure-aws-credentials@v4
21+
with:
22+
role-to-assume: ${{ secrets.AWS_ACC_ROLE }}
23+
aws-region: eu-west-1
24+
25+
- name: Process Branch Name
26+
id: process-branch-name
27+
run: |
28+
# Convert to lowercase, remove special characters, and truncate to 20 characters
29+
sanitized_branch_name=$(echo "${{ github.event.pull_request.head.ref }}" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]//g' | cut -c1-20)
30+
echo "processedBranchName=$sanitized_branch_name" >> $GITHUB_OUTPUT
31+
32+
- name: Remove Preview Environment from S3
33+
run: |
34+
aws s3 rm s3://${{ secrets.AWS_PREVIEW_BUCKET_NAME }}/${{ steps.process-branch-name.outputs.processedBranchName }} --recursive
35+
36+
- name: Find Previous Deployment Comment
37+
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
38+
id: find-comment
39+
with:
40+
issue-number: ${{ github.event.pull_request.number }}
41+
comment-author: 'github-actions[bot]'
42+
body-includes: '🚀 Preview environment deployed!'
43+
44+
- name: Remove Deployment PR Comment
45+
run: gh api --method DELETE /repos/${{ github.repository }}/issues/comments/${{ steps.find-comment.outputs.comment-id }}
46+
env:
47+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)