forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
149 lines (126 loc) · 5.24 KB
/
Copy pathpreview-deployment.yml
File metadata and controls
149 lines (126 loc) · 5.24 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
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
name: Preview Deployment for PRs
on:
pull_request:
types: [opened, synchronize, reopened]
pull_request_review:
types: [submitted]
jobs:
build-and-deploy-preview:
runs-on: ubuntu-latest
# Skip drafts and runs where Heroku is not configured
if: ${{ github.event.pull_request.draft == false && secrets.HEROKU_API_KEY != '' }}
steps:
- name: Checkout code
uses: actions/checkout@v7
with:
ref: ${{ github.head_ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: 20
cache: npm
cache-dependency-path: novaRewards/package-lock.json
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Generate preview URL
id: preview-url
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
PREVIEW_URL="pr-${PR_NUMBER}-nova-rewards-staging.herokuapp.com"
echo "url=${PREVIEW_URL}" >> $GITHUB_OUTPUT
- name: Build backend
working-directory: ./backend
run: |
NEXT_PUBLIC_API_URL=https://api-pr-${{ github.event.pull_request.number }}-nova-rewards.herokuapp.com \
NEXT_PUBLIC_STELLAR_NETWORK=testnet \
NEXT_PUBLIC_HORIZON_URL=https://horizon-testnet.stellar.org \
NEXT_PUBLIC_ISSUER_PUBLIC=GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \
npm run build
- name: Create Heroku app if not exists
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
run: |
PREVIEW_APP="pr-${{ github.event.pull_request.number }}-nova-rewards"
if ! heroku apps:info "${PREVIEW_APP}" --exit-code 2>/dev/null; then
heroku apps:create "${PREVIEW_APP}" \
--region us \
--stack heroku-22 || true
fi
- name: Deploy to Heroku
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
run: |
PREVIEW_APP="pr-${{ github.event.pull_request.number }}-nova-rewards"
git remote add heroku https://git.heroku.com/${PREVIEW_APP}.git
git push heroku HEAD:main --force || true
- name: Configure preview environment
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
run: |
PREVIEW_APP="pr-${{ github.event.pull_request.number }}-nova-rewards"
heroku config:set \
NODE_ENV=staging \
DATABASE_URL=${{ secrets.STAGING_DATABASE_URL }} \
REDIS_URL=${{ secrets.STAGING_REDIS_URL }} \
--app "${PREVIEW_APP}" || true
- name: Run database migrations on preview
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
run: |
PREVIEW_APP="pr-${{ github.event.pull_request.number }}-nova-rewards"
heroku run "npm run db:migrate" --app "${PREVIEW_APP}" --exit-code || true
- name: Seed preview database
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
run: |
PREVIEW_APP="pr-${{ github.event.pull_request.number }}-nova-rewards"
heroku run "npm run db:seed:preview" --app "${PREVIEW_APP}" --exit-code || true
- name: Post deployment comment
continue-on-error: true
if: always()
uses: actions/github-script@v9
with:
script: |
const pr = context.payload.pull_request;
const prNumber = pr.number;
const apiUrl = `https://api-pr-${prNumber}-nova-rewards.herokuapp.com`;
const commentBody = `## 🚀 Preview Deployment Ready
✅ Your preview environment is ready for testing!
- **API**: [${apiUrl}](${apiUrl})
- **PR Number**: ${prNumber}
### Test Credentials
- Email: \`testuser1@test.com\`
- Password: \`TestPassword123!\`
### Notes
- The preview environment uses the staging database with seed data
- Changes are deployed automatically on new commits
- Preview environment expires 7 days after PR closes
Happy testing! 🎉`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody,
});
cleanup-preview:
runs-on: ubuntu-latest
if: ${{ github.event.action == 'closed' && secrets.HEROKU_API_KEY != '' }}
steps:
- name: Destroy preview app
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
run: |
PREVIEW_APP="pr-${{ github.event.pull_request.number }}-nova-rewards"
heroku apps:destroy "${PREVIEW_APP}" --confirm "${PREVIEW_APP}" || true
- name: Post cleanup comment
continue-on-error: true
uses: actions/github-script@v9
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '🧹 Preview deployment has been cleaned up.',
});