refactor github actions iteration=12 (#1053) #68
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: Scale Backend Deployment | |
on: | |
workflow_dispatch: | |
inputs: | |
scaling_type: | |
description: "Do you want to scale up or down? type either 'up' or 'down'. defaults to 'up'" | |
required: true | |
default: "up" | |
jobs: | |
scale_up: | |
if: ${{ github.event.inputs.scaling_type == 'up' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Create new droplet | |
id: create_droplet | |
uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master | |
with: | |
token: ${{ secrets.DO_ACCESS_TOKEN }} | |
script: | | |
# This script will be executed inside a composite action. | |
# rename GITHUB_OUTPUT so that it's clear that it's a | |
# composite output and should be handled differently from $GITHUB_OUTPUT | |
COMPOSITE_OUTPUT=$GITHUB_OUTPUT | |
doctl compute droplet list 'zubhub-services*' > droplets.txt | |
droplets_count=`wc -l < droplets.txt` | |
echo "TARGET_DROPLETS_COUNT=$(($droplets_count))" >> $COMPOSITE_OUTPUT | |
doctl compute droplet create zubhub-services-$(($droplets_count-1)) --image \ | |
${{ secrets.SOURCE_SNAPSHOT_ID }} --tag-name zubhub-services --size s-1vcpu-1gb \ | |
--region nyc1 --enable-monitoring --ssh-keys ${{ secrets.DO_PUBLIC_SSHKEY_FP }} --wait | |
sleep 30s | |
echo "NEW_DROPLET_IP=$(doctl compute droplet get zubhub-services-$(($droplets_count-1)) \ | |
--format PublicIPv4 --no-header)" >> $COMPOSITE_OUTPUT | |
- name: Connect new droplet to swarm | |
uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master | |
env: | |
NEW_DROPLET_IP: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).NEW_DROPLET_IP }} | |
with: | |
HOST: ${{ env.NEW_DROPLET_IP }} | |
USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} | |
KEY: ${{ secrets.DO_SSHKEY }} | |
script: "docker swarm join --token ${{secrets.SWARM_WORKER_JOIN_TOKEN}} ${{secrets.SWARM_MASTER_HOST_AND_PORT}};sleep 10" | |
- name: Scale up deployment | |
uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master | |
env: | |
TARGET_DROPLETS_COUNT: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).TARGET_DROPLETS_COUNT }} | |
with: | |
HOST: ${{ secrets.DO_BACKEND_HOST }} | |
USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} | |
KEY: ${{ secrets.DO_SSHKEY }} | |
script: "docker service scale zubhub-services_web=${{env.TARGET_DROPLETS_COUNT}}" | |
scale_down: | |
if: ${{ github.event.inputs.scaling_type == 'down' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get target Droplet IP and Droplet Count | |
id: get_target_droplet_ip | |
with: | |
token: ${{ secrets.DO_ACCESS_TOKEN }} | |
script: | | |
# This script will be executed inside a composite action. | |
# rename GITHUB_OUTPUT so that it's clear that it's a | |
# composite output and should be handled differently from $GITHUB_OUTPUT | |
COMPOSITE_OUTPUT=$GITHUB_OUTPUT | |
doctl compute droplet list 'zubhub-services*' > droplets.txt | |
droplets_count=`wc -l < droplets.txt` | |
echo "TARGET_DROPLETS_COUNT=$(($droplets_count - 2))" >> $COMPOSITE_OUTPUT | |
echo "TARGET_DROPLET_IP=$(doctl compute droplet get zubhub-services-$(($droplets_count-2)) \ | |
--format PublicIPv4 --no-header)" >> $COMPOSITE_OUTPUT | |
- name: Scale down deployment | |
env: | |
TARGET_DROPLETS_COUNT: ${{ fromJson(steps.get_target_droplet_ip.outputs.JSON_STRING).TARGET_DROPLETS_COUNT }} | |
if: ${{ env.TARGET_DROPLETS_COUNT > 0 }} | |
uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master | |
with: | |
HOST: ${{ secrets.DO_BACKEND_HOST }} | |
USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} | |
KEY: ${{ secrets.DO_SSHKEY }} | |
script: "docker service scale zubhub-services_web=${{env.TARGET_DROPLETS_COUNT}}" | |
- name: Disconnect Target Droplet From Swarm | |
env: | |
TARGET_DROPLETS_COUNT: ${{ fromJson(steps.get_target_droplet_ip.outputs.JSON_STRING).TARGET_DROPLETS_COUNT }} | |
TARGET_DROPLET_IP: ${{ fromJson(steps.get_target_droplet_ip.outputs.JSON_STRING).TARGET_DROPLET_IP }} | |
if: ${{ env.TARGET_DROPLETS_COUNT > 0 }} | |
uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master | |
with: | |
HOST: ${{ env.TARGET_DROPLET_IP }} | |
USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} | |
KEY: ${{ secrets.DO_SSHKEY }} | |
script: "docker swarm leave;sleep 5s" | |
- name: Destroy Target Droplet | |
uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master | |
env: | |
TARGET_DROPLETS_COUNT: ${{ fromJson(steps.get_target_droplet_ip.outputs.JSON_STRING).TARGET_DROPLETS_COUNT }} | |
if: ${{ env.TARGET_DROPLETS_COUNT > 0 }} | |
with: | |
token: ${{ secrets.DO_ACCESS_TOKEN }} | |
script: | | |
doctl compute droplet delete -f zubhub-services-$TARGET_DROPLETS_COUNT | |
sleep 10s | |
- name: Remove Target Droplet From Node List | |
env: | |
TARGET_DROPLETS_COUNT: ${{ fromJson(steps.get_target_droplet_ip.outputs.JSON_STRING).TARGET_DROPLETS_COUNT }} | |
if: ${{ env.TARGET_DROPLETS_COUNT > 0 }} | |
uses: unstucturedstudio/zubhub/.github/actions/ssh_action@master | |
with: | |
HOST: ${{ secrets.DO_BACKEND_HOST }} | |
USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} | |
KEY: ${{ secrets.DO_SSHKEY }} | |
script: "docker node rm zubhub-services-${{env.TARGET_DROPLETS_COUNT}}" |