Merge pull request #901 from dangdangwalk/backend/DANG-1268 #472
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: Backend Deploy | |
on: | |
push: | |
branches: [main] | |
paths: | |
- "backend/server/**" | |
env: | |
IMAGE: dangdangwalk | |
REPO: 533267282498.dkr.ecr.ap-northeast-2.amazonaws.com | |
TAG: ${GITHUB_SHA::7} | |
jobs: | |
build: | |
name: ECR Build | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: ./backend/ | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
- name: Cache dependencies | |
id: cache | |
uses: actions/cache@v3 | |
with: | |
path: backend/server/node_modules | |
key: | | |
${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('backend/server/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('backend/server/package-lock.json') }} | |
- name: Install Dependencies | |
if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }} | |
working-directory: ./backend/server | |
run: npm ci | |
- name: Run build | |
working-directory: ./backend/server | |
run: npm run build | |
- 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: ap-northeast-2 | |
- name: ECR Login | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: ECR Delete | |
run: | | |
IMAGE_TAG=$(aws ecr describe-images --repository-name ${{ env.IMAGE }} | jq '.imageDetails[].imageTags[0]') | |
if [[ -z "$IMAGE_TAG" ]]; then | |
echo "No images found in the repository." | |
else | |
aws ecr batch-delete-image --repository-name ${{ env.IMAGE }} --image-ids imageTag=$IMAGE_TAG | |
fi | |
- name: Docker build | |
run: | | |
docker build --platform linux/amd64 -t ${{ env.IMAGE }} --target prod -f server/Dockerfile . | |
- name: ECR push | |
run: | | |
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin ${{ env.REPO }} | |
docker tag ${{ env.IMAGE }} ${{ env.REPO }}/${{ env.IMAGE }}:${{ env.TAG }} | |
docker push ${{ env.REPO }}/${{ env.IMAGE}}:${{ env.TAG }} | |
deploy: | |
name: ECR Deploy | |
needs: build | |
runs-on: [self-hosted, label-api] | |
steps: | |
- name: Create .env | |
env: | |
ENV_FILE: ${{ secrets.ENV_FILE_BACKEND }} | |
run: | | |
if [ ! -d "backend/server" ]; then | |
mkdir backend/server; | |
fi | |
echo "$ENV_FILE" > ./backend/server/.env.prod | |
- name: Deploy to Amazon EC2 | |
run: | | |
chmod 777 /home/ubuntu/run-backend.sh | |
/home/ubuntu/run-backend.sh ${{ env.TAG }} .env.prod ${{ env.REPO }} ${{ env.IMAGE }} |