If I missed the answer to this in the documentation, I apologize. I find that Doctl can be tedious to use at times.

  1. Can we alias apps by their name instead of their ID, so we can do doctl apps create-deployment <app name>

  2. Is there a more practical way to get the most recent deployment than doctl apps list-deployments, then scroll to the top of that response (because it returns every deployment ever), then copy the id, and then run doctl apps get-deployment <app id> <deployment id>?

Here’s what I’d like:

doctl apps get-deployment <app name> [latest | active | failed] [int for number of results]

eg:

doctl apps get-deployment MyApp failed 5 would return the 5 most recent failed deploys

doctl apps get-deployment MyApp would return the latest deployment in any state

doctl apps get-deployment MyApp active would return either a build in progress, or nothing

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

👋🏼 @planetzalex

Thank you for the feedback, these are great ideas! I agree they would make great improvements to the doctl experience.

Until this is implemented natively, you can chain commands to get what you need. I realize that this is not very convenient for day to day use but more for scripting, but you can also create aliases for these commands using your shell. Hopefully this is still helpful in the mean time and others that might come across this question while using doctl in scripts.

Look up an app’s UUID by its name

doctl apps list --format ID,Spec.Name | grep ' sample-php$' | cut -d' ' -f1

The --format option limits the output columns to just the app UUID and name. Replace sample-php with your app’s name.

The space before the name is not always required, but it can be helpful if you have two apps named php-test-app and test-app. If you omit the space and grep for test-app, both will match. Same for $ which signifies the end of the line. Alternatively, you can add a | head -1 at the end to only return the first app if multiple match.

To combine with create-deployment:

doctl apps create-deployment "$(doctl apps list --format ID,Spec.Name | grep ' sample-php$' | cut -d' ' -f1)"`

Look up the UUID of the most recent deployment

doctl apps list-deployments --format ID --no-header <app uuid> | head -1

Similar to the previous command but with --no-header so that the output starts with just the results. You can replace -1 with any number to increase the number of deployments printed.

Retrieve an app’s active deployment

You can get the active deployment’s UUID using:

doctl apps get --format ActiveDeployment.ID --no-header <app uuid>

You can pass this to doctl apps get-deployment as in the create-deployment example above.

Another option is to have doctl print the results as JSON and parse that using a different tool (such as jq), which allows for much more advanced logic.

doctl apps get --output json <app uuid> | jq -r '.[0].active_deployment.id'

Since the active deployment is nested under the app object, you can also print the whole deployment instead of just the ID:

doctl apps get --output json <app uuid> | jq '.[0].active_deployment'

List failed deployments

This is where the JSON method really helps.

doctl apps list-deployments --output json <app uuid> | jq -r '[.[] | select(.phase == "ERROR")] | sort_by(.created_at)'

To get the UUID of the most recent failed deployment:

doctl apps list-deployments --output json <app uuid> | jq -r '[.[] | select(.phase == "ERROR")] | sort_by(.created_at) | .[0].id'

To get the details of the most recent failed deployment:

doctl apps get-deployment <app uuid> "$(doctl apps list-deployments --output json <app uuid> | jq -r '[.[] | select(.phase == "ERROR")] | sort_by(.created_at) | .[0].id')"

At this point you may also want to further extract the required details using jq instead of extracting the ID and passing it to get-deployment.