#!/bin/bash source /opt/environment/.env ## This script triggers a CI/CD pipeline. # It then restores a backup using `restore-backup.service` systemd service ## This script requires `restore-backup.service` in `/etc/systemd/system` ## Dependencies (in service.yaml `packages` section) # - jq # - curl ## Environment variables # GITEA_SERVICE_APPLICATION_TOKEN: Gitea API token. # GITEA_SERVICE_REPOSITORY: repository where the project resides. # GITEA_INSTANCE_URL: Gitea url. # SERVICE_DATABASE_CONTAINER_NAME: Container name for database. # GITEA_WORKFLOW_NAME: deploy.yaml curl -X POST -H "Authorization: token $GITEA_SERVICE_APPLICATION_TOKEN" \ -H "Content-Type: application/json" \ $GITEA_INSTANCE_URL/api/v1/repos/$GITEA_SERVICE_REPOSITORY/actions/workflows/$GITEA_WORKFLOW_NAME/dispatches \ -d '{"ref": "main", "inputs": {"ref": "main"}}' RUN_ID=$(curl -s -H "Authorization: token $GITEA_SERVICE_APPLICATION_TOKEN" \ $GITEA_INSTANCE_URL/api/v1/repos/$GITEA_SERVICE_REPOSITORY/actions/runs \ | jq -r '.workflow_runs | sort_by(.created_at) | .[0].id') while true; do STATUS=$(curl -s -H "Authorization: token $GITEA_SERVICE_APPLICATION_TOKEN" \ $GITEA_INSTANCE_URL/api/v1/repos/$GITEA_SERVICE_REPOSITORY/actions/runs/$RUN_ID \ | jq -r '.status') if [ "$STATUS" = "completed" ]; then CONCLUSION=$(curl -s -H "Authorization: token $GITEA_SERVICE_APPLICATION_TOKEN" \ $GITEA_INSTANCE_URL/api/v1/repos/$GITEA_SERVICE_REPOSITORY/actions/runs/$RUN_ID \ | jq -r '.conclusion') echo "Workflow finished with status: $CONCLUSION" break fi echo "Waiting 10 seconds..." sleep 10 done if [ "$CONCLUSION" = "success" ]; then echo "Launching command..." while [ "$(docker inspect -f '{{.State.Running}}' $SERVICE_DATABASE_CONTAINER_NAME-1 2>/dev/null)" != "true" ]; do echo "Waiting database container status" sleep 5 done systemctl start restore-backup.service else echo "Workflow failed or was cancelled, aborting." exit 1 fi