v1.0.1 #27
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy Next.js App | |
on: | |
push: | |
branches: | |
- main | |
env: | |
AWS_REGION: ap-northeast-2 | |
S3_BUCKET_NAME: sns-client-bucket | |
CODE_DEPLOY_APPLICATION_NAME: sns-client-code-deploy | |
CODE_DEPLOY_GROUP_NAME: ec2-deploy | |
jobs: | |
deploy: | |
name: Deploy to EC2 | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
- name: Cache pnpm | |
uses: actions/cache@v3 | |
with: | |
path: ~/.npm-global | |
key: ${{ runner.os }}-pnpm-cache | |
restore-keys: | | |
${{ runner.os }}-pnpm-cache | |
- name: Install pnpm | |
run: | | |
if ! command -v pnpm &> /dev/null; then | |
npm install -g pnpm | |
fi | |
- name: Install dependencies | |
run: pnpm install --frozen-lockfile | |
- name: Create .env file | |
run: | | |
echo "NEXT_PUBLIC_PROD_API_URL=${{ secrets.NEXT_PUBLIC_PROD_API_URL }}" > .env | |
- name: Build Next.js app | |
run: pnpm build | |
- name: Make zip file | |
run: | | |
mkdir -p build/standalone/.next/static | |
mkdir -p build/standalone/public | |
mkdir -p build/standalone/scripts | |
tar -cf - -C .next/standalone . | tar -xf - -C build/standalone/ | |
tar -cf - -C .next/static . | tar -xf - -C build/standalone/.next/static/ | |
tar -cf - -C public . | tar -xf - -C build/standalone/public/ | |
cp scripts/* build/standalone/scripts/ | |
cp appspec.yml build/standalone | |
cp .env build/standalone/ | |
cp ecosystem.config.js build/standalone/ | |
cd build | |
zip -qq -r ../nextjs-app-${{ github.sha }}.zip . | |
shell: bash | |
- name: Clean old files from S3 | |
run: | | |
aws s3 ls s3://${{ env.S3_BUCKET_NAME }}/ | awk '{print $4}' | grep 'nextjs-app-' | while read file; do | |
aws s3 rm s3://${{ env.S3_BUCKET_NAME }}/$file; | |
done | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_REGION: ${{ env.AWS_REGION }} | |
- name: Upload to S3 | |
run: | | |
aws s3 cp nextjs-app-${{ github.sha }}.zip s3://${{ env.S3_BUCKET_NAME }}/nextjs-app-${{ github.sha }}.zip | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_REGION: ${{ env.AWS_REGION }} | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
- name: Deploy via AWS CLI | |
run: | | |
aws deploy create-deployment \ | |
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ | |
--deployment-config-name CodeDeployDefault.AllAtOnce \ | |
--deployment-group-name ${{ env.CODE_DEPLOY_GROUP_NAME }} \ | |
--s3-location bucket=${{ env.S3_BUCKET_NAME }},bundleType=zip,key=nextjs-app-${{ github.sha }}.zip |