Deploy Stacks #48
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: Deploy Stacks | |
on: | |
workflow_dispatch: | |
inputs: | |
image_tag: | |
description: "Version to deploy" | |
required: true | |
image_name: | |
description: "Name of the image to deploy" | |
required: true | |
replicas: | |
description: "Number of replicas" | |
required: true | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Deploy the app | |
run: echo "Deploying the app..." | |
- name: Echo inputs | |
run: | | |
echo "Image tag = ${{ github.event.inputs.image_tag }}" | |
echo "Image name = ${{ github.event.inputs.image_name }}" | |
echo "Replicas = ${{ github.event.inputs.replicas }}" | |
- name: Copy file to Server | |
id: copy | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PASSWORD }} | |
source: nest-stack.yml, parser-stack.yml, crawler-stack.yml, scale_nest.sh, scale_parser.sh, scale_crawler.sh | |
target: /home/ubuntu/ | |
- name: Deploy to Server | |
id: deploy | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PASSWORD }} | |
script: | | |
LOG_FILE="log_file.txt" | |
export IMAGE_TAG=${{ github.event.inputs.image_tag }} | |
export SERVICE_REPLICAS=${{ github.event.inputs.replicas }} | |
export IMAGE_NAME=${{ github.event.inputs.image_name }} | |
export DATABASE_URL=${{ secrets.DATABASE_URL }} | |
export REDIS_HOST=${{ secrets.REDIS_HOST }} | |
export REDIS_PORT=${{ secrets.REDIS_PORT }} | |
export REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }} | |
export EMAIL_USER=${{ secrets.EMAIL_USER }} | |
export EMAIL_PASS=${{ secrets.EMAIL_PASS }} | |
export JWT_SECRET=${{ secrets.JWT_SECRET }} | |
export SLACK_SECRET=${{ secrets.SLACK_DEPLOY_SECRET }} | |
export RABBITMQ_HOST=${{ secrets.RABBITMQ_HOST }} | |
export RABBITMQ_DEFAULT_USER=${{ secrets.RABBITMQ_DEFAULT_USER }} | |
export RABBITMQ_DEFAULT_PASS=${{ secrets.RABBITMQ_DEFAULT_PASS }} | |
export SWAGGER_USER=${{ secrets.SWAGGER_USER }} | |
export SWAGGER_PASSWORD=${{ secrets.SWAGGER_PASSWORD }} | |
if [ "$SERVICE_REPLICAS" == "1" ]; then | |
export SERVICE_REPLICAS="2" | |
fi | |
if [ "$IMAGE_NAME" == "suhach0523/techeerism-nest" ]; then | |
echo "Deploying Nest Stack..." | tee -a $LOG_FILE | |
docker stack deploy -c nest-stack.yml techeerzip --detach=true | tee -a $LOG_FILE | |
elif [ "$IMAGE_NAME" == "suhach0523/techeerism-parser" ]; then | |
echo "Deploying Parser..." | tee -a $LOG_FILE | |
docker stack deploy -c parser-stack.yml techeerzip --detach=true | tee -a $LOG_FILE | |
elif [ "$IMAGE_NAME" == "suhach0523/techeerism-crawler" ]; then | |
echo "Deploying Crawler..." | tee -a $LOG_FILE | |
docker stack deploy -c crawler-stack.yml techeerzip --detach=true | tee -a $LOG_FILE | |
else | |
echo "Invalid image name" | |
exit 1 | |
fi | |
- name: Scale Down if Needed | |
if: ${{ github.event.inputs.replicas }} == "1" | |
id: scale_down | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PASSWORD }} | |
script: | | |
LOG_FILE="log_file.txt" | |
export IMAGE_NAME=${{ github.event.inputs.image_name }} | |
if [ "$IMAGE_NAME" == "suhach0523/techeerism-nest" ]; then | |
ls | |
echo "Scaling Down Nest" | tee -a $LOG_FILE | |
chmod +x /home/ubuntu/scale_nest.sh | tee -a $LOG_FILE | |
/home/ubuntu/scale_nest.sh | tee -a $LOG_FILE | |
elif [ "$IMAGE_NAME" == "suhach0523/techeerism-parser" ]; then | |
echo "Scaling Down Parser" | tee -a $LOG_FILE | |
chmod +x /home/ubuntu/scale_parser.sh | tee -a $LOG_FILE | |
/home/ubuntu/scale_parser.sh | tee -a $LOG_FILE | |
elif [ "$IMAGE_NAME" == "suhach0523/techeerism-crawler" ]; then | |
echo "Scaling Down Crawler" | tee -a $LOG_FILE | |
chmod +x /home/ubuntu/scale_crawler.sh | tee -a $LOG_FILE | |
/home/ubuntu/scale_crawler.sh | tee -a $LOG_FILE | |
else | |
echo "Invalid image name" | |
exit 1 | |
fi | |
- name: Capture Failure Details | |
if: always() | |
run: | | |
if [ "${{ steps.deploy.outcome }}" == "failure" ]; then | |
echo "FAILED_STEP=Deploy Step" >> $GITHUB_ENV | |
echo "FAILED_LOGS=$(cat log_file.txt)" >> $GITHUB_ENV | |
elif [ "${{ steps.copy.outcome }}" == "failure" ]; then | |
echo "FAILED_STEP=Copy Step" >> $GITHUB_ENV | |
echo "FAILED_LOGS=$(cat log_file.txt)" >> $GITHUB_ENV | |
fi | |
- name: Clean Up Files in Server | |
if: always() | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PASSWORD }} | |
script: | | |
rm -f /home/ubuntu/nest-stack.yml | |
rm -f /home/ubuntu/parser-stack.yml | |
rm -f /home/ubuntu/crawler-stack.yml | |
rm -f /home/ubuntu/scale_nest.sh | |
rm -f /home/ubuntu/scale_parser.sh | |
rm -f /home/ubuntu/scale_crawler.sh | |
rm -f /home/ubuntu/log_file.txt | |
- name: Final Message To Slack | |
if: always() | |
env: | |
IMAGE_NAME: ${{ github.event.inputs.image_name }} | |
DOCKER_IMAGE_TAG: ${{ github.event.inputs.image_tag }} | |
SLACK_SECRET: ${{ secrets.SLACK_DEPLOY_SECRET }} | |
run: | | |
if [ -f log_file.txt ]; then | |
FAILED_LOGS=$(cat log_file.txt) | |
else | |
FAILED_LOGS="No logs available" | |
fi | |
if [ "$FAILED_STEP" != "" ]; then | |
echo "The step '$FAILED_STEP' failed." | |
curl -X POST https://techeer-029051b54345.herokuapp.com/api/v1/deploy/status \ | |
-H "Content-Type: application/json" \ | |
-d '{ | |
"status": "failed", | |
"imageName": "'"$IMAGE_NAME"'", | |
"imageTag": "'"$DOCKER_IMAGE_TAG"'", | |
"failedStep": "'"$FAILED_STEP"'", | |
"logs": "'"$FAILED_LOGS"'", | |
"secret": "'"$SLACK_SECRET"'", | |
"jobURL": "'"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"'" | |
}' | |
else | |
echo "All steps succeeded!" | |
curl -X POST https://techeer-029051b54345.herokuapp.com/api/v1/deploy/status \ | |
-H "Content-Type: application/json" \ | |
-d '{ | |
"status": "success", | |
"imageName": "'"$IMAGE_NAME"'", | |
"imageTag": "'"$DOCKER_IMAGE_TAG"'", | |
"failedStep": "'"$FAILED_STEP"'", | |
"logs": "'"$FAILED_LOGS"'", | |
"secret": "'"$SLACK_SECRET"'", | |
"jobURL": "'"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"'" | |
}' | |
fi |