ssh take3 #11
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: CI/CD | |
on: | |
push: | |
branches: | |
- fixes/cleanup-1 | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to Amazon ECR | |
uses: aws-actions/amazon-ecr-login@v2 | |
env: | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
- name: Build and Push Backend Docker Image | |
env: | |
IMAGE_TAG: ${{ github.sha }} | |
run: | | |
BACKEND_IMAGE_URI="${{ secrets.BACKEND_ECR }}:${{ env.IMAGE_TAG }}" | |
docker build -t $BACKEND_IMAGE_URI -f ./Dockerfile ./ | |
docker push $BACKEND_IMAGE_URI | |
- name: Build and Push Frontend Docker Image | |
env: | |
IMAGE_TAG: ${{ github.sha }} | |
run: | | |
FRONTEND_IMAGE_URI="${{ secrets.FRONTEND_ECR }}:${{ env.IMAGE_TAG }}" | |
docker build -t $FRONTEND_IMAGE_URI -f ./frontend/Dockerfile ./frontend | |
docker push $FRONTEND_IMAGE_URI | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set up environment variables | |
run: | | |
echo "BACKEND_ECR=${{ secrets.BACKEND_ECR }}" >> $GITHUB_ENV | |
echo "FRONTEND_ECR=${{ secrets.FRONTEND_ECR }}" >> $GITHUB_ENV | |
echo "IMAGE_TAG=${{ github.sha }}" >> $GITHUB_ENV | |
- name: Deploy via SSH | |
run: | | |
echo "${{ secrets.EC2_SSH_KEY }}" > ec2_key.pem | |
chmod 600 ec2_key.pem | |
ssh -o StrictHostKeyChecking=no -i ec2_key.pem ec2-user@${{ secrets.EC2_INSTANCE_IP }} \ | |
"export BACKEND_ECR=${{ secrets.BACKEND_ECR }} && \ | |
export FRONTEND_ECR=${{ secrets.FRONTEND_ECR }} && \ | |
export IMAGE_TAG=${{ github.sha }} && \ | |
docker-compose pull && \ | |
docker-compose up -d" |