CodeDeploy Test #7
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: Install dependencies | |
run: npm install | |
- name: Create .env file | |
run: | | |
echo "NEXT_PUBLIC_PROD_API_URL=${{ secrets.NEXT_PUBLIC_PROD_API_URL }}" > .env | |
- name: Build Next.js app | |
run: npm run build | |
- name: Make zip file | |
run: zip -qq -r nextjs-app-${{ github.sha }}.zip .next node_modules package.json | |
- 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: 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 |