From f137f1f4c2e9e8ad4c2cc4c95b53e81d264a060e Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Tue, 19 Dec 2023 01:38:48 +0100 Subject: [PATCH 01/11] refactor github actions * use matrix strategy for similar jobs to save time * update version of third-party actions * use composite actions Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/actions/checkout/action.yml | 17 +++++ .../actions/docker_build_and_push/action.yml | 47 ++++++++++++++ .github/actions/scp_action/action.yml | 29 +++++++++ .github/actions/ssh_action/action.yml | 25 ++++++++ .github/workflows/build_deploy_backend.yml | 63 +++++-------------- .github/workflows/build_deploy_docs.yml | 4 +- .github/workflows/build_deploy_frontend.yml | 12 ++-- .github/workflows/build_locust.yml | 10 +-- .github/workflows/create_destroy_test_vm.yaml | 12 ++-- zubhub_backend/docker-compose.prod.yml | 28 ++++----- 10 files changed, 166 insertions(+), 81 deletions(-) create mode 100644 .github/actions/checkout/action.yml create mode 100644 .github/actions/docker_build_and_push/action.yml create mode 100644 .github/actions/scp_action/action.yml create mode 100644 .github/actions/ssh_action/action.yml diff --git a/.github/actions/checkout/action.yml b/.github/actions/checkout/action.yml new file mode 100644 index 000000000..741ff786f --- /dev/null +++ b/.github/actions/checkout/action.yml @@ -0,0 +1,17 @@ +name: Checkout files action +inputs: + ref: + required: false + type: string + repository: + required: false + type: string + +runs: + using: "composite" + steps: + - name: Checkout files + uses: actions/checkout@v4 + with: + ref: ${{ inputs.ref }} + repository: ${{ inputs.repository }} diff --git a/.github/actions/docker_build_and_push/action.yml b/.github/actions/docker_build_and_push/action.yml new file mode 100644 index 000000000..2244e5138 --- /dev/null +++ b/.github/actions/docker_build_and_push/action.yml @@ -0,0 +1,47 @@ +name: Docker build and push action +inputs: + dockerhub_username: + required: true + type: string + dockerhub_token: + required: true + type: string + context: + required: true + type: string + file: + required: true + type: string + tags: + required: true + type: string + push: + required: true + type: boolean + +runs: + using: "composite" + steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ inputs.dockerhub_username }} + password: ${{ inputs.dockerhub_token }} + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v5 + with: + context: ${{ inputs.context }}} + file: ${{ inputs.file }} + push: ${{ inputs.push }} + tags: ${{ inputs.tags }} + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/.github/actions/scp_action/action.yml b/.github/actions/scp_action/action.yml new file mode 100644 index 000000000..7ddfe9958 --- /dev/null +++ b/.github/actions/scp_action/action.yml @@ -0,0 +1,29 @@ +name: Copy files action +inputs: + host: + required: true + type: string + username: + required: true + type: string + key: + required: true + type: string + source: + required: true + type: string + target: + required: true + type: string + +runs: + using: "composite" + steps: + - name: Copy file via scp + uses: appleboy/scp-action@master + with: + host: ${{ inputs.host }} + username: ${{ inputs.username }} + key: ${{ inputs.key }} + source: ${{ inputs.source }} + target: ${{ inputs.target }} diff --git a/.github/actions/ssh_action/action.yml b/.github/actions/ssh_action/action.yml new file mode 100644 index 000000000..85ae8c647 --- /dev/null +++ b/.github/actions/ssh_action/action.yml @@ -0,0 +1,25 @@ +name: Execute remote command action +inputs: + host: + required: true + type: string + username: + required: true + type: string + key: + required: true + type: string + script: + required: true + type: string + +runs: + using: "composite" + steps: + - name: Executing remote command + uses: appleboy/ssh-action@master + with: + host: ${{ inputs.host }} + username: ${{ inputs.username }} + key: ${{ inputs.key }} + script: ${{ inputs.script }} diff --git a/.github/workflows/build_deploy_backend.yml b/.github/workflows/build_deploy_backend.yml index f7383c444..f426a76e9 100644 --- a/.github/workflows/build_deploy_backend.yml +++ b/.github/workflows/build_deploy_backend.yml @@ -13,64 +13,32 @@ on: workflow_dispatch: jobs: - build: + build_and_push: runs-on: ubuntu-latest + strategy: + matrix: + service: ['web', 'celery', 'media'] steps: - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - name: Login to DockerHub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Checkout files - uses: actions/checkout@v2 - - - name: Build and push django api - id: docker_build_web - uses: docker/build-push-action@v2 - with: - context: ./zubhub_backend/ - file: ./zubhub_backend/compose/web/prod/Dockerfile - push: true - tags: unstructuredstudio/zubhub-services_web:latest - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} - - - name: Build and push celery worker - id: docker_build_celery - uses: docker/build-push-action@v2 - with: - context: ./zubhub_backend/ - file: ./zubhub_backend/compose/celery/prod/Dockerfile - push: true - tags: unstructuredstudio/zubhub-services_celery:latest - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master - - name: Build and push media service - id: docker_build_media - uses: docker/build-push-action@v2 + - name: Build and push ${{ matrix.service }} + uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master with: + dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }} + dockerhub_token: ${{ secrets.DOCKERHUB_TOKEN }} context: ./zubhub_backend/ - file: ./zubhub_backend/compose/media/prod/Dockerfile + file: ./zubhub_backend/compose/${{ matrix.service }}/prod/Dockerfile push: true - tags: unstructuredstudio/zubhub-services_media:latest - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} + tags: unstructuredstudio/zubhub-services_${{ matrix.service }}:latest deploy: - needs: build + needs: build_and_push runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master - - name: Copy file via scp - uses: appleboy/scp-action@master + - uses: unstructuredstudio/zubhub/.github/actions/scp_action@master with: host: ${{ secrets.DO_BACKEND_HOST }} username: ${{ secrets.DO_BACKEND_USERNAME }} @@ -78,8 +46,7 @@ jobs: source: "." target: "/home/zubhub-services/zubhub" - - name: Executing remote command - uses: appleboy/ssh-action@master + - uses: unsctructuredstudio/zubhub/.github/actions/ssh_action@master with: host: ${{ secrets.DO_BACKEND_HOST }} username: ${{ secrets.DO_BACKEND_USERNAME }} diff --git a/.github/workflows/build_deploy_docs.yml b/.github/workflows/build_deploy_docs.yml index fdc5b9674..bdab90f65 100644 --- a/.github/workflows/build_deploy_docs.yml +++ b/.github/workflows/build_deploy_docs.yml @@ -11,8 +11,8 @@ jobs: deploy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 with: python-version: "3.x" - run: | diff --git a/.github/workflows/build_deploy_frontend.yml b/.github/workflows/build_deploy_frontend.yml index ea07d4fc1..ee047492d 100644 --- a/.github/workflows/build_deploy_frontend.yml +++ b/.github/workflows/build_deploy_frontend.yml @@ -17,21 +17,21 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up QEMU - uses: docker/setup-qemu-action@v1 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Checkout files - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Build and push id: docker_build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v5 with: context: ./zubhub_frontend/zubhub/ file: ./zubhub_frontend/zubhub/Dockerfile.prod @@ -45,7 +45,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - name: Copy file via scp uses: appleboy/scp-action@master diff --git a/.github/workflows/build_locust.yml b/.github/workflows/build_locust.yml index ea70921ed..41ff94655 100644 --- a/.github/workflows/build_locust.yml +++ b/.github/workflows/build_locust.yml @@ -23,21 +23,21 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up QEMU - uses: docker/setup-qemu-action@v1 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Checkout files - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Build and push locust id: docker_build_locust - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v5 with: context: ./locust/ file: ./locust/Dockerfile diff --git a/.github/workflows/create_destroy_test_vm.yaml b/.github/workflows/create_destroy_test_vm.yaml index 9c63b1277..30c92dfa6 100644 --- a/.github/workflows/create_destroy_test_vm.yaml +++ b/.github/workflows/create_destroy_test_vm.yaml @@ -47,7 +47,7 @@ jobs: # See https://stackoverflow.com/questions/74957218/what-is-the-difference-between-pull-request-and-pull-request-target-event-in-git # See https://dev.to/suzukishunsuke/secure-github-actions-by-pullrequesttarget-641#:~:text=pull_request_target%20is%20one%20of%20the,the%20pull%20request's%20base%20branch. - name: Checkout source branch - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: ref: ${{ env.CHECKOUT_COMMIT_REF }} repository: ${{ env.CHECKOUT_REPO }} @@ -148,7 +148,7 @@ jobs: # See https://stackoverflow.com/questions/74957218/what-is-the-difference-between-pull-request-and-pull-request-target-event-in-git # See https://dev.to/suzukishunsuke/secure-github-actions-by-pullrequesttarget-641#:~:text=pull_request_target%20is%20one%20of%20the,the%20pull%20request's%20base%20branch. - name: Checkout source branch - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: ref: ${{ env.CHECKOUT_COMMIT_REF }} repository: ${{ env.CHECKOUT_REPO }} @@ -175,20 +175,20 @@ jobs: EOF - name: Set up QEMU - uses: docker/setup-qemu-action@v1 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push ${{ matrix.service }} id: docker_build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v5 with: # for frontend, context is ./zubhub_frontend/zubhub/ # for backend services, context is ./zubhub_backend/ diff --git a/zubhub_backend/docker-compose.prod.yml b/zubhub_backend/docker-compose.prod.yml index dbd5c4b3b..6b769d819 100644 --- a/zubhub_backend/docker-compose.prod.yml +++ b/zubhub_backend/docker-compose.prod.yml @@ -95,20 +95,20 @@ services: depends_on: - rabbitmq - prometheus: - image: prom/prometheus - command: - - --config.file=/etc/prometheus/prometheus.yml - deploy: - replicas: 1 - restart_policy: - condition: on-failure - placement: - max_replicas_per_node: 1 - constraints: - - "node.role==manager" - volumes: - - ./compose/prometheus.yml:/etc/prometheus/prometheus.yml:ro + # prometheus: + # image: prom/prometheus + # command: + # - --config.file=/etc/prometheus/prometheus.yml + # deploy: + # replicas: 1 + # restart_policy: + # condition: on-failure + # placement: + # max_replicas_per_node: 1 + # constraints: + # - "node.role==manager" + # volumes: + # - ./compose/prometheus.yml:/etc/prometheus/prometheus.yml:ro secrets: zubhub_services_secrets: From 923cc7015dbbb6bf13af75343b252ce80608539b Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Thu, 21 Dec 2023 00:11:37 +0100 Subject: [PATCH 02/11] refactor github actions fix error in composite action. for context see https://github.com/unstructuredstudio/zubhub/pull/1041 Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/actions/docker_build_and_push/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/docker_build_and_push/action.yml b/.github/actions/docker_build_and_push/action.yml index 2244e5138..0b0742385 100644 --- a/.github/actions/docker_build_and_push/action.yml +++ b/.github/actions/docker_build_and_push/action.yml @@ -44,4 +44,5 @@ runs: tags: ${{ inputs.tags }} - name: Image digest + shell: bash run: echo ${{ steps.docker_build.outputs.digest }} From 9af610f8396ac8fab505362522029b927f191c8a Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Thu, 21 Dec 2023 00:23:02 +0100 Subject: [PATCH 03/11] refactor github actions iteration=4 fix error in composite action. for context see https://github.com/unstructuredstudio/zubhub/pull/1041 Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/actions/docker_build_and_push/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/docker_build_and_push/action.yml b/.github/actions/docker_build_and_push/action.yml index 0b0742385..766bacb44 100644 --- a/.github/actions/docker_build_and_push/action.yml +++ b/.github/actions/docker_build_and_push/action.yml @@ -38,7 +38,7 @@ runs: id: docker_build uses: docker/build-push-action@v5 with: - context: ${{ inputs.context }}} + context: ${{ inputs.context }} file: ${{ inputs.file }} push: ${{ inputs.push }} tags: ${{ inputs.tags }} From 5ea2ffdfb1a000e6dd1f2309f76d73a311e549c5 Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Thu, 21 Dec 2023 02:47:19 +0100 Subject: [PATCH 04/11] refactor github actions iteration=5 * use matrix strategy for similar jobs to save time * update version of third-party actions * use composite actions Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .../actions/docker_build_and_push/action.yml | 8 +- .github/actions/doctl_action/action.yaml | 22 +++++ .github/workflows/build_deploy_backend.yml | 6 +- .github/workflows/build_deploy_docs.yml | 2 +- .github/workflows/build_deploy_frontend.yml | 43 ++++------ .github/workflows/build_locust.yml | 85 ------------------- .github/workflows/create_destroy_test_vm.yaml | 31 ++----- .github/workflows/locust.yml | 72 ++++++++++++++++ .github/workflows/scale_backend.yml | 10 +-- 9 files changed, 129 insertions(+), 150 deletions(-) create mode 100644 .github/actions/doctl_action/action.yaml delete mode 100644 .github/workflows/build_locust.yml create mode 100644 .github/workflows/locust.yml diff --git a/.github/actions/docker_build_and_push/action.yml b/.github/actions/docker_build_and_push/action.yml index 766bacb44..93f7b9945 100644 --- a/.github/actions/docker_build_and_push/action.yml +++ b/.github/actions/docker_build_and_push/action.yml @@ -1,9 +1,9 @@ name: Docker build and push action inputs: - dockerhub_username: + username: required: true type: string - dockerhub_token: + token: required: true type: string context: @@ -31,8 +31,8 @@ runs: - name: Login to DockerHub uses: docker/login-action@v3 with: - username: ${{ inputs.dockerhub_username }} - password: ${{ inputs.dockerhub_token }} + username: ${{ inputs.username }} + password: ${{ inputs.token }} - name: Build and push id: docker_build diff --git a/.github/actions/doctl_action/action.yaml b/.github/actions/doctl_action/action.yaml new file mode 100644 index 000000000..8fdf5014e --- /dev/null +++ b/.github/actions/doctl_action/action.yaml @@ -0,0 +1,22 @@ +name: Doctl action +inputs: + token: + required: true + type: string + script: + required: true + type: string +outputs: + json_string: ${{ toJson(steps.script.outputs) }} + +runs: + using: "composite" + steps: + - name: Install doctl + uses: digitalocean/action-doctl@v2 + with: + token: ${{ inputs.token }} + + - name: execute script + id: script + run: ${{ inputs.script }} diff --git a/.github/workflows/build_deploy_backend.yml b/.github/workflows/build_deploy_backend.yml index f426a76e9..16c849726 100644 --- a/.github/workflows/build_deploy_backend.yml +++ b/.github/workflows/build_deploy_backend.yml @@ -24,8 +24,8 @@ jobs: - name: Build and push ${{ matrix.service }} uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master with: - dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }} - dockerhub_token: ${{ secrets.DOCKERHUB_TOKEN }} + username: ${{ secrets.DOCKERHUB_USERNAME }} + token: ${{ secrets.DOCKERHUB_TOKEN }} context: ./zubhub_backend/ file: ./zubhub_backend/compose/${{ matrix.service }}/prod/Dockerfile push: true @@ -46,7 +46,7 @@ jobs: source: "." target: "/home/zubhub-services/zubhub" - - uses: unsctructuredstudio/zubhub/.github/actions/ssh_action@master + - uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master with: host: ${{ secrets.DO_BACKEND_HOST }} username: ${{ secrets.DO_BACKEND_USERNAME }} diff --git a/.github/workflows/build_deploy_docs.yml b/.github/workflows/build_deploy_docs.yml index bdab90f65..babbee87d 100644 --- a/.github/workflows/build_deploy_docs.yml +++ b/.github/workflows/build_deploy_docs.yml @@ -11,7 +11,7 @@ jobs: deploy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master - uses: actions/setup-python@v5 with: python-version: "3.x" diff --git a/.github/workflows/build_deploy_frontend.yml b/.github/workflows/build_deploy_frontend.yml index ee047492d..a548d3528 100644 --- a/.github/workflows/build_deploy_frontend.yml +++ b/.github/workflows/build_deploy_frontend.yml @@ -16,51 +16,38 @@ jobs: build: runs-on: ubuntu-latest steps: - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Login to DockerHub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} + uses: unstructuredstudio/zubhub/.github/actions/checkout@master - - name: Checkout files - uses: actions/checkout@v4 - - - name: Build and push - id: docker_build - uses: docker/build-push-action@v5 + - name: Build and push frontend + uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + token: ${{ secrets.DOCKERHUB_TOKEN }} context: ./zubhub_frontend/zubhub/ file: ./zubhub_frontend/zubhub/Dockerfile.prod push: true tags: unstructuredstudio/zubhub-frontend:latest - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} deploy: needs: build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master - - name: Copy file via scp - uses: appleboy/scp-action@master + - uses: unstructuredstudio/zubhub/.github/actions/scp_action@master with: - host: ${{ secrets.DO_FRONTEND_HOST }} - username: ${{ secrets.DO_FRONTEND_USERNAME }} + host: ${{ secrets.DO_BACKEND_HOST }} + username: ${{ secrets.DO_BACKEND_USERNAME }} key: ${{ secrets.DO_SSHKEY }} source: "." target: "/home/zubhub-frontend/zubhub" - - name: Executing remote command - uses: appleboy/ssh-action@master + - uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master with: - host: ${{ secrets.DO_FRONTEND_HOST }} - username: ${{ secrets.DO_FRONTEND_USERNAME }} - port: ${{ secrets.DO_SSHPORT }} + host: ${{ secrets.DO_BACKEND_HOST }} + username: ${{ secrets.DO_BACKEND_USERNAME }} key: ${{ secrets.DO_SSHKEY }} - script: "cp /home/zubhub-frontend/zubhub/zubhub_frontend/zubhub/deploy_frontend.sh /home/zubhub-frontend/ && sudo bash /home/zubhub-frontend/deploy_frontend.sh" + script: | + cp /home/zubhub-frontend/zubhub/zubhub_frontend/zubhub/deploy_frontend.sh /home/zubhub-frontend/ + sudo bash /home/zubhub-frontend/deploy_frontend.sh" diff --git a/.github/workflows/build_locust.yml b/.github/workflows/build_locust.yml deleted file mode 100644 index 41ff94655..000000000 --- a/.github/workflows/build_locust.yml +++ /dev/null @@ -1,85 +0,0 @@ -name: Build/Deploy/Destroy Locust Service - -on: - push: - branches: - - master - paths: - - "locust/**" - - workflow_dispatch: - inputs: - action_type: - description: - "Do you want to build new locust container or deploy/destroy a locust service? type \ - 'build' to build new container, 'deploy' to deploy a locust service, 'destroy' to \ - destroy existing locust service. defaults to 'build' " - required: true - default: "build" - -jobs: - build: - if: ${{github.event_name == 'push' || github.event.inputs.action_type == 'build' }} - runs-on: ubuntu-latest - steps: - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Login to DockerHub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Checkout files - uses: actions/checkout@v4 - - - name: Build and push locust - id: docker_build_locust - uses: docker/build-push-action@v5 - with: - context: ./locust/ - file: ./locust/Dockerfile - push: true - tags: unstructuredstudio/zubhub-services_locust:latest - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} - - deploy: - if: ${{ github.event.inputs.action_type == 'deploy' }} - runs-on: ubuntu-latest - steps: - - name: Install doctl - uses: digitalocean/action-doctl@v2 - with: - token: ${{ secrets.DO_ACCESS_TOKEN }} - - - name: Create new droplet - run: | - doctl compute droplet create locust --image \ - ${{ secrets.SOURCE_SNAPSHOT_ID }} --tag-name zubhub-locust --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 locust \ - --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_ENV - - - name: Deploy locust - uses: appleboy/ssh-action@master - with: - HOST: ${{ env.NEW_DROPLET_IP }} - USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} - KEY: ${{ secrets.DO_SSHKEY }} - script: "docker run -d -p 8089:8089 unstructuredstudio/zubhub-services_locust:latest -f /mnt/locust/locustfile.py" - - destroy: - if: ${{ github.event.inputs.action_type == 'destroy' }} - runs-on: ubuntu-latest - steps: - - name: Install doctl - uses: digitalocean/action-doctl@v2 - with: - token: ${{ secrets.DO_ACCESS_TOKEN }} - - - name: Destroy Target Droplet - run: "doctl compute droplet delete -f locust" diff --git a/.github/workflows/create_destroy_test_vm.yaml b/.github/workflows/create_destroy_test_vm.yaml index 30c92dfa6..5c050c2da 100644 --- a/.github/workflows/create_destroy_test_vm.yaml +++ b/.github/workflows/create_destroy_test_vm.yaml @@ -46,8 +46,7 @@ jobs: # See https://stackoverflow.com/questions/75873833/how-to-protect-github-secrets-in-pull-request-actions-from-malicious-pull-reques # See https://stackoverflow.com/questions/74957218/what-is-the-difference-between-pull-request-and-pull-request-target-event-in-git # See https://dev.to/suzukishunsuke/secure-github-actions-by-pullrequesttarget-641#:~:text=pull_request_target%20is%20one%20of%20the,the%20pull%20request's%20base%20branch. - - name: Checkout source branch - uses: actions/checkout@v4 + uses: unstructuredstudio/zubhub/.github/actions/checkout@master with: ref: ${{ env.CHECKOUT_COMMIT_REF }} repository: ${{ env.CHECKOUT_REPO }} @@ -94,8 +93,7 @@ jobs: doctl compute domain records create unstructured.studio --record-type A --record-name \ $MEDIA_DOMAIN --record-data $NEW_DROPLET_IP --record-ttl 600 - - name: Copy file via scp - uses: appleboy/scp-action@master + uses: unstructuredstudio/zubhub/.github/actions/scp_action@master with: host: ${{env.NEW_DROPLET_IP}} username: ${{ secrets.DO_BACKEND_USERNAME }} @@ -147,8 +145,7 @@ jobs: # See https://stackoverflow.com/questions/75873833/how-to-protect-github-secrets-in-pull-request-actions-from-malicious-pull-reques # See https://stackoverflow.com/questions/74957218/what-is-the-difference-between-pull-request-and-pull-request-target-event-in-git # See https://dev.to/suzukishunsuke/secure-github-actions-by-pullrequesttarget-641#:~:text=pull_request_target%20is%20one%20of%20the,the%20pull%20request's%20base%20branch. - - name: Checkout source branch - uses: actions/checkout@v4 + uses: unstructuredstudio/zubhub/.github/actions/checkout@master with: ref: ${{ env.CHECKOUT_COMMIT_REF }} repository: ${{ env.CHECKOUT_REPO }} @@ -174,22 +171,11 @@ jobs: REACT_APP_DEV_VIDEO_UPLOAD_PRESET_NAME=dev_video_upload_preset EOF - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Login to DockerHub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Build and push ${{ matrix.service }} - id: docker_build - uses: docker/build-push-action@v5 + uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + token: ${{ secrets.DOCKERHUB_TOKEN }} # for frontend, context is ./zubhub_frontend/zubhub/ # for backend services, context is ./zubhub_backend/ context: ./zubhub_${{ matrix.service == 'frontend' && 'frontend/zubhub' || 'backend' }}/ @@ -198,9 +184,6 @@ jobs: file: ./zubhub_${{ matrix.service == 'frontend' && 'frontend/zubhub' || format('backend/compose/{0}', matrix.service) }}/${{ matrix.service != 'frontend' && 'prod/' || '' }}Dockerfile${{ matrix.service == 'frontend' && '.prod' || '' }} push: true tags: unstructuredstudio/zubhub-test_${{ matrix.service }}:latest - - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} ################################################################################## ################################################################################# @@ -221,7 +204,7 @@ jobs: echo "MEDIA_DOMAIN=${{ needs.create_test_vm.outputs.MEDIA_DOMAIN }}" >> $GITHUB_ENV - name: Executing remote command - uses: appleboy/ssh-action@master + uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master with: host: ${{env.NEW_DROPLET_IP}} username: ${{ secrets.DO_BACKEND_USERNAME }} diff --git a/.github/workflows/locust.yml b/.github/workflows/locust.yml new file mode 100644 index 000000000..da618e4f2 --- /dev/null +++ b/.github/workflows/locust.yml @@ -0,0 +1,72 @@ +name: Build/Deploy/Destroy Locust Service + +on: + push: + branches: + - master + paths: + - "locust/**" + + workflow_dispatch: + inputs: + action_type: + description: + "Do you want to build new locust container or deploy/destroy a locust service? type \ + 'build' to build new container, 'deploy' to deploy a locust service, 'destroy' to \ + destroy existing locust service. defaults to 'build' " + required: true + default: "deploy" + +jobs: + build: + if: ${{github.event_name == 'push' || github.event.inputs.action_type == 'build' }} + runs-on: ubuntu-latest + steps: + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master + + - name: Build and push locust + uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + token: ${{ secrets.DOCKERHUB_TOKEN }} + context: ./locust/ + file: ./locust/Dockerfile + push: true + tags: unstructuredstudio/zubhub-services_locust:latest + + deploy: + if: ${{ github.event.inputs.action_type == 'deploy' }} + runs-on: ubuntu-latest + steps: + - name: Create new DO droplet + id: create_droplet + uses: unsctructuredstudio/zubhub/.github/actions/doctl@master + with: + token: ${{ secrets.DO_ACCESS_TOKEN }} + script: | + doctl compute droplet create locust --image \ + ${{ secrets.SOURCE_SNAPSHOT_ID }} --tag-name zubhub-locust --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 locust \ + --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_OUTPUT + + - name: Deploy locust + uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master + with: + host: ${{ fromJson(steps.create_droplet.outputs.json_string).NEW_DROPLET_IP }} + username: ${{ secrets.DO_BACKEND_USERNAME }} + key: ${{ secrets.DO_SSHKEY }} + script: | + docker run -d -p 8089:8089 unstructuredstudio/zubhub-services_locust:latest \ + -f /mnt/locust/locustfile.py + + destroy: + if: ${{ github.event.inputs.action_type == 'destroy' }} + runs-on: ubuntu-latest + steps: + - name: Destroy Target Droplet + uses: unstructuredstudio/zubhub/.github/actions/doctl@master + with: + token: ${{ secrets.DO_ACCESS_TOKEN }} + script: doctl compute droplet delete -f locust diff --git a/.github/workflows/scale_backend.yml b/.github/workflows/scale_backend.yml index b2fb67539..615b84a31 100644 --- a/.github/workflows/scale_backend.yml +++ b/.github/workflows/scale_backend.yml @@ -31,7 +31,7 @@ jobs: --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_ENV - name: Connect new droplet to swarm - uses: appleboy/ssh-action@master + uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master with: HOST: ${{ env.NEW_DROPLET_IP }} USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} @@ -39,7 +39,7 @@ jobs: script: "docker swarm join --token ${{secrets.SWARM_WORKER_JOIN_TOKEN}} ${{secrets.SWARM_MASTER_HOST_AND_PORT}};sleep 10" - name: Scale up deployment - uses: appleboy/ssh-action@master + uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master with: HOST: ${{ secrets.DO_BACKEND_HOST }} USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} @@ -65,7 +65,7 @@ jobs: - name: Scale down deployment if: ${{ env.TARGET_DROPLETS_COUNT > 0 }} - uses: appleboy/ssh-action@master + uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master with: HOST: ${{ secrets.DO_BACKEND_HOST }} USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} @@ -74,7 +74,7 @@ jobs: - name: Disconnect Target Droplet From Swarm if: ${{ env.TARGET_DROPLETS_COUNT > 0 }} - uses: appleboy/ssh-action@master + uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master with: HOST: ${{ env.TARGET_DROPLET_IP }} USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} @@ -89,7 +89,7 @@ jobs: - name: Remove Target Droplet From Node List if: ${{ env.TARGET_DROPLETS_COUNT > 0 }} - uses: appleboy/ssh-action@master + uses: unstucturedstudio/zubhub/.github/actions/ssh_action@master with: HOST: ${{ secrets.DO_BACKEND_HOST }} USERNAME: ${{ secrets.DO_BACKEND_USERNAME }} From 10bbd14ab09d4c9170c624cebdc75a2c0809cc53 Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Thu, 21 Dec 2023 03:09:08 +0100 Subject: [PATCH 05/11] refactor github actions iteration=6 fix error in github actions. for context see https://github.com/unstructuredstudio/zubhub/pull/1044 Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/workflows/build_deploy_frontend.yml | 2 +- .github/workflows/create_destroy_test_vm.yaml | 7 ++++--- .github/workflows/locust.yml | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build_deploy_frontend.yml b/.github/workflows/build_deploy_frontend.yml index a548d3528..f25359ca2 100644 --- a/.github/workflows/build_deploy_frontend.yml +++ b/.github/workflows/build_deploy_frontend.yml @@ -16,7 +16,7 @@ jobs: build: runs-on: ubuntu-latest steps: - uses: unstructuredstudio/zubhub/.github/actions/checkout@master + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master - name: Build and push frontend uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master diff --git a/.github/workflows/create_destroy_test_vm.yaml b/.github/workflows/create_destroy_test_vm.yaml index 5c050c2da..d958c9d33 100644 --- a/.github/workflows/create_destroy_test_vm.yaml +++ b/.github/workflows/create_destroy_test_vm.yaml @@ -46,7 +46,7 @@ jobs: # See https://stackoverflow.com/questions/75873833/how-to-protect-github-secrets-in-pull-request-actions-from-malicious-pull-reques # See https://stackoverflow.com/questions/74957218/what-is-the-difference-between-pull-request-and-pull-request-target-event-in-git # See https://dev.to/suzukishunsuke/secure-github-actions-by-pullrequesttarget-641#:~:text=pull_request_target%20is%20one%20of%20the,the%20pull%20request's%20base%20branch. - uses: unstructuredstudio/zubhub/.github/actions/checkout@master + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master with: ref: ${{ env.CHECKOUT_COMMIT_REF }} repository: ${{ env.CHECKOUT_REPO }} @@ -93,7 +93,7 @@ jobs: doctl compute domain records create unstructured.studio --record-type A --record-name \ $MEDIA_DOMAIN --record-data $NEW_DROPLET_IP --record-ttl 600 - uses: unstructuredstudio/zubhub/.github/actions/scp_action@master + - uses: unstructuredstudio/zubhub/.github/actions/scp_action@master with: host: ${{env.NEW_DROPLET_IP}} username: ${{ secrets.DO_BACKEND_USERNAME }} @@ -110,6 +110,7 @@ jobs: echo "MEDIA_DOMAIN=$MEDIA_DOMAIN" >> $GITHUB_OUTPUT echo "CHECKOUT_COMMIT_REF=${{ env.CHECKOUT_COMMIT_REF }}" >> $GITHUB_OUTPUT echo "CHECKOUT_REPO=${{ env.CHECKOUT_REPO }}" >> $GITHUB_OUTPUT + outputs: NEW_DROPLET_IP: ${{ steps.set_output.outputs.NEW_DROPLET_IP }} FRONTEND_DOMAIN: ${{ steps.set_output.outputs.FRONTEND_DOMAIN }} @@ -145,7 +146,7 @@ jobs: # See https://stackoverflow.com/questions/75873833/how-to-protect-github-secrets-in-pull-request-actions-from-malicious-pull-reques # See https://stackoverflow.com/questions/74957218/what-is-the-difference-between-pull-request-and-pull-request-target-event-in-git # See https://dev.to/suzukishunsuke/secure-github-actions-by-pullrequesttarget-641#:~:text=pull_request_target%20is%20one%20of%20the,the%20pull%20request's%20base%20branch. - uses: unstructuredstudio/zubhub/.github/actions/checkout@master + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master with: ref: ${{ env.CHECKOUT_COMMIT_REF }} repository: ${{ env.CHECKOUT_REPO }} diff --git a/.github/workflows/locust.yml b/.github/workflows/locust.yml index da618e4f2..1062ded6d 100644 --- a/.github/workflows/locust.yml +++ b/.github/workflows/locust.yml @@ -40,7 +40,7 @@ jobs: steps: - name: Create new DO droplet id: create_droplet - uses: unsctructuredstudio/zubhub/.github/actions/doctl@master + uses: unstructuredstudio/zubhub/.github/actions/doctl@master with: token: ${{ secrets.DO_ACCESS_TOKEN }} script: | From f08124dc706086d3227485d31dcd97f50ecd1077 Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Thu, 21 Dec 2023 03:18:54 +0100 Subject: [PATCH 06/11] refactor github actions iteration=7 fix error in github actions. for context see https://github.com/unstructuredstudio/zubhub/pull/1044 Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/workflows/locust.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/locust.yml b/.github/workflows/locust.yml index 1062ded6d..9e55ae05e 100644 --- a/.github/workflows/locust.yml +++ b/.github/workflows/locust.yml @@ -40,7 +40,7 @@ jobs: steps: - name: Create new DO droplet id: create_droplet - uses: unstructuredstudio/zubhub/.github/actions/doctl@master + uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master with: token: ${{ secrets.DO_ACCESS_TOKEN }} script: | @@ -66,7 +66,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Destroy Target Droplet - uses: unstructuredstudio/zubhub/.github/actions/doctl@master + uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master with: token: ${{ secrets.DO_ACCESS_TOKEN }} script: doctl compute droplet delete -f locust From e0f3c2514e027aa1a709067f510e69c057d082d8 Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Thu, 21 Dec 2023 03:27:39 +0100 Subject: [PATCH 07/11] refactor github actions iteration=8 fix error in github actions. for context see https://github.com/unstructuredstudio/zubhub/pull/1044 Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/actions/doctl_action/action.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/doctl_action/action.yaml b/.github/actions/doctl_action/action.yaml index 8fdf5014e..2859437d0 100644 --- a/.github/actions/doctl_action/action.yaml +++ b/.github/actions/doctl_action/action.yaml @@ -20,3 +20,4 @@ runs: - name: execute script id: script run: ${{ inputs.script }} + shell: bash From 45a4d8ccc21a1a9af78c130c320dd2031b922997 Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Thu, 21 Dec 2023 23:42:58 +0100 Subject: [PATCH 08/11] refactor github actions iteration=9 fix error in github actions. for context see https://github.com/unstructuredstudio/zubhub/pull/1044 Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/actions/doctl_action/action.yaml | 7 ++++++- .github/workflows/locust.yml | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/actions/doctl_action/action.yaml b/.github/actions/doctl_action/action.yaml index 2859437d0..5afeebc53 100644 --- a/.github/actions/doctl_action/action.yaml +++ b/.github/actions/doctl_action/action.yaml @@ -7,7 +7,8 @@ inputs: required: true type: string outputs: - json_string: ${{ toJson(steps.script.outputs) }} + JSON_STRING: + value: ${{ steps.set_output.outputs.JSON_STRING }} runs: using: "composite" @@ -21,3 +22,7 @@ runs: id: script run: ${{ inputs.script }} shell: bash + + - name: Set output + id: set_output + run: echo "JSON_STRING=${{ toJson(env) }}" >> $GITHUB_OUTPUT diff --git a/.github/workflows/locust.yml b/.github/workflows/locust.yml index 9e55ae05e..cbee404e1 100644 --- a/.github/workflows/locust.yml +++ b/.github/workflows/locust.yml @@ -49,12 +49,12 @@ jobs: --region nyc1 --enable-monitoring --ssh-keys ${{ secrets.DO_PUBLIC_SSHKEY_FP }} --wait sleep 30s echo "NEW_DROPLET_IP=$(doctl compute droplet get locust \ - --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_OUTPUT + --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_ENV - name: Deploy locust uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master with: - host: ${{ fromJson(steps.create_droplet.outputs.json_string).NEW_DROPLET_IP }} + host: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).NEW_DROPLET_IP }} username: ${{ secrets.DO_BACKEND_USERNAME }} key: ${{ secrets.DO_SSHKEY }} script: | From ab5dd05a5831ca3ecca0f5822e5026e34918a215 Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Thu, 21 Dec 2023 23:57:55 +0100 Subject: [PATCH 09/11] refactor github actions iteration=10 fix error in github actions. for context see https://github.com/unstructuredstudio/zubhub/pull/1044 Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/actions/doctl_action/action.yaml | 6 +----- .github/workflows/locust.yml | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/actions/doctl_action/action.yaml b/.github/actions/doctl_action/action.yaml index bbe4f671e..0b85cf8af 100644 --- a/.github/actions/doctl_action/action.yaml +++ b/.github/actions/doctl_action/action.yaml @@ -8,7 +8,7 @@ inputs: type: string outputs: JSON_STRING: - value: ${{ steps.set_output.outputs.JSON_STRING }} + value: ${{ toJson(steps.script.outputs) }} runs: using: "composite" @@ -22,7 +22,3 @@ runs: id: script run: ${{ inputs.script }} shell: bash - - - name: Set output - id: set_output - run: echo "JSON_STRING=${{ toJson(env) }}" >> $GITHUB_OUTPUT diff --git a/.github/workflows/locust.yml b/.github/workflows/locust.yml index cbee404e1..68efc0fe3 100644 --- a/.github/workflows/locust.yml +++ b/.github/workflows/locust.yml @@ -49,7 +49,7 @@ jobs: --region nyc1 --enable-monitoring --ssh-keys ${{ secrets.DO_PUBLIC_SSHKEY_FP }} --wait sleep 30s echo "NEW_DROPLET_IP=$(doctl compute droplet get locust \ - --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_ENV + --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_OUTPUT - name: Deploy locust uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master From 3b47cacd8e0e6323dd38677e6909df877718b24a Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Fri, 22 Dec 2023 00:57:12 +0100 Subject: [PATCH 10/11] refactor github actions iteration=11 fix error in github actions. for context see https://github.com/unstructuredstudio/zubhub/pull/1044 Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/workflows/locust.yml | 98 +++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 41 deletions(-) diff --git a/.github/workflows/locust.yml b/.github/workflows/locust.yml index 68efc0fe3..913f9dde0 100644 --- a/.github/workflows/locust.yml +++ b/.github/workflows/locust.yml @@ -1,46 +1,57 @@ name: Build/Deploy/Destroy Locust Service -on: - push: - branches: - - master - paths: - - "locust/**" +# on: +# push: +# branches: +# - master +# paths: +# - "locust/**" + +# workflow_dispatch: +# inputs: +# action_type: +# description: +# "Do you want to build new locust container or deploy/destroy a locust service? type \ +# 'build' to build new container, 'deploy' to deploy a locust service, 'destroy' to \ +# destroy existing locust service. defaults to 'build' " +# required: true +# default: "deploy" - workflow_dispatch: - inputs: - action_type: - description: - "Do you want to build new locust container or deploy/destroy a locust service? type \ - 'build' to build new container, 'deploy' to deploy a locust service, 'destroy' to \ - destroy existing locust service. defaults to 'build' " - required: true - default: "deploy" +on: + pull_request: + types: [opened, reopened, synchronize] jobs: - build: - if: ${{github.event_name == 'push' || github.event.inputs.action_type == 'build' }} - runs-on: ubuntu-latest - steps: - - uses: unstructuredstudio/zubhub/.github/actions/checkout@master + # build: + # if: ${{github.event_name == 'push' || github.event.inputs.action_type == 'build' }} + # runs-on: ubuntu-latest + # steps: + # - uses: unstructuredstudio/zubhub/.github/actions/checkout@master - - name: Build and push locust - uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - token: ${{ secrets.DOCKERHUB_TOKEN }} - context: ./locust/ - file: ./locust/Dockerfile - push: true - tags: unstructuredstudio/zubhub-services_locust:latest + # - name: Build and push locust + # uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master + # with: + # username: ${{ secrets.DOCKERHUB_USERNAME }} + # token: ${{ secrets.DOCKERHUB_TOKEN }} + # context: ./locust/ + # file: ./locust/Dockerfile + # push: true + # tags: unstructuredstudio/zubhub-services_locust:latest deploy: - if: ${{ github.event.inputs.action_type == 'deploy' }} + # if: ${{ github.event.inputs.action_type == 'deploy' }} + if: | + ( + github.event.action == 'opened' || + github.event.action == 'reopened' || + github.event.action == 'synchronize' + ) runs-on: ubuntu-latest steps: - name: Create new DO droplet id: create_droplet - uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master + # uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master + uses: unstructuredstudio/zubhub/.github/actions/doctl_action@refactor_actions with: token: ${{ secrets.DO_ACCESS_TOKEN }} script: | @@ -49,7 +60,12 @@ jobs: --region nyc1 --enable-monitoring --ssh-keys ${{ secrets.DO_PUBLIC_SSHKEY_FP }} --wait sleep 30s echo "NEW_DROPLET_IP=$(doctl compute droplet get locust \ - --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_OUTPUT + --format PublicIPv4 --no-header)" >> $GITHUB_OUTPUT + + - name: print out last step's output + run: | + echo "print out last step's output:===================" + echo "${{ steps.create_droplet.outputs.JSON_STRING }}" - name: Deploy locust uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master @@ -61,12 +77,12 @@ jobs: docker run -d -p 8089:8089 unstructuredstudio/zubhub-services_locust:latest \ -f /mnt/locust/locustfile.py - destroy: - if: ${{ github.event.inputs.action_type == 'destroy' }} - runs-on: ubuntu-latest - steps: - - name: Destroy Target Droplet - uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master - with: - token: ${{ secrets.DO_ACCESS_TOKEN }} - script: doctl compute droplet delete -f locust + # destroy: + # if: ${{ github.event.inputs.action_type == 'destroy' }} + # runs-on: ubuntu-latest + # steps: + # - name: Destroy Target Droplet + # uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master + # with: + # token: ${{ secrets.DO_ACCESS_TOKEN }} + # script: doctl compute droplet delete -f locust From efce31a5a2bf2d88b03572bbdb94ff67d9455ad6 Mon Sep 17 00:00:00 2001 From: Ndibe Raymond Olisaemeka Date: Fri, 22 Dec 2023 01:24:39 +0100 Subject: [PATCH 11/11] refactor github actions iteration=11 * use matrix strategy for similar jobs to save time * update version of third-party actions * use composite actions Issue: #1030 Signed-off-by: Ndibe Raymond Olisaemeka --- .github/actions/comment_action/action.yml | 35 +++ .github/workflows/create_destroy_test_vm.yaml | 230 +++++++++++------- .github/workflows/locust.yml | 107 ++++---- .github/workflows/scale_backend.yml | 75 ++++-- 4 files changed, 276 insertions(+), 171 deletions(-) create mode 100644 .github/actions/comment_action/action.yml diff --git a/.github/actions/comment_action/action.yml b/.github/actions/comment_action/action.yml new file mode 100644 index 000000000..cad7ea906 --- /dev/null +++ b/.github/actions/comment_action/action.yml @@ -0,0 +1,35 @@ +name: Comment action +inputs: + token: + required: true + type: string + issue_number: + required: true + type: string + owner: + required: true + type: string + repo: + required: true + type: string + message: + required: true + type: string + +runs: + using: "composite" + steps: + - uses: actions/github-script@v5 + with: + github-token: ${{ inputs.token }} + script: | + const issue_number = ${{ inputs.issue_number }}; + const owner = ${{ inputs.owner }}; + const repo = ${{ inputs.repo }}; + const message = ${{ inputs.message }}; + await github.rest.issues.createComment({ + owner: owner, + repo: repo, + issue_number: issue_number, + body: message + }); diff --git a/.github/workflows/create_destroy_test_vm.yaml b/.github/workflows/create_destroy_test_vm.yaml index d958c9d33..e6974aca6 100644 --- a/.github/workflows/create_destroy_test_vm.yaml +++ b/.github/workflows/create_destroy_test_vm.yaml @@ -24,6 +24,17 @@ jobs: ) runs-on: ubuntu-latest steps: + - uses: unstructuredstudio/zubhub/.github/actions/comment_action@master + with: + token: ${{ secrets.GITHUB_TOKEN }} + issue_number: ${{ github.event.pull_request.number }} + owner: ${{ github.repository_owner }} + repo: ${{ github.repository }} + message: | + @${{ github.actor }} is creating a test VM for this PR 🚀🚀🚀 + This may take a few minutes so relax and grab a cup of coffee ☕ + We will notify you when the VM is ready. + - name: Get checkout commit ref and repo run: | @@ -51,49 +62,54 @@ jobs: ref: ${{ env.CHECKOUT_COMMIT_REF }} repository: ${{ env.CHECKOUT_REPO }} - - name: Install doctl - uses: digitalocean/action-doctl@v2 + - name: Create new droplet and DNS records + id: create_droplet + uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master with: token: ${{ secrets.DO_ACCESS_TOKEN }} + script: | - - name: Create new droplet and DNS records - run: | + # 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 + NEW_DROPLET_NAME=zubhub-test-${{ github.event.pull_request.number }} + FRONTEND_DOMAIN=${NEW_DROPLET_NAME} + API_DOMAIN=api.${FRONTEND_DOMAIN} + MEDIA_DOMAIN=media.${FRONTEND_DOMAIN} + echo "NEW_DROPLET_NAME=$NEW_DROPLET_NAME" >> $COMPOSITE_OUTPUT + echo "FRONTEND_DOMAIN=$FRONTEND_DOMAIN" >> $COMPOSITE_OUTPUT + echo "API_DOMAIN=$API_DOMAIN" >> $COMPOSITE_OUTPUT + echo "MEDIA_DOMAIN=$MEDIA_DOMAIN" >> $COMPOSITE_OUTPUT + # check if droplet already exists and exit script if it does + NEW_DROPLET_IP=$(doctl compute droplet get $NEW_DROPLET_NAME \ + --format PublicIPv4 --no-header 2>/dev/null || true) + if [[ -n "$NEW_DROPLET_IP" ]] ; then + echo "Droplet already exists. Save droplet IP to env variable and exit..." + echo "NEW_DROPLET_IP=$NEW_DROPLET_IP" >> $COMPOSITE_OUTPUT + exit 0 + fi - NEW_DROPLET_NAME=zubhub-test-${{ github.event.pull_request.number }} - FRONTEND_DOMAIN=${NEW_DROPLET_NAME} - API_DOMAIN=api.${FRONTEND_DOMAIN} - MEDIA_DOMAIN=media.${FRONTEND_DOMAIN} - echo "NEW_DROPLET_NAME=$NEW_DROPLET_NAME" >> $GITHUB_ENV - echo "FRONTEND_DOMAIN=$FRONTEND_DOMAIN" >> $GITHUB_ENV - echo "API_DOMAIN=$API_DOMAIN" >> $GITHUB_ENV - echo "MEDIA_DOMAIN=$MEDIA_DOMAIN" >> $GITHUB_ENV - # check if droplet already exists and exit script if it does - NEW_DROPLET_IP=$(doctl compute droplet get $NEW_DROPLET_NAME \ - --format PublicIPv4 --no-header 2>/dev/null || true) - if [[ -n "$NEW_DROPLET_IP" ]] ; then - echo "Droplet already exists. Save droplet IP to env variable and exit..." - echo "NEW_DROPLET_IP=$NEW_DROPLET_IP" >> $GITHUB_ENV - exit 0 - fi - - # create new droplet - doctl compute droplet create $NEW_DROPLET_NAME --image \ - ${{ secrets.ZUBHUB_TEST_SNAPSHOT_ID }} --tag-name zubhub-test --size s-1vcpu-1gb \ - --region nyc1 --enable-monitoring --ssh-keys ${{ secrets.DO_PUBLIC_SSHKEY_FP }} --wait - sleep 30s - NEW_DROPLET_IP=$(doctl compute droplet get $NEW_DROPLET_NAME \ - --format PublicIPv4 --no-header) - echo "NEW_DROPLET_IP=$NEW_DROPLET_IP" >> $GITHUB_ENV - - # we only need records for frontend, media server and api server - doctl compute domain records create unstructured.studio --record-type A --record-name \ - $FRONTEND_DOMAIN --record-data $NEW_DROPLET_IP --record-ttl 600 - doctl compute domain records create unstructured.studio --record-type A --record-name \ - $API_DOMAIN --record-data $NEW_DROPLET_IP --record-ttl 600 - doctl compute domain records create unstructured.studio --record-type A --record-name \ - $MEDIA_DOMAIN --record-data $NEW_DROPLET_IP --record-ttl 600 + # create new droplet + doctl compute droplet create $NEW_DROPLET_NAME --image \ + ${{ secrets.ZUBHUB_TEST_SNAPSHOT_ID }} --tag-name zubhub-test --size s-1vcpu-1gb \ + --region nyc1 --enable-monitoring --ssh-keys ${{ secrets.DO_PUBLIC_SSHKEY_FP }} --wait + sleep 30s + NEW_DROPLET_IP=$(doctl compute droplet get $NEW_DROPLET_NAME \ + --format PublicIPv4 --no-header) + echo "NEW_DROPLET_IP=$NEW_DROPLET_IP" >> $COMPOSITE_OUTPUT + + # we only need records for frontend, media server and api server + doctl compute domain records create unstructured.studio --record-type A --record-name \ + $FRONTEND_DOMAIN --record-data $NEW_DROPLET_IP --record-ttl 600 + doctl compute domain records create unstructured.studio --record-type A --record-name \ + $API_DOMAIN --record-data $NEW_DROPLET_IP --record-ttl 600 + doctl compute domain records create unstructured.studio --record-type A --record-name \ + $MEDIA_DOMAIN --record-data $NEW_DROPLET_IP --record-ttl 600 - uses: unstructuredstudio/zubhub/.github/actions/scp_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 }} @@ -103,6 +119,11 @@ jobs: - name: Set output id: set_output + env: + NEW_DROPLET_IP: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).NEW_DROPLET_IP }} + FRONTEND_DOMAIN: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).FRONTEND_DOMAIN }} + API_DOMAIN: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).API_DOMAIN }} + MEDIA_DOMAIN: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).MEDIA_DOMAIN }} run: | echo "NEW_DROPLET_IP=$NEW_DROPLET_IP" >> $GITHUB_OUTPUT echo "FRONTEND_DOMAIN=$FRONTEND_DOMAIN" >> $GITHUB_OUTPUT @@ -247,6 +268,17 @@ jobs: sed -i '/"build":/ s/node /NODE_ENV=development node /' \ /home/zubhub/zubhub_frontend/zubhub/package.json sudo bash /home/zubhub/test_vm_deployment/deploy_test_fullstack.sh + + - uses: unstructuredstudio/zubhub/.github/actions/comment_action@master + with: + token: ${{ secrets.GITHUB_TOKEN }} + issue_number: ${{ github.event.pull_request.number }} + owner: ${{ github.repository_owner }} + repo: ${{ github.repository }} + message: | + Test VM is ready ✔✔✔ + You can access it with the url: + https://${{env.FRONTEND_DOMAIN}}.unstructured.studio ###################################################################################### ################################################################################# @@ -262,61 +294,85 @@ jobs: ) && contains(github.event.pull_request.labels.*.name, 'create-test-vm') != true runs-on: ubuntu-latest steps: - - name: Install doctl - uses: digitalocean/action-doctl@v2 + - uses: unstructuredstudio/zubhub/.github/actions/comment_action@master with: - token: ${{ secrets.DO_ACCESS_TOKEN }} + token: ${{ secrets.GITHUB_TOKEN }} + issue_number: ${{ github.event.pull_request.number }} + owner: ${{ github.repository_owner }} + repo: ${{ github.repository }} + message: | + @${{ github.actor }} is deleting test VM ... - name: Get pr number and droplet ip - run: | + id: get_pr_number_and_droplet_ip + uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master + with: + token: ${{ secrets.DO_ACCESS_TOKEN }} + script: | - # for events like unlabelled, github.event.pull_request.number is available - PR_NUMBER=${{ github.event.pull_request.number }} - if [[ -z "$PR_NUMBER" ]] ; then - # github.event.pull_request.number is not available in closed event - sudo apt-get install jq -y - PR_NUMBER=$(curl --silent --show-error -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ - https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/pulls | \ - jq -r '.[0].number') - fi - - if [[ -z "$PR_NUMBER" ]] ; then - echo "PR_NUMBER is not available. Exiting..." - exit 1 - fi - DROPLET_NAME=zubhub-test-$PR_NUMBER - - # Get droplet ip - DROPLET_IP=$(doctl compute droplet get $DROPLET_NAME \ - --format PublicIPv4 --no-header 2>/dev/null || true) - if [[ -z "$DROPLET_IP" ]] ; then - echo "Droplet does not exist. Exiting..." - exit 0 - fi - echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV - echo "DROPLET_IP=$DROPLET_IP" >> $GITHUB_ENV - echo "DROPLET_NAME=$DROPLET_NAME" >> $GITHUB_ENV - - - name: delete DNS records - run: | + # 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 + # for events like unlabelled, github.event.pull_request.number is available + PR_NUMBER=${{ github.event.pull_request.number }} + if [[ -z "$PR_NUMBER" ]] ; then + # github.event.pull_request.number is not available in closed event + sudo apt-get install jq -y + PR_NUMBER=$(curl --silent --show-error -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/pulls | \ + jq -r '.[0].number') + fi + + if [[ -z "$PR_NUMBER" ]] ; then + echo "PR_NUMBER is not available. Exiting..." + exit 1 + fi + DROPLET_NAME=zubhub-test-$PR_NUMBER + + # Get droplet ip + DROPLET_IP=$(doctl compute droplet get $DROPLET_NAME \ + --format PublicIPv4 --no-header 2>/dev/null || true) + if [[ -z "$DROPLET_IP" ]] ; then + echo "Droplet does not exist. Exiting..." + exit 0 + fi + echo "DROPLET_IP=$DROPLET_IP" >> $COMPOSITE_OUTPUT + echo "DROPLET_NAME=$DROPLET_NAME" >> $COMPOSITE_OUTPUT + + - name: delete DNS records and test droplet + uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master + env: + DROPLET_IP: ${{ fromJson(steps.get_pr_number_and_droplet_ip.outputs.JSON_STRING).DROPLET_IP }} + DROPLET_NAME: ${{ fromJson(steps.get_pr_number_and_droplet_ip.outputs.JSON_STRING).DROPLET_NAME }} + with: + token: ${{ secrets.DO_ACCESS_TOKEN }} + script: | - # Get the domain records - records=$(doctl compute domain records list unstructured.studio --format ID,Data --no-header) - # Loop through the records - while IFS= read -r record; do - # Split the record into ID and IP - ID=$(echo $record | cut -d' ' -f1) - IP=$(echo $record | cut -d' ' -f2) - - # Delete the record if it points to the droplet IP - if [[ -n "$ID" && "$IP" == "${{env.DROPLET_IP}}" ]]; then - doctl compute domain records delete unstructured.studio $ID --force + # Get the domain records + records=$(doctl compute domain records list unstructured.studio --format ID,Data --no-header) + # Loop through the records + while IFS= read -r record; do + # Split the record into ID and IP + ID=$(echo $record | cut -d' ' -f1) + IP=$(echo $record | cut -d' ' -f2) + + # Delete the record if it points to the droplet IP + if [[ -n "$ID" && "$IP" == "${{env.DROPLET_IP}}" ]]; then + doctl compute domain records delete unstructured.studio $ID --force + fi + done <<< "$records" + # delete test droplet + if [[ -n "${{ env.DROPLET_NAME }}" ]] ; then + doctl compute droplet delete ${{ env.DROPLET_NAME }} --force fi - done <<< "$records" - - name: Delete test droplet - run: | - if [[ -n "${{ env.DROPLET_NAME }}" ]] ; then - doctl compute droplet delete ${{ env.DROPLET_NAME }} --force - fi + - uses: unstructuredstudio/zubhub/.github/actions/comment_action@master + with: + token: ${{ secrets.GITHUB_TOKEN }} + issue_number: ${{ github.event.pull_request.number }} + owner: ${{ github.repository_owner }} + repo: ${{ github.repository }} + message: | + Test VM deleted ✔✔✔ ###################################################################################### diff --git a/.github/workflows/locust.yml b/.github/workflows/locust.yml index 913f9dde0..0d5da4a03 100644 --- a/.github/workflows/locust.yml +++ b/.github/workflows/locust.yml @@ -1,88 +1,79 @@ name: Build/Deploy/Destroy Locust Service -# on: -# push: -# branches: -# - master -# paths: -# - "locust/**" - -# workflow_dispatch: -# inputs: -# action_type: -# description: -# "Do you want to build new locust container or deploy/destroy a locust service? type \ -# 'build' to build new container, 'deploy' to deploy a locust service, 'destroy' to \ -# destroy existing locust service. defaults to 'build' " -# required: true -# default: "deploy" - on: - pull_request: - types: [opened, reopened, synchronize] + push: + branches: + - master + paths: + - "locust/**" + + workflow_dispatch: + inputs: + action_type: + description: + "Do you want to build new locust container or deploy/destroy a locust service? type \ + 'build' to build new container, 'deploy' to deploy a locust service, 'destroy' to \ + destroy existing locust service. defaults to 'build' " + required: true + default: "deploy" jobs: - # build: - # if: ${{github.event_name == 'push' || github.event.inputs.action_type == 'build' }} - # runs-on: ubuntu-latest - # steps: - # - uses: unstructuredstudio/zubhub/.github/actions/checkout@master + build: + if: ${{github.event_name == 'push' || github.event.inputs.action_type == 'build' }} + runs-on: ubuntu-latest + steps: + - uses: unstructuredstudio/zubhub/.github/actions/checkout@master - # - name: Build and push locust - # uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master - # with: - # username: ${{ secrets.DOCKERHUB_USERNAME }} - # token: ${{ secrets.DOCKERHUB_TOKEN }} - # context: ./locust/ - # file: ./locust/Dockerfile - # push: true - # tags: unstructuredstudio/zubhub-services_locust:latest + - name: Build and push locust + uses: unstructuredstudio/zubhub/.github/actions/docker_build_and_push@master + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + token: ${{ secrets.DOCKERHUB_TOKEN }} + context: ./locust/ + file: ./locust/Dockerfile + push: true + tags: unstructuredstudio/zubhub-services_locust:latest deploy: - # if: ${{ github.event.inputs.action_type == 'deploy' }} - if: | - ( - github.event.action == 'opened' || - github.event.action == 'reopened' || - github.event.action == 'synchronize' - ) + if: ${{ github.event.inputs.action_type == 'deploy' }} runs-on: ubuntu-latest steps: - name: Create new DO droplet id: create_droplet - # uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master - uses: unstructuredstudio/zubhub/.github/actions/doctl_action@refactor_actions + 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 create locust --image \ ${{ secrets.SOURCE_SNAPSHOT_ID }} --tag-name zubhub-locust --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 locust \ - --format PublicIPv4 --no-header)" >> $GITHUB_OUTPUT - - - name: print out last step's output - run: | - echo "print out last step's output:===================" - echo "${{ steps.create_droplet.outputs.JSON_STRING }}" + --format PublicIPv4 --no-header)" >> $COMPOSITE_OUTPUT - name: Deploy locust uses: unstructuredstudio/zubhub/.github/actions/ssh_action@master + env: + NEW_DROPLET_IP: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).NEW_DROPLET_IP }} with: - host: ${{ fromJson(steps.create_droplet.outputs.JSON_STRING).NEW_DROPLET_IP }} + host: ${{ env.NEW_DROPLET_IP }} username: ${{ secrets.DO_BACKEND_USERNAME }} key: ${{ secrets.DO_SSHKEY }} script: | docker run -d -p 8089:8089 unstructuredstudio/zubhub-services_locust:latest \ -f /mnt/locust/locustfile.py - # destroy: - # if: ${{ github.event.inputs.action_type == 'destroy' }} - # runs-on: ubuntu-latest - # steps: - # - name: Destroy Target Droplet - # uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master - # with: - # token: ${{ secrets.DO_ACCESS_TOKEN }} - # script: doctl compute droplet delete -f locust + destroy: + if: ${{ github.event.inputs.action_type == 'destroy' }} + runs-on: ubuntu-latest + steps: + - name: Destroy Target Droplet + uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master + with: + token: ${{ secrets.DO_ACCESS_TOKEN }} + script: doctl compute droplet delete -f locust diff --git a/.github/workflows/scale_backend.yml b/.github/workflows/scale_backend.yml index 615b84a31..bc491df47 100644 --- a/.github/workflows/scale_backend.yml +++ b/.github/workflows/scale_backend.yml @@ -13,25 +13,31 @@ jobs: if: ${{ github.event.inputs.scaling_type == 'up' }} runs-on: ubuntu-latest steps: - - name: Install doctl - uses: digitalocean/action-doctl@v2 + - name: Create new droplet + id: create_droplet + uses: unstructuredstudio/zubhub/.github/actions/doctl_action@master with: token: ${{ secrets.DO_ACCESS_TOKEN }} + script: | - - name: Create new droplet - run: | - doctl compute droplet list 'zubhub-services*' > droplets.txt - droplets_count=`wc -l < droplets.txt` - echo "TARGET_DROPLETS_COUNT=$(($droplets_count))" >> $GITHUB_ENV - 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)) \ - --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_ENV - + # 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 }} @@ -40,6 +46,8 @@ jobs: - 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 }} @@ -50,20 +58,25 @@ jobs: if: ${{ github.event.inputs.scaling_type == 'down' }} runs-on: ubuntu-latest steps: - - name: Install doctl - uses: digitalocean/action-doctl@v2 + - name: Get target Droplet IP and Droplet Count + id: get_target_droplet_ip with: token: ${{ secrets.DO_ACCESS_TOKEN }} + script: | - - name: Get target Droplet IP and Droplet Count - run: | - doctl compute droplet list 'zubhub-services*' > droplets.txt - droplets_count=`wc -l < droplets.txt` - echo "TARGET_DROPLETS_COUNT=$(($droplets_count - 2))" >> $GITHUB_ENV - echo "TARGET_DROPLET_IP=$(doctl compute droplet get zubhub-services-$(($droplets_count-2)) \ - --template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_ENV + # 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: @@ -73,6 +86,9 @@ jobs: 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: @@ -82,12 +98,19 @@ jobs: 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 }} - run: | - doctl compute droplet delete -f zubhub-services-$TARGET_DROPLETS_COUNT - sleep 10s + 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: