Dev #94
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: Docker Image CI | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set image version (tag) | |
id: image_version | |
run: echo "gitsha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
- name: login to Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: sarahabuirmeileh | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- 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 | |
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" | |
- name: Configure Docker Hosts on EC2 Instances | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.DEPLOYMENT_KEY }}" > ~/.ssh/id_ed25519 | |
chmod 600 ~/.ssh/id_ed25519 | |
for ip in ${{ steps.get_instance_ips.outputs.ips }} | |
do | |
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no ubuntu@$ip "docker compose -f 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 }} |