How to set a custom maximum job duration

Currently the maximum duration of a job is "hard-coded" at the organization level, and is solely based on the organization's plan.

This article explains how to leverage the step-level background attribute and the CircleCI API v2 "Cancel job" endpoint to customize a job's maximum duration.

 

Prerequisites

You will need to store a personal API token (it can be a personal API token generated for a machine-user) in a project-level or context-level environment variable.

 

Instructions

Add the following as the first step of your job:

- run:
name: Set maximum job duration to 90 minutes
background: true
command: |
sleep 90m
curl --request POST \
--url https://circleci.com/api/v2/project/gh/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/job/$CIRCLE_BUILD_NUM/cancel \
--header "Circle-Token: $CIRCLECI_API_TOKEN"

Note: CIRCLE_PROJECT_USERNAME, CIRCLE_PROJECT_REPONAME, and CIRCLE_BUILD_NUM, are built-in environment variables, and are automatically populated for each job.

Was this article helpful?
0 out of 1 found this helpful

Comments

0 comments

Article is closed for comments.