Created
August 19, 2024 06:36
-
-
Save kitgrose/7ffc20e39430f67e1ff1aec58141db95 to your computer and use it in GitHub Desktop.
Trigger Cloudways Git Deployment
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
#!/bin/bash | |
[email protected] | |
CLOUDWAYS_API_KEY=xxxxxxxx | |
CLOUDWAYS_API_URL=https://api.cloudways.com/api/v1 | |
SERVER_ID=123456 | |
APP_ID=987654 | |
GIT_BRANCH=release | |
# Fetch OAuth Access Token via cURL | |
ACCESS_TOKEN=$(curl --silent POST "$CLOUDWAYS_API_URL/oauth/access_token" --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' --data-urlencode "email=$CLOUDWAYS_API_EMAIL" --data-urlencode "api_key=$CLOUDWAYS_API_KEY" | jq -r '.access_token') | |
# Trigger Git pull | |
GIT_PULL_RESPONSE=$(curl --silent POST "$CLOUDWAYS_API_URL/git/pull" --header 'Content-Type: application/x-www-form-urlencoded' --header "Authorization: Bearer $ACCESS_TOKEN" --data-urlencode "server_id=$SERVER_ID" --data-urlencode "app_id=$APP_ID" --data-urlencode "branch_name=$GIT_BRANCH") | |
GIT_PULL_STATUS=$(jq -r '.status' <<< "$GIT_PULL_RESPONSE") | |
if [[ $GIT_PULL_STATUS != 'true' ]]; then | |
echo "Git pull failed. Response from server was $GIT_PULL_RESPONSE" | |
exit 1 | |
fi | |
echo "Git pull requested. Waiting for operation to complete..." | |
OPERATION_ID=$(jq -r '.operation_id' <<< "$GIT_PULL_RESPONSE") | |
# Wait for the operation to complete | |
ATTEMPT_COUNTER=0 | |
MAX_ATTEMPTS=5 | |
while true | |
do | |
OPERATION_STATUS=$(curl --silent GET "$CLOUDWAYS_API_URL/operation/$OPERATION_ID" --header 'Content-Type: application/x-www-form-urlencoded' --header "Authorization: Bearer $ACCESS_TOKEN") | |
OPERATION_IS_COMPLETE=$(jq -r '.operation.is_completed' <<< "$OPERATION_STATUS") | |
if [ "$OPERATION_IS_COMPLETE" -eq 1 ]; then | |
echo | |
jq -r '.operation.status' <<< "$OPERATION_STATUS" | |
exit 0 | |
fi | |
if [ $ATTEMPT_COUNTER -eq $MAX_ATTEMPTS ]; then | |
echo | |
echo "Operation did not complete after $MAX_ATTEMPTS attempts. Exiting." | |
exit 1 | |
fi | |
printf '.' | |
ATTEMPT_COUNTER=$((ATTEMPT_COUNTER + 1)) | |
sleep 5 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment