Skip to content

Commit

Permalink
Enhance README
Browse files Browse the repository at this point in the history
  • Loading branch information
Terre8055 committed Nov 13, 2024
1 parent 941dc38 commit 275e8e4
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 143 deletions.
18 changes: 10 additions & 8 deletions .env.develop
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FILE_NAME=user_db
GET_PATH=$HOME/sus-db
LOG_PATH=$HOME/sus-db/sus_db.log
FILE_NAME=
GET_PATH=
LOG_PATH=


# REDIS Config == DEV
PORT=
HOST=
PASSWORD=
REDIS_OM_URL=
SSDB_EXTERNAL_SUPPORT=

# S3 Config
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
S3_BUCKET_NAME=
263 changes: 135 additions & 128 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ permissions:
env:
SECRETS_NAME: susdb
ECR_REPO: susdb
ECR_REGISTRY: 590183661216.dkr.ecr.eu-west-1.amazonaws.com
ECR_REGISTRY: terre8055
PROJECT_ID: susdb
S3_BUCKET: susdb

Expand All @@ -32,16 +32,23 @@ jobs:
with:
fetch-depth: 0

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
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: Configure AWS credentials
# uses: aws-actions/configure-aws-credentials@v1
# 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-ecr
# uses: aws-actions/amazon-ecr-login@v1

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Login to Docker Hub
id: login-dockerhub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Generate Version
id: versioning
Expand Down Expand Up @@ -83,17 +90,17 @@ jobs:
echo "New version: $new_version"
echo "::set-output name=new_version::$new_version"
- name: Retrieve secrets from AWS Secrets Manager
run: |
echo "Retrieving secrets from AWS Secrets Manager..."
SECRETS=$(aws secretsmanager get-secret-value --secret-id ${{ env.SECRETS_NAME }} --query SecretString --output text)
echo "$SECRETS" | jq -r 'to_entries | map("\(.key)=\(.value)") | .[]' > .env
# - name: Retrieve secrets from AWS Secrets Manager
# run: |
# echo "Retrieving secrets from AWS Secrets Manager..."
# SECRETS=$(aws secretsmanager get-secret-value --secret-id ${{ env.SECRETS_NAME }} --query SecretString --output text)
# echo "$SECRETS" | jq -r 'to_entries | map("\(.key)=\(.value)") | .[]' > .env


- name: Temporarily Store .env in s3 config store
run: |
echo "Upload env variables to s3..."
aws s3 cp .env s3://${{ env.S3_BUCKET }}
# - name: Temporarily Store .env in s3 config store
# run: |
# echo "Upload env variables to s3..."
# aws s3 cp .env s3://${{ env.S3_BUCKET }}


- name: Set up Docker Buildx
Expand All @@ -115,7 +122,7 @@ jobs:
id: build-image
uses: docker/build-push-action@v6
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REGISTRY: ${{ steps.login-dockerhub.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPO }}
IMAGE_TAG: ${{ steps.versioning.outputs.new_version }}
with:
Expand All @@ -125,8 +132,8 @@ jobs:
tags: |
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest
cache-from: type=gha,src=/tmp/.buildx-cache
cache-to: type=gha,dest=/tmp/.buildx-cache-new,mode=max
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max


- name: Move cache
Expand All @@ -144,128 +151,128 @@ jobs:
run: rm -f .env


deploy_to_managed_instances:
runs-on: ubuntu-latest
needs:
- build-push-and-tag
# deploy_to_managed_instances:
# runs-on: ubuntu-latest
# needs:
# - build-push-and-tag

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
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: Retrieve Managed Instances by Tags
id: get_instances
run: |
# Fetch instance IDs with specific tags, e.g., Environment=prod
instance_ids=$(aws ec2 describe-instances \
--filters "Name=tag:Project,Values=SUS" "Name=instance-state-name,Values=running" \
--query "Reservations[].Instances[].InstanceId" \
--output text)
# steps:
# - name: Checkout code
# uses: actions/checkout@v2

# - name: Configure AWS credentials
# uses: aws-actions/configure-aws-credentials@v1
# 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: Retrieve Managed Instances by Tags
# id: get_instances
# run: |
# # Fetch instance IDs with specific tags, e.g., Environment=prod
# instance_ids=$(aws ec2 describe-instances \
# --filters "Name=tag:Project,Values=SUS" "Name=instance-state-name,Values=running" \
# --query "Reservations[].Instances[].InstanceId" \
# --output text)

# Check if any instances were found
if [ -z "$instance_ids" ]; then
echo "No instances found with the specified tags."
exit 1
fi
# # Check if any instances were found
# if [ -z "$instance_ids" ]; then
# echo "No instances found with the specified tags."
# exit 1
# fi

echo "Instance IDs: $instance_ids"
echo "::set-output name=instance_ids::$instance_ids"
echo "::set-output name=instance_ids::$instance_ids"
- name: Executing Docker Commands on Instances using SSM
uses: peterkimzz/aws-ssm-send-command@master
id: ssm
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-1
instance-ids: ${{ steps.get_instances.outputs.instance_ids }}
working-directory: /home/ec2-user
comment: "Running docker commands"
command: |
sudo sh -c "set -e && \
export PATH=\$PATH:/usr/local/bin && \
cd /home/ec2-user && \
rm -rf .env.llm && \
echo 'Downloading docker-compose.yml from S3...' && \
aws s3 cp s3://${{ env.S3_BUCKET }}/docker-compose.yml . && \
echo 'Downloading .env from S3...' && \
aws s3 cp s3://${{ env.S3_BUCKET }}/.env . && \
echo 'Stopping existing Docker containers...' && \
/usr/local/bin/docker-compose -f /home/ec2-user/docker-compose.yml down && \
echo 'Logging in to Amazon ECR...' && \
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ env.ECR_REGISTRY }} && \
echo 'Pulling latest Docker images...' && \
/usr/local/bin/docker-compose -f /home/ec2-user/docker-compose.yml pull && \
echo 'Starting Docker services with Docker Compose...' && \
/usr/local/bin/docker-compose -f /home/ec2-user/docker-compose.yml up -d"
# echo "Instance IDs: $instance_ids"
# echo "::set-output name=instance_ids::$instance_ids"
# echo "::set-output name=instance_ids::$instance_ids"




# - name: Executing Docker Commands on Instances using SSM
# uses: peterkimzz/aws-ssm-send-command@master
# id: ssm
# with:
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# aws-region: eu-west-1
# instance-ids: ${{ steps.get_instances.outputs.instance_ids }}
# working-directory: /home/ec2-user
# comment: "Running docker commands"
# command: |
# sudo sh -c "set -e && \
# export PATH=\$PATH:/usr/local/bin && \
# cd /home/ec2-user && \
# rm -rf .env.llm && \
# echo 'Downloading docker-compose.yml from S3...' && \
# aws s3 cp s3://${{ env.S3_BUCKET }}/docker-compose.yml . && \
# echo 'Downloading .env from S3...' && \
# aws s3 cp s3://${{ env.S3_BUCKET }}/.env . && \
# echo 'Stopping existing Docker containers...' && \
# /usr/local/bin/docker-compose -f /home/ec2-user/docker-compose.yml down && \
# echo 'Logging in to Amazon ECR...' && \
# aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ env.ECR_REGISTRY }} && \
# echo 'Pulling latest Docker images...' && \
# /usr/local/bin/docker-compose -f /home/ec2-user/docker-compose.yml pull && \
# echo 'Starting Docker services with Docker Compose...' && \
# /usr/local/bin/docker-compose -f /home/ec2-user/docker-compose.yml up -d"



- name: Get the outputs
run: echo "The Command ID is ${{ steps.ssm.outputs.command-id }}"

- name: Check AWS SSM Command Status
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
run: |
command_id=${{ steps.ssm.outputs.command-id }}
instance_ids=${{ steps.get_instances.outputs.instance_ids }}
# Initial delay to ensure the command has time to start
sleep 10
for instance_id in $instance_ids; do
while true; do
# Fetch the command invocation details
output=$(aws ssm get-command-invocation \
--command-id $command_id \
--instance-id $instance_id \
--output json)
echo "Output: $output"
# - name: Get the outputs
# run: echo "The Command ID is ${{ steps.ssm.outputs.command-id }}"

# - name: Check AWS SSM Command Status
# env:
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# AWS_REGION: ${{ secrets.AWS_REGION }}
# run: |
# command_id=${{ steps.ssm.outputs.command-id }}
# instance_ids=${{ steps.get_instances.outputs.instance_ids }}

# # Initial delay to ensure the command has time to start
# sleep 10

# for instance_id in $instance_ids; do
# while true; do
# # Fetch the command invocation details
# output=$(aws ssm get-command-invocation \
# --command-id $command_id \
# --instance-id $instance_id \
# --output json)
# echo "Output: $output"

# Extract the status from the output
status=$(echo $output | jq -r '.Status')
echo "Current status for instance $instance_id: $status"
# # Extract the status from the output
# status=$(echo $output | jq -r '.Status')
# echo "Current status for instance $instance_id: $status"

# Check if the status is Success or Failed
if [[ "$status" == "Success" ]]; then
echo "Final status for instance $instance_id: $status"
break
elif [[ "$status" == "Failed" ]]; then
echo "Commands failed on instance $instance_id. Ending the pipeline."
exit 1
fi
# # Check if the status is Success or Failed
# if [[ "$status" == "Success" ]]; then
# echo "Final status for instance $instance_id: $status"
# break
# elif [[ "$status" == "Failed" ]]; then
# echo "Commands failed on instance $instance_id. Ending the pipeline."
# exit 1
# fi

# Delay before the next status check
sleep 60
done
done
# # Delay before the next status check
# sleep 60
# done
# done


- name: Remove env file from s3
if: always()
run: aws s3 rm s3://${{ env.S3_BUCKET }}/.env
# - name: Remove env file from s3
# if: always()
# run: aws s3 rm s3://${{ env.S3_BUCKET }}/.env


create-release:
needs:
- build-push-and-tag
- deploy_to_managed_instances
# - deploy_to_managed_instances
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand Down
Loading

0 comments on commit 275e8e4

Please sign in to comment.