-
Notifications
You must be signed in to change notification settings - Fork 15
99 lines (78 loc) · 2.98 KB
/
aws-amplify.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
name: Deploy Next.js to AWS Amplify
on:
pull_request:
branches: [ "main" ]
paths:
- .github/workflows/aws-amplify.yml
- client/**
- assistant/**
env:
AWS_REGION: ${{ vars.AWS_REGION }}
NEXT_PUBLIC_API_DOMAIN: ${{ vars.NEXT_PUBLIC_API_DOMAIN }}
AMPLIFY_APP_ID: ${{ vars.AMPLIFY_APP_ID }}
S3_BUCKET: ${{ vars.S3_BUCKET }}
NEXT_STANDALONE: "true"
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cache node modules
uses: actions/cache@v2
with:
path: client/node_modules
key: ${{ runner.os }}-node-${{ hashFiles('client/**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::654654285942:role/Github-OIDC
audience: sts.amazonaws.com
aws-region: ${{ env.AWS_REGION }}
- name: Configure Amplify CLI
run: npm install -g @aws-amplify/cli
- name: Install Dependencies
run: npm install
working-directory: ./client # 仅在 ./client 目录下安装 Next.js 项目的依赖项
- name: Build Amplify App
run: npm run build
working-directory: ./client # 仅在 ./client 目录下构建 Next.js 应用程序
- name: Zip Build Folder
run: zip -r build.zip .next/standalone .next/static
working-directory: ./client
- name: Upload Build to S3
run: |
aws s3 cp ./client/build.zip s3://${{ env.S3_BUCKET }}/builds/${{ github.sha }}.zip
- name: Check if branch exists in Amplify
id: check_branch
run: |
branch_name="${{ github.head_ref }}"
app_id="${{ vars.AMPLIFY_APP_ID }}"
branch_exists=$(aws amplify list-branches --app-id $app_id --query "branches[?branchName=='$branch_name'].branchName" --output text)
if [ -z "$branch_exists" ]; then
echo "Branch $branch_name does not exist. Creating branch..."
aws amplify create-branch --app-id $app_id --branch-name $branch_name
else
echo "Branch $branch_name already exists."
fi
- name: Start Amplify deployment
run: |
branch_name="${{ github.head_ref }}"
app_id="${{ vars.AMPLIFY_APP_ID }}"
source_url="s3://${{ env.S3_BUCKET }}/builds/${{ github.sha }}.zip"
aws amplify start-deployment --app-id $app_id --branch-name $branch_name --source-url $source_url --output json > deployment.json
- name: Output deployment details
run: |
cat deployment.json