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 stepapi_docs |
environment | The environment for the currently running stepapi_docs |
change | The change the currently running step belongs toapi_docs |
step | The currently running stepapi_docs |
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
action: deploy
status_code: running
initial_step_tree:
metadata:
custom:
change: metadata
automated: false
approved_by: []
rejected_by: []
created_at: '2020-05-20T10:00:00.000000Z'
started_at: '2020-05-20T10:00:05.000000Z'
finished_at:
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
step_type: standard
child_execution_strategy: sequential
change_id: 3a97c789-8f4e-497c-a1aa-04efaa2e87e7
created_at: '2020-05-20T10:00:00.000000Z'
started_at: '2020-05-20T10:00:05.000000Z'
finished_at:
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
- 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.↩