adjusted logging level to check production issues #38
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: Build, Deploy, and Run on EC2 | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
build-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set execute permissions for Maven Wrapper | |
run: chmod +x ./video-downloader-backend/mvnw | |
- name: Set up JDK | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Build backend with Maven | |
run: | | |
./mvnw clean package -DskipTests | |
working-directory: ./video-downloader-backend | |
# Log in to Amazon ECR | |
- name: Log in to Amazon ECR | |
env: | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
run: | | |
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com | |
# Build and Push Docker images to ECR | |
- name: Build and Push Docker images | |
env: | |
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }} | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
run: | | |
docker build -t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/video-downloader-backend ./video-downloader-backend | |
docker build -t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/video-downloader-frontend ./video-downloader-frontend | |
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/video-downloader-backend | |
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/video-downloader-frontend | |
# Deploy Docker images to EC2 | |
- name: Deploy Docker images to EC2 | |
env: | |
HOST: ${{ secrets.AWS_EC2_HOST }} | |
USER: ${{ secrets.AWS_EC2_USER }} | |
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }} | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
run: | | |
echo "${{ secrets.AWS_EC2_PEM }}" > /tmp/key.pem | |
chmod 600 /tmp/key.pem | |
mkdir -p ~/.ssh | |
echo -e "Host *\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config | |
# SSH into EC2 and pull the images from ECR | |
ssh -i /tmp/key.pem "$USER@$HOST" <<EOF | |
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com | |
docker network create my-app-network || true | |
docker stop backend frontend || true | |
docker rm backend frontend || true | |
docker pull $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/video-downloader-backend | |
docker pull $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/video-downloader-frontend | |
docker run -d --network=my-app-network --name backend -p 8080:8080 $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/video-downloader-backend | |
docker run -d --network=my-app-network --name frontend -p 80:80 $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/video-downloader-frontend | |
EOF |