Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploy to all hosts in ASG #50

Merged
merged 8 commits into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
pull_request:
branches:
- main
- dev


jobs:
Expand All @@ -17,6 +18,10 @@ jobs:
contents: read
packages: write

outputs:
ec2_ips: ${{ steps.get_instance_ips.outputs.ips }}
image_version: ${{ steps.image_version.outputs.gitsha }}

steps:
- uses: actions/checkout@v3

Expand All @@ -35,25 +40,42 @@ jobs:
- name: Puplish 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 Instance IPs
id: get_instance_ips
run: |
instances=$(aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=serve-and-shine-asg" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output text)
echo "::set-output name=ips::$instances"
instances=$(aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=serve-and-shine-asg" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output json | jq --compact-output 'flatten')
echo "ips=$instances" >> $GITHUB_OUTPUT
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}

- name: Configure Docker Hosts on EC2 Instances
run: |
for ip in ${{ steps.get_instance_ips.outputs.ips }}
do
ssh -i ${{ secrets.DEPLOYMENT_KEY }} -o StrictHostKeyChecking=no github@$ip "docker compose -f docker-compose-prod.yml up -d"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't do like this, because docker-compose-prod.yml file is not copied in ec2 instance

done


deploy:
needs: ["build"]
runs-on: ubuntu-latest
strategy:
matrix:
ip: ${{ fromJson(needs.build.outputs.ec2_ips) }}
steps:
- uses: actions/checkout@v3
- name: configure docker host
uses: khaledez/configure-docker-host@v1
with:
host: ${{ matrix.ip }}
user: github
ssh-private-key: ${{ secrets.DEPLOYMENT_KEY }}
- name: login to Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: sarahabuirmeileh
password: ${{ secrets.GITHUB_TOKEN }}
- name: deploy
run: docker compose -f docker-compose-prod.yml up -d
env:
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
GIT_VERSISON: ${{ steps.image_version.outputs.gitsha }}
GIT_VERSISON: ${{ needs.build.outputs.image_version }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
Expand All @@ -64,4 +86,4 @@ jobs:
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 }}
PORT: ${{ secrets.PORT }}