Skip to content

Commit

Permalink
refactor github actions iteration=11 (#1052)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
NdibeRaymond authored Dec 22, 2023
1 parent 140384e commit 6f9dda3
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 115 deletions.
35 changes: 35 additions & 0 deletions .github/actions/comment_action/action.yml
Original file line number Diff line number Diff line change
@@ -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
});
230 changes: 143 additions & 87 deletions .github/workflows/create_destroy_test_vm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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 }}
Expand All @@ -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
Expand Down Expand Up @@ -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
######################################################################################

#################################################################################
Expand All @@ -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 ✔✔✔
######################################################################################
11 changes: 9 additions & 2 deletions .github/workflows/locust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,24 @@ jobs:
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 \
--template "{{(index .Networks.V4 1).IPAddress}}")" >> $GITHUB_ENV
--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: |
Expand Down
Loading

0 comments on commit 6f9dda3

Please sign in to comment.