From 9f211f44ea53ef5f5616b1c45132b8f19b52c396 Mon Sep 17 00:00:00 2001 From: Sarah Parker Date: Wed, 10 Jul 2024 08:28:44 -0400 Subject: [PATCH] added deploy frontend job --- .github/workflows/main.yaml | 61 +++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 390a5e3..b39c9f3 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -53,7 +53,7 @@ jobs: docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG deploy-api: - name: Deploy to EC2 + name: Deploy API to EC2 runs-on: ubuntu-latest needs: build-api steps: @@ -103,7 +103,64 @@ jobs: echo "$status" if [ "$status" == "Success" ]; then - echo "Command executed successfully" + echo "Your API is running" + else + echo "Command failed" + exit 1 + fi + +deploy-frontend: + name: Deploy Frontend to EC2 + runs-on: ubuntu-latest + needs: build-frontend + steps: + - name: Configure AWS ECR credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{secrets.AWS_REGION}} + - name: Login to Amazon ECR + id: login-aws-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Pull and Run Docker Image from ECR + run: | + command_id=$(aws ssm send-command \ + --document-name "AWS-RunShellScript" \ + --targets "Key=instanceids,Values=${{ secrets.INSTANCE_ID_1}}, ${{ secrets.INSTANCE_ID_2}}" \ + --comment "Pull Docker Image and run it" \ + --parameters commands='[ + "aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin ${{ steps.login-aws-ecr.outputs.registry }}", + "docker stop ${{ vars.FRONTEND_CONTAINER_NAME }} || true", + "docker pull ${{ steps.login-aws-ecr.outputs.registry }}/${{secrets.FRONTEND_IMAGE_REPO}}:latest", + "docker run --rm -d -p 80:80 --name ${{ vars.FRONTEND_CONTAINER_NAME }} ${{steps.login-aws-ecr.outputs.registry}}/${{secrets.FRONTEND_IMAGE_REPO}}:latest"]' --query "Command.CommandId" --output text) + + echo "command_id=$command_id" >> $GITHUB_ENV + + + - name: Wait for Docker Commands to Complete on Instance 1 + run: | + echo ${{ env.command_id }} + aws ssm wait command-executed \ + --command-id ${{ env.command_id }} \ + --instance-id ${{ secrets.INSTANCE_ID_1}} || true + + - name: Wait for Docker Commands to Complete on Instance 2 + run: | + echo ${{ env.command_id }} + aws ssm wait command-executed \ + --command-id ${{ env.command_id }} \ + --instance-id ${{ secrets.INSTANCE_ID_2}} || true + + - name: Check Command Status + run: | + status=$(aws ssm list-commands --command-id ${{ env.command_id }} --query "Commands[0].StatusDetails" --output text) + + echo "$status" + + if [ "$status" == "Success" ]; then + echo "YAY we did it! Your frontend is running" else echo "Command failed" exit 1