|
| 1 | +name: Deploy and Test Monitoring Stack |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +env: |
| 9 | + DO_API_URL: "https://api.digitalocean.com/v2/droplets" |
| 10 | + DROPLET_NAME: "monitoring-stack-ansible-droplet" |
| 11 | + DROPLET_SIZE: "s-2vcpu-4gb-120gb-intel" |
| 12 | + DROPLET_REGION: "fra1" |
| 13 | + DROPLET_IMAGE: "ubuntu-22-04-x64" |
| 14 | + VPC_UUID: "${{ secrets.VPC_ID }}" |
| 15 | + |
| 16 | +jobs: |
| 17 | + create-run-test-destroy: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + # Checkout the code |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v3 |
| 23 | + |
| 24 | + # Create a DigitalOcean Droplet |
| 25 | + - name: Create DigitalOcean Droplet |
| 26 | + id: create_droplet |
| 27 | + run: | |
| 28 | + curl -X POST -H 'Content-Type: application/json' \ |
| 29 | + -H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" \ |
| 30 | + -d '{ |
| 31 | + "name": "${{ env.DROPLET_NAME }}", |
| 32 | + "size": "${{ env.DROPLET_SIZE }}", |
| 33 | + "region": "${{ env.DROPLET_REGION }}", |
| 34 | + "image": "${{ env.DROPLET_IMAGE }}", |
| 35 | + "vpc_uuid": "${{ env.VPC_UUID }}", |
| 36 | + "ssh_keys": ["${{ secrets.SSH_KEY_ID }}"] |
| 37 | + }' \ |
| 38 | + $DO_API_URL |
| 39 | + echo "::set-output name=droplet_id::$(jq -r '.droplet.id')" |
| 40 | +
|
| 41 | + # Wait for the droplet to become active |
| 42 | + - name: Wait for Droplet to be Active |
| 43 | + run: | |
| 44 | + while [ "$(curl -X GET -H 'Content-Type: application/json' \ |
| 45 | + -H 'Authorization: Bearer ${{ secrets.DO_API_TOKEN }}' \ |
| 46 | + "https://api.digitalocean.com/v2/droplets/${{ steps.create_droplet.outputs.droplet_id }}" | jq -r '.droplet.status')" != "active" ]; do |
| 47 | + echo "Waiting for droplet to be active..." |
| 48 | + sleep 10 |
| 49 | + done |
| 50 | +
|
| 51 | + # Fetch Droplet IP Address |
| 52 | + - name: Fetch Droplet IP Address |
| 53 | + id: get_ip |
| 54 | + run: | |
| 55 | + IP_ADDRESS=$(curl -X GET -H 'Content-Type: application/json' \ |
| 56 | + -H 'Authorization: Bearer ${{ secrets.DO_API_TOKEN }}' \ |
| 57 | + "https://api.digitalocean.com/v2/droplets/${{ steps.create_droplet.outputs.droplet_id }}" | jq -r '.droplet.networks.v4[0].ip_address') |
| 58 | + echo "::set-output name=ip::${IP_ADDRESS}" |
| 59 | +
|
| 60 | + # Run the Ansible Playbook on the droplet |
| 61 | + - name: Run Ansible Playbook |
| 62 | + uses: dawidd6/action-ansible-playbook@v2 |
| 63 | + with: |
| 64 | + playbook: playbooks/site.yml |
| 65 | + inventory: inventory/inventory.yml |
| 66 | + extra_args: "-i ${{ steps.get_ip.outputs.ip }}, -u root --private-key ${{ secrets.SSH_PRIVATE_KEY }}" |
| 67 | + |
| 68 | + # Install pytest and run tests |
| 69 | + - name: Install and Run pytest |
| 70 | + run: | |
| 71 | + pip install pytest testinfra |
| 72 | + pytest --ansible-inventory=./inventory/inventory.yml --force-ansible --connection=ansible |
| 73 | +
|
| 74 | + # Destroy the droplet after the test |
| 75 | + - name: Destroy DigitalOcean Droplet |
| 76 | + run: | |
| 77 | + curl -X DELETE -H 'Content-Type: application/json' \ |
| 78 | + -H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" \ |
| 79 | + "https://api.digitalocean.com/v2/droplets/${{ steps.create_droplet.outputs.droplet_id }}" |
| 80 | +
|
| 81 | + # Archive test results |
| 82 | + - name: Archive Test Results |
| 83 | + uses: actions/upload-artifact@v3 |
| 84 | + with: |
| 85 | + name: pytest-results |
| 86 | + path: pytest_results/ |
0 commit comments