Deploy Stacks #118
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 | |
# environment: | |
# description: "Environment to deploy" | |
# 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/ssh-action@master | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PASSWORD }} | |
capture_stdout: 'true' | |
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 }} | |
export AWS_S3_ACCESS_KEY=${{ secrets.AWS_S3_ACCESS_KEY }} | |
export AWS_S3_SECRET_ACCESS_KEY=${{ secrets.AWS_S3_SECRET_ACCESS_KEY }} | |
export AWS_S3_BUCKET_NAME=${{ secrets.AWS_S3_BUCKET_NAME }} | |
export AWS_REGION=${{ secrets.AWS_REGION }} | |
export REDIS_URL=${{ secrets.REDIS_URL }} | |
export RABBITMQ_URL=${{ secrets.RABBITMQ_URL }} | |
export GOOGLE_AUTH_TYPE=${{ secrets.GOOGLE_AUTH_TYPE }} | |
export GOOGLE_AUTH_PROJECT_ID=${{ secrets.GOOGLE_AUTH_PROJECT_ID }} | |
export GOOGLE_AUTH_PRIVATE_KEY_ID=${{ secrets.GOOGLE_AUTH_PRIVATE_KEY_ID }} | |
export GOOGLE_AUTH_PRIVATE_KEY=${{ secrets.GOOGLE_AUTH_PRIVATE_KEY }} | |
export GOOGLE_AUTH_CLIENT_EMAIL=${{ secrets.GOOGLE_AUTH_CLIENT_EMAIL }} | |
export GOOGLE_AUTH_CLIENT_ID=${{ secrets.GOOGLE_AUTH_CLIENT_ID }} | |
export GOOGLE_FOLDER_ID=${{ secrets.GOOGLE_FOLDER_ID }} | |
export ARCHIVE_FOLDER_ID=${{ secrets.ARCHIVE_FOLDER_ID }} | |
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/ssh-action@master | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PASSWORD }} | |
capture_stdout: 'true' | |
script: | | |
LOG_FILE="log_file.txt" | |
export IMAGE_NAME=${{ github.event.inputs.image_name }} | |
if [ "$IMAGE_NAME" == "suhach0523/techeerism-nest" ]; then | |
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: Save Logs To File | |
if: always() | |
run: | | |
cat <<EOF > log_file.txt | |
${{ steps.scale_down.outputs.stdout }} | |
EOF | |
- 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: | | |
FAILED_STEP="" | |
if grep -q "task: non-zero exit (1)" log_file.txt; then | |
FAILED_STEP="Scale" | |
elif grep -q failure log_file.txt; then | |
FAILED_STEP="Scale" | |
fi | |
FAILED_LOGS=$(tail -n 10 log_file.txt) | |
ESCAPED_LOGS=$(echo "$FAILED_LOGS" | sed ':a;N;$!ba;s/\n/\\n/g') | |
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": "'"$ESCAPED_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": "'"$ESCAPED_LOGS"'", | |
"secret": "'"$SLACK_SECRET"'", | |
"jobURL": "'"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"'" | |
}' | |
fi | |
- name: Clean Up Files in Server | |
if: always() | |
uses: appleboy/ssh-action@master | |
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 |