optimization: increase alerts firing to be 30s instead of 10s #22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy and Test Monitoring Stack | |
on: | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
env: | |
DO_API_URL: "https://api.digitalocean.com/v2/droplets" | |
DROPLET_NAME: "monitoring-stack-ansible-droplet" | |
DROPLET_SIZE: "s-2vcpu-4gb-120gb-intel" | |
DROPLET_REGION: "fra1" | |
DROPLET_IMAGE: "ubuntu-22-04-x64" | |
VPC_UUID: "${{ secrets.VPC_ID }}" | |
jobs: | |
create-run-test-destroy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Create DigitalOcean Droplet | |
id: create_droplet | |
run: | | |
DROPLET_ID=$(curl -X POST -H 'Content-Type: application/json' \ | |
-H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" \ | |
-d '{ | |
"name": "${{ env.DROPLET_NAME }}", | |
"size": "${{ env.DROPLET_SIZE }}", | |
"region": "${{ env.DROPLET_REGION }}", | |
"image": "${{ env.DROPLET_IMAGE }}", | |
"vpc_uuid": "${{ env.VPC_UUID }}", | |
"ssh_keys": ["${{ secrets.SSH_KEY_ID }}"] | |
}' \ | |
$DO_API_URL | jq -r '.droplet.id') | |
echo "droplet_id=${DROPLET_ID}" >> $GITHUB_ENV | |
- name: Wait for Droplet to be Active | |
run: | | |
for i in {1..15}; do | |
DROPLET_STATUS=$(curl -X GET -H 'Content-Type: application/json' \ | |
-H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" \ | |
"https://api.digitalocean.com/v2/droplets/${{ env.droplet_id }}" | jq -r '.droplet.status') | |
if [ "$DROPLET_STATUS" == "active" ]; then | |
echo "Droplet is active!" | |
sleep 60 | |
break | |
else | |
echo "Waiting for droplet to be active... Current status: $DROPLET_STATUS" | |
sleep 10 | |
fi | |
if [ "$i" -eq 15 ]; then | |
echo "Droplet did not become active in time." | |
exit 1 | |
fi | |
done | |
- name: Fetch Droplet IP Address | |
id: get_ip | |
run: | | |
IP_ADDRESS=$(curl -X GET -H 'Content-Type: application/json' \ | |
-H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" \ | |
"https://api.digitalocean.com/v2/droplets/${{ env.droplet_id }}" | jq -r '.droplet.networks.v4[0].ip_address') | |
echo "ip=${IP_ADDRESS}" >> $GITHUB_ENV | |
- name: Create SSH Private Key File | |
run: | | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ./lido-task-dg | |
chmod 600 ./lido-task-dg | |
- name: Update Inventory File with Droplet IP | |
run: | | |
sed -i "s/ansible_host: .*/ansible_host: ${{ env.ip }}/" inventory/inventory.yml | |
cat inventory/inventory.yml | |
- name: Run Ansible Playbook | |
uses: dawidd6/action-ansible-playbook@v2 | |
with: | |
playbook: playbooks/site.yml | |
directory: ./ | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
inventory: | | |
all: | |
hosts: | |
digitalocean: | |
ansible_host: ${{ env.ip }} | |
ansible_user: root | |
ansible_ssh_private_key_file: ./lido-task-dg | |
- name: Install and Run pytest | |
run: | | |
pip uninstall -y testinfra | |
pip install pytest pytest-testinfra | |
pytest --ansible-inventory=./inventory/inventory.yml --force-ansible --connection=ansible | |
- name: Destroy DigitalOcean Droplet | |
if: always() # Ensures this step runs even if previous steps fail | |
run: | | |
if [ -n "${{ env.droplet_id }}" ]; then | |
curl -X DELETE -H 'Content-Type: application/json' \ | |
-H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" \ | |
"https://api.digitalocean.com/v2/droplets/${{ env.droplet_id }}" | |
else | |
echo "Droplet ID not set; skipping deletion." | |
fi |