Skip to content

Commit

Permalink
feat: implementing CI for the code (#1)
Browse files Browse the repository at this point in the history
* bug: fix stuck at waiting for active status

* Update deploy-test-monitoring-DO.yml

* creating inventory on the fly

* Update deploy-test-monitoring-DO.yml

* adding ssh & inventory configurations in CI

* adding fetch ip step back

* Update deploy-test-monitoring-DO.yml

* Update deploy-test-monitoring-DO.yml

* cancel the archive step, to be added later
  • Loading branch information
Aazme authored Oct 24, 2024
1 parent 36ccebd commit 9412ef0
Showing 1 changed file with 46 additions and 30 deletions.
76 changes: 46 additions & 30 deletions .github/workflows/deploy-test-monitoring-DO.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ jobs:
create-run-test-destroy:
runs-on: ubuntu-latest
steps:
# Checkout the code
- name: Checkout code
uses: actions/checkout@v3

# Create a DigitalOcean Droplet
- name: Create DigitalOcean Droplet
id: create_droplet
run: |
curl -X POST -H 'Content-Type: application/json' \
DROPLET_ID=$(curl -X POST -H 'Content-Type: application/json' \
-H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" \
-d '{
"name": "${{ env.DROPLET_NAME }}",
Expand All @@ -36,52 +34,70 @@ jobs:
"vpc_uuid": "${{ env.VPC_UUID }}",
"ssh_keys": ["${{ secrets.SSH_KEY_ID }}"]
}' \
$DO_API_URL
echo "::set-output name=droplet_id::$(jq -r '.droplet.id')"
$DO_API_URL | jq -r '.droplet.id')
echo "droplet_id=${DROPLET_ID}" >> $GITHUB_ENV
# Wait for the droplet to become active
- name: Wait for Droplet to be Active
run: |
while [ "$(curl -X GET -H 'Content-Type: application/json' \
-H 'Authorization: Bearer ${{ secrets.DO_API_TOKEN }}' \
"https://api.digitalocean.com/v2/droplets/${{ steps.create_droplet.outputs.droplet_id }}" | jq -r '.droplet.status')" != "active" ]; do
echo "Waiting for droplet to be active..."
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
done
fi
# Fetch Droplet IP Address
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/${{ steps.create_droplet.outputs.droplet_id }}" | jq -r '.droplet.networks.v4[0].ip_address')
echo "::set-output name=ip::${IP_ADDRESS}"
# Run the Ansible Playbook on the droplet
-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
inventory: inventory/inventory.yml
extra_args: "-i ${{ steps.get_ip.outputs.ip }}, -u root --private-key ${{ secrets.SSH_PRIVATE_KEY }}"

# Install pytest and run tests
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 install pytest testinfra
pytest --ansible-inventory=./inventory/inventory.yml --force-ansible --connection=ansible
# Destroy the droplet after the test
- name: Destroy DigitalOcean Droplet
run: |
curl -X DELETE -H 'Content-Type: application/json' \
-H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" \
"https://api.digitalocean.com/v2/droplets/${{ steps.create_droplet.outputs.droplet_id }}"
# Archive test results
- name: Archive Test Results
uses: actions/upload-artifact@v3
with:
name: pytest-results
path: pytest_results/
"https://api.digitalocean.com/v2/droplets/${{ env.droplet_id }}"

0 comments on commit 9412ef0

Please sign in to comment.