Skip to content
Stackship documentation Svenska

JobsUsers

Run a job

Start a job from a container image with the stsh CLI, follow its output, check the result and delete it.

Requires: jobs/write, jobs/readLogs

Running a job needs jobs/write in the resource group, and reading its output jobs/readLogs. The Jobs Operator, Contributor and Owner roles have both.

Before you start

  • The stsh CLI, signed in — see Sign in with the CLI.
  • The boundary and the resource group to run the job in. Pass them with -b and -g, or leave them out to use your context's defaults.
  • The id of a cluster the boundary is projected into. List them with:
bash
stsh boundary projections -b my-boundary

Start the job

bash
stsh job create nightly-import -b my-boundary -g my-resource-group \
  --set image=ghcr.io/example/importer \
  --set tag=1.4.2 \
  --set clusterId=<cluster-id> \
  --set computePlan=small \
  --set 'env={"SOURCE_URL":"https://data.example.com/export.csv"}'

Or put the body in a file and pass --file job.json; --set then overrides single fields:

json
{
  "image": "ghcr.io/example/importer",
  "tag": "1.4.2",
  "clusterId": "<cluster-id>",
  "computePlan": "small",
  "env": { "SOURCE_URL": "https://data.example.com/export.csv" },
  "activeDeadlineSeconds": 1800,
  "ttlSecondsAfterFinished": 86400
}
  • The name becomes the Kubernetes Job's name: lowercase letters, digits and hyphens, starting and ending with a letter or digit, at most 63 characters, and not in use by another job in the resource group.
  • Give the image without a tag and the tag separately; the tag is latest when you leave it out. The platform joins the two with a colon, so an image given with its tag ends up with two tags and cannot be pulled.
  • clusterId is required in practice: without it the job is refused.
  • --set reads a value that parses as JSON as JSON. A tag that looks like a number, such as 2 or 1.4, must be quoted — --set tag='"1.4"' — or the create is refused.

The command prints Done. a few seconds after the platform has accepted the job; it does not wait for the job to finish. When the platform refuses the job — no cluster, a cluster the boundary is not projected into, a name already in use — the only answer is failed to create job; check those first.

Follow its output

bash
stsh job logs nightly-import -g my-resource-group

The command streams what the container writes until you stop it with Ctrl+C. When the container has exited, the stream ends and the CLI connects again, so it prints the job's whole output again every few seconds. Before the job's pod exists, and after it is removed, it answers No pods found for job., repeated the same way.

Check the result

bash
stsh job get nightly-import -g my-resource-group
stsh job list -g my-resource-group

status is Pending, Running, Succeeded or Failed; a job that succeeded also has completedAt. The schedule and lastRunUtc columns of stsh job list stay empty; jobs have neither. The statuses are described in Job settings.

Run it again

A job runs once. To run it again, delete it and create it again with the same name, or create a job with a new name. A finished job keeps its name taken until it is deleted or removed after its time to live.

Delete a job

bash
stsh job delete nightly-import -g my-resource-group

The CLI asks you to confirm; -y skips the question. The job is removed at once; Kubernetes then stops its pod, if it is still running, and removes the pod and its output in the background. The CLI reports Deleted also for a name that has no job.