Gitlab CI
Using Gitlab CI with Qovery is super powerful and gives you the ability to manage the way that you want to deploy your applications. As the possibility are endless, I will share with you a couple of scenarios and pre-built scripts that you can use. Feel free to adapt them to your need.
qovery:image: gitlab/dindservices:- docker:dindvariables:QOVERY_ORGANIZATION_ID: "xxx"QOVERY_ENVIRONMENT_ID: "xxx"QOVERY_APPLICATION_ID: "xxx"script:- docker run qoveryrd/qovery-action $QOVERY_ORGANIZATION_ID $QOVERY_ENVIRONMENT_ID $QOVERY_APPLICATION_ID $QOVERY_API_TOKEN $CI_COMMIT_SHA
Scenarios
Those scenarios can be copy/paste and adapted to your usage. Do not forget to generate a Qovery API token to use them.
Deploy your application with a specific commit ID
show code
#!/usr/bin/env shAPP_COMMIT_ID="TO CHANGE WITH YOUR APP COMMIT ID TO DEPLOY"ENVIRONMENT_ID="TO CHANGE WITH YOUR ENVIRONMENT ID"APPLICATION_ID="TO CHANGE WITH YOUR APPLICATION ID"QOVERY_API_TOKEN="TO CHANGE"set -e# if the command did not succeed, then the job will just failed# Doc: https://api-doc.qovery.com/#operation/deployApplicationresult=$(curl -sb -X POST -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \-d "{\"git_commit_id\": \"$APP_COMMIT_ID\"}" "https://api.qovery.com/application/$APPLICATION_ID/deploy")echo $resulttimeoutInSeconds=3600endTime=$(($(date +%s) + timeoutInSeconds))## wait for successful deploymentwhile [ "$(date +%s)" -lt $endTime ]; do# check deployment status# Doc: https://api-doc.qovery.com/#operation/getProjectEnvironmentStatuscurrent_state=$(curl -sb -X GET -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \"https://api.qovery.com/environment/$ENVIRONMENT_ID/status" | jq -r .state)if [ "RUNNING" = "$current_state" ]; thenbreakfi# shellcheck disable=SC2039if [[ "$current_state" =~ ^"ERROR_.*" ]]; thenecho "deployment error with current state: $(current_state) - connect to https://console.qovery.com" > /dev/stderrexit 1fiprintf "environment state: $current_state\n"sleep 5 # wait to check againdone## keep goingexit 0
Create a Preview Environment for your Pull-Request
show code
#!/usr/bin/env shPROJECT_ID="TO CHANGE"BASE_ENVIRONMENT="name of environment TO CHANGE"QOVERY_API_TOKEN="TO CHANGE"set -ebaseEnvironmentId=$(curl -sb -X GET -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \"https://api.qovery.com/project/$PROJECT_ID/environment" | jq -r ".results[] | select(.name==\"$BASE_ENVIRONMENT\") | .id")# clone the environment base on the correct right branchnewEnvironmentId=$(curl -sb -X POST -d "{\"name\": \"[PR] $BRANCH_NAME\"}" -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \"https://api.qovery.com/environment/$baseEnvironmentId/clone" | jq -r ".id")# get all apps from envapps=$(curl -sb -X GET -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \"https://api.qovery.com/environment/$newEnvironmentId/application" | jq -r ".")echo "$apps" | jq -c '.results[]' | while read row; do# get complete app JSON and clear necessary fields, otherwise the Qovery API returns 4xxapp=$(echo "$row" | jq -r ".git_repository.branch=\"$BRANCH_NAME\"" | jq -r "del(.environment)" | jq -r "del(.created_at)" \| jq -r "del(.id)" | jq -r "del(.updated_at)" | jq -r "del(.git_repository.url)" \| jq -r "del(.git_repository.deployed_commit_id)" | jq -r "del(.git_repository.deployed_commit_date)" \| jq -r "del(.git_repository.deployed_commit_contributor)" | jq -r "del(.git_repository.deployed_commit_tag)" \| jq -r "del(.git_repository.provider)" | jq -r "del(.git_repository.owner)" | jq -r "del(.git_repository.has_access)" \| jq -r "del(.git_repository.name)" | jq -r "del(.maximum_cpu)" | jq -r "del(.maximum_memory)" | jq -r "del(.ports[].name)" \| jq -r "del(.storage)" | jq -r "del(.git_repository.root_path)")appId=$(echo "$app" | jq -r '.id')# escape double quotesformatApp=$(echo "$app" | jq -c '.' | sed 's/"/\\"/g')# update app via Qovery APIsavedApp=$(curl -sb -X PUT -d "$formatApp" -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \"https://api.qovery.com/application/$appId" | jq -r '.')done# deploy envresult=$(curl -sb -X POST -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \"https://api.qovery.com/environment/$newEnvironmentId/deploy")timeoutInSeconds=3600endTime=$(($(date +%s) + timeoutInSeconds))## wait for successful deploymentwhile [ "$(date +%s)" -lt $endTime ]; do# check deployment status# Doc: https://api-doc.qovery.com/#operation/getProjectEnvironmentStatuscurrent_state=$(curl -sb -X GET -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \"https://api.qovery.com/environment/$newEnvironmentId/status" | jq -r .state)if [ "RUNNING" = "$current_state" ]; thenbreakfi# shellcheck disable=SC2039if [[ "$current_state" =~ ^".*_ERROR" ]]; thenecho "deployment error with current state: $(current_state) - connect to https://console.qovery.com" > /dev/stderrexit 1fiprintf "environment state: $current_state\n"sleep 5 # wait to check againdone## keep goingexit 0
Terraform
Do you want to include Terraform in your CI? Check out our Terraform documentation.
Any other scenario?
Feel free to share your custom scripts with us, and we'll be happy to share them with the community. Contact us on our forum.