Skip to content

fixing docker file 11 #78

fixing docker file 11

fixing docker file 11 #78

Workflow file for this run

name: Deploy
on:
push:
branches: [ "main" ]
jobs:
build:
name: Build Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- 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: Build and push image to ECR
env:
ECR_REGISTRY: ${{ steps.login-aws-ecr.outputs.registry }}
ECR_REPOSITORY: ${{secrets.AWS_ECR_REPO}}
IMAGE_TAG: latest
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
deploy:
name: Deploy to EC2
runs-on: ubuntu-latest
needs: build
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.CONTAINER_NAME }} || true",
"docker pull ${{ steps.login-aws-ecr.outputs.registry }}/${{secrets.AWS_ECR_REPO}}:latest",
"docker run --rm -d -p 3000:3000 -e DB_HOST=${{ secrets.DB_HOST}} -e DB_USERNAME=${{ secrets.DB_USERNAME}} -e DB_PASSWORD=${{ secrets.DB_PASSWORD}} -e ENVIRONMENT=production --name ${{ vars.CONTAINER_NAME }} ${{steps.login-aws-ecr.outputs.registry}}/${{secrets.AWS_ECR_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 "Command executed successfully"
else
echo "Command failed"
exit 1
fi