Persistent Context is helpfull when you need to save information from the job execution, for example it can be used to save the infracost or save the thread id when using the Slack extension. We can also use it to save any JSON information generated inside Terrakube Jobs.
Using Persistent Contexts with the Terrakube API
In order to save the information the terrakube API exposes the following endpoint
POST {{terrakubeApi}}/context/v1/{{jobId}}
{
"slackThreadId": "12345667",
"infracost": {}
}
Job context can only be updated when the status of the Job is running.
To get the context you can use the Terrakube API
GET {{terrakubeApi}}/context/v1/{{jobId}}
Using Persistent Contexts in Templates
The persistent context can be used using the Context extension from the Terrakube extension repository. It supports saving a JSON file or saving a new property inside the context JSON.
import Context
new Context("$terrakubeApi", "$terrakubeToken", "$jobId", "$workingDirectory").saveFile("infracost", "infracost.json")
new Context("$terrakubeApi", "$terrakubeToken", "$jobId", "$workingDirectory").saveProperty("slackId", "1234567890")
This is an example of a Terrakube template using the persistent job context to save the infracost information.