From 8e790bc68634302ede7e61cf193fdf6f6e6903d2 Mon Sep 17 00:00:00 2001 From: Sarah Abu irmeileh <127017088+SarahAbuirmeileh@users.noreply.github.com> Date: Tue, 24 Oct 2023 20:17:41 +0300 Subject: [PATCH] Update build.yml --- .github/workflows/build.yml | 48 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e1ae6cbf..43dc810b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,7 +21,7 @@ jobs: - name: Set image version (tag) id: image_version - run: echo "gitsha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + run: echo "gitsha=$(git rev-parse --short HEAD)" >> $GITHUB_ENV - name: login to Registry uses: docker/login-action@v3 @@ -32,36 +32,36 @@ jobs: - name: Build the Docker image run: docker build . --file Dockerfile --tag ghcr.io/sarahabuirmeileh/serve-and-shine:${{ steps.image_version.outputs.gitsha }} - - - name: Puplish docker image + + - name: Publish docker image run: docker push ghcr.io/sarahabuirmeileh/serve-and-shine:${{ steps.image_version.outputs.gitsha }} - - + - name: Configure AWS CLI run: aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} && aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} && aws configure set region ${{ secrets.AWS_REGION }} - + - name: Get EC2 instances from the Auto Scaling Group run: | - instances=$(aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=serve-and-shine-ASG-" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output text) + instances=$(aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=serve-and-shine-ASG-" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output text | tr -s '\t' ' ' | sed 's/ /,/g') echo "EC2_INSTANCES=$instances" >> $GITHUB_ENV - name: Deploy to EC2 instances run: | - instances=(${{ env.EC2_INSTANCES }}) + IFS=',' read -ra instances <<< "$EC2_INSTANCES" for instance in "${instances[@]}"; do - ssh ubuntu@$instance "docker pull ghcr.io/sarahabuirmeileh/serve-and-shine:${{ steps.image_version.outputs.gitsha }}" - ssh ubuntu@$instance "docker-compose -f /path/to/your/docker-compose-prod.yml up -d" - env: - DB_PASSWORD: ${{ secrets.DB_PASSWORD}} - GIT_VERSISON: ${{steps.image_version.outputs.gitsha}} - SECRET_KEY: ${{ secrets.SECRET_KEY}} - DB_HOST: ${{ secrets.DB_HOST}} - DB_PORT: ${{ secrets.DB_PORT}} - DB_USERNAME: ${{ secrets.DB_USERNAME}} - DB_NAME: ${{ secrets.DB_NAME}} - AWS_BUCKET_NAME: ${{ secrets.AWS_BUCKET_NAME}} - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID}} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY}} - AWS_REGION: ${{ secrets.AWS_REGION}} - AWS_CERTIFICATES_BUCKET_NAME: ${{ secrets.AWS_CERTIFICATES_BUCKET_NAME}} - PORT: ${{ secrets.PORT}} + ssh ubuntu@"$instance" "docker pull ghcr.io/sarahabuirmeileh/serve-and-shine:${{ steps.image_version.outputs.gitsha }}" + ssh ubuntu@"$instance" "docker-compose -f /path/to/your/docker-compose-prod.yml up -d" + done + env: + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + GIT_VERSION: ${{ steps.image_version.outputs.gitsha }} + SECRET_KEY: ${{ secrets.SECRET_KEY }} + DB_HOST: ${{ secrets.DB_HOST }} + DB_PORT: ${{ secrets.DB_PORT }} + DB_USERNAME: ${{ secrets.DB_USERNAME }} + DB_NAME: ${{ secrets.DB_NAME }} + AWS_BUCKET_NAME: ${{ secrets.AWS_BUCKET_NAME }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_REGION: ${{ secrets.AWS_REGION }} + AWS_CERTIFICATES_BUCKET_NAME: ${{ secrets.AWS_CERTIFICATES_BUCKET_NAME }} + PORT: ${{ secrets.PORT }}