Context
The OpsChain context framework provides a read only set of values. These values enable you to reuse code between projects and environments, conditionally performing logic based on when and where the step is being performed.
After reading this guide you should understand:
- the information available in the OpsChain context
- how to access the OpsChain context values in your actions
OpsChain context
Within each action, OpsChain context values are available via OpsChain.context
(which will behave like a Hashie Mash). The OpsChain.context
includes the following information:
Context key | Description |
---|---|
project | The project for the currently running step1 |
environment | The environment for the currently running step1 |
change | The change the currently running step belongs to1 |
step | The currently running step1 |
user | Information about the user who submitted the changename - the user who submitted the changegroups - an array of LDAP groups that the user is a member of |
Accessing the context information
Context information can be accessed using dot or square bracket notation with string or symbol keys. These examples are equivalent:
require 'opschain'
OpsChain.context.change.action
OpsChain.context[:change][:action]
OpsChain.context['change']['action']
The OpsChain.context
structure is read only.
Example usage
In the example below, running the main
action in the development environment will set the OpsChain logger to the DEBUG level. When running in any other environment, the OpsChain logger will remain in the default (INFO) level.
require 'opschain'
action :enable_logging do
OpsChain.logger.level = ::Logger::DEBUG if OpsChain.context.parents.environment.code == 'dev'
end
action main: ['enable_logging'] do
.... main process
end
Sample context values
Below is an example of the values available to an action via OpsChain.context
(formatted as YAML):
---
change:
id: 3a97c789-8f4e-497c-a1aa-04efaa2e87e7
created_by: mary
name: deploy
action: deploy
initial_step_tree:
metadata:
custom:
change: metadata
property_overrides:
pixel: overriding
scheduled: false
approved_by: []
rejected_by: []
status_code: running
created_at: '2020-05-20T10:00:00.000000Z'
started_at: '2020-05-20T10:00:05.000000Z'
finished_at:
updated_at: '2025-04-25T06:22:28.803399Z'
asset_name:
environment_name: Receivables
project_name: Finance
git_remote_name: origin
git_rev: bug-fix
commit_sha: 5213c76dad01ac0d87c2c900d46778675d4dc760
requires_approval_from:
step:
id: 37fdf12f-aff3-4134-8926-a2321cef5acf
approved_by: []
rejected_by: []
continued_by: []
requires_approval_from:
action: deploy
name: deploy
step_type: standard
child_execution_strategy: sequential
change_id: 3a97c789-8f4e-497c-a1aa-04efaa2e87e7
skip_on_retry: false
created_at: '2020-05-20T10:00:00.000000Z'
started_at: '2020-05-20T10:00:05.000000Z'
finished_at:
updated_at: '2025-04-25T06:22:28.803399Z'
status_code: running
user:
name: mary
groups:
- manager
- purchasing
parents:
project:
id: bb7bef85-805f-43d9-a267-7e901630ffa0
code: fin
name: Finance
description: Finance applications
archived: false
project_type: Standard
environment:
id: 530d796b-60ff-4bc6-ad09-ac3eaf1afa45
code: rcv
name: Receivables
description: RMS
archived: false
Footnotes
-
The attributes available within these context keys are the same as those available to you from the relevant API endpoint. See the OpsChain API documentation for more details. ↩ ↩2 ↩3 ↩4