Delete Droplets #1400
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: Delete Droplets | |
on: | |
workflow_run: | |
workflows: [Deploy and Test Branch] | |
types: | |
- completed | |
jobs: | |
on-success: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
steps: | |
- name: get logs | |
uses: dawidd6/action-download-artifact@v6 | |
with: | |
run_id: ${{ github.event.workflow_run.id}} | |
if_no_artifact_found: warn | |
- name: get server name | |
run: | | |
echo "SERVER=$(cat ./server/server)" >> $GITHUB_ENV | |
- name: get PR | |
env: | |
GH_TOKEN: ${{ github.token }} | |
run: | | |
echo "pull_request=$(gh api -H 'Accept: application/vnd.github+json' -H 'X-Github-Api-Version: 2022-11-28' \ | |
/repos/${{ github.repository}}/actions/runs/${{ github.event.workflow_run.id }} | jq .pull_requests[0].number)" >> $GITHUB_ENV | |
- name: discord success message | |
uses: appleboy/discord-action@master | |
with: | |
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }} | |
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }} | |
color: "#42f545" | |
username: "GitHub Bot" | |
message: " ${{ github.repository }}:${{ github.event.workflow_run.name }}:PR ${{ env.pull_request }} was successful: droplets from this workflow (tag ${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt}}) will be deleted in 15 min" | |
file: ./results/results.log | |
- name: delete droplets | |
if: success() || failure() | |
run: | | |
sleep 1m | |
response=$(curl -X DELETE \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ | |
-w "\n%{http_code}" \ | |
"https://api.digitalocean.com/v2/droplets?tag_name=$TAG") | |
status_code=$(echo "$response" | tail -n1) | |
body=$(echo "$response" | sed '$d') | |
echo "Response body: $body" | |
echo "Status code: $status_code" | |
if [ "$status_code" -eq 204 ]; then | |
echo "Droplets deleted successfully" | |
else | |
echo "Failed to delete droplets. Status code: $status_code" | |
exit 1 | |
fi | |
sleep 1m | |
env: | |
DIGITALOCEAN_TOKEN: ${{ secrets.DO_TEST_TOKEN }} | |
TAG: ${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }} | |
- name: mark server as available | |
if: success() || failure() | |
uses: appleboy/ssh-action@master | |
with: | |
host: server.${{ env.SERVER }}.clustercat.com | |
username: root | |
key: ${{ secrets.TESTING_SSH_KEY }} | |
script: | | |
rm /tmp/branchtest | |
on-failure: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
steps: | |
- name: get logs | |
uses: dawidd6/action-download-artifact@v6 | |
with: | |
run_id: ${{ github.event.workflow_run.id}} | |
if_no_artifact_found: warn | |
- name: get server name | |
run: | | |
echo "SERVER=$(cat ./server/server)" >> $GITHUB_ENV | |
- name: get PR | |
env: | |
GH_TOKEN: ${{ github.token }} | |
run: | | |
echo "pull_request=$(gh api -H 'Accept: application/vnd.github+json' -H 'X-Github-Api-Version: 2022-11-28' \ | |
/repos/${{ github.repository}}/actions/runs/${{ github.event.workflow_run.id }} | jq .pull_requests[0].number)" >> $GITHUB_ENV | |
- name: discord failure message | |
uses: appleboy/discord-action@master | |
with: | |
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }} | |
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }} | |
color: "#990000" | |
username: "GitHub Bot" | |
message: " ${{ github.repository}}:${{ github.event.workflow_run.name }}:PR ${{ env.pull_request}} failed: droplets from this workflow (tag ${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }}) will be deleted in 3 hours" | |
file: ./results/results.log | |
- name: discord error message | |
uses: appleboy/discord-action@master | |
with: | |
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }} | |
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }} | |
color: "#990000" | |
username: "GitHub Bot" | |
message: "errors from ${{ github.event.workflow_run.name }}" | |
file: ./results/errors.log | |
- name: delete droplets | |
if: success() || failure() | |
run: | | |
sleep 1m | |
response=$(curl -X DELETE \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ | |
-w "\n%{http_code}" \ | |
"https://api.digitalocean.com/v2/droplets?tag_name=$TAG") | |
status_code=$(echo "$response" | tail -n1) | |
body=$(echo "$response" | sed '$d') | |
echo "Response body: $body" | |
echo "Status code: $status_code" | |
if [ "$status_code" -eq 204 ]; then | |
echo "Droplets deleted successfully" | |
else | |
echo "Failed to delete droplets. Status code: $status_code" | |
exit 1 | |
fi | |
sleep 1m | |
env: | |
DIGITALOCEAN_TOKEN: ${{ secrets.DO_TEST_TOKEN }} | |
TAG: ${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }} | |
- name: mark server as available | |
if: success() || failure() | |
uses: appleboy/ssh-action@master | |
with: | |
host: server.${{ env.SERVER }}.clustercat.com | |
username: root | |
key: ${{ secrets.TESTING_SSH_KEY }} | |
script: | | |
rm /tmp/branchtest |