Skip to content

Commit ef57338

Browse files
authored
Update README.md
1 parent 01b3edd commit ef57338

File tree

1 file changed

+56
-58
lines changed

1 file changed

+56
-58
lines changed

README.md

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Set Environment Variables by Scope
22

3-
This action takes specially formatted environment variables and/or an input file to emit scoped environment variables.
3+
This action sets environment variables, (and optionally step outputs), to different values determined by a provided `scope`. The values for each variable and scope can be provided in specially-named environment variables or an input file.
44

55
## Index <!-- omit in toc -->
66

@@ -23,21 +23,17 @@ This action takes specially formatted environment variables and/or an input file
2323

2424
## Inputs
2525

26-
| Parameter | Is Required | Description |
27-
| ------------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
28-
| `scope` | true | The scope is used to select key names and values to be set as environment vars, key names that do not contain the scope will not be exported |
29-
| `input-file` | false | A specially formatted YAML file containing possible environment variable candidates with their associated scopes |
30-
| `create-output-variables` | false | Create output variables (in addiction to environment variables) for use in other steps and jobs, accepts true or false, defaults to false |
31-
| `error-on-no-match` | false | An error will be thrown if no env or output variables are created, a warning will appear for all keys that don't provide a value for the input scope |
32-
| `custom-error-message` | false | The error message that will be displayed if no environment or output variables are created, `error_on_no_match` must be set to true |
33-
34-
### Environment Variables
35-
36-
This action relies heavily on environment variables as part of its inputs. _Any_ environment variable with an [`@` in it's key name](#key-name) is used as a possible candidate for scoped output. More information is included in the [usage instructions](#usage-instructions) section.
26+
| Parameter | Is Required | Description |
27+
| ------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
28+
| `scope` | true | The scope is used to identify which value to select for each key name. See the usage instructions below. |
29+
| `input-file` | false | A specially formatted YAML file containing possible environment variable candidates with their associated scopes. |
30+
| `create-output-variables` | false | Create output variables (in addiction to environment variables) for use in other steps and jobs. Accepts true or false. Defaults to false. |
31+
| `error-on-no-match` | false | An error will be thrown if no environment variables or outputs are created, a warning will appear for all keys that don't provide a value for the input scope. |
32+
| `custom-error-message` | false | The error message that will be displayed if no environment or output variables are created. `error_on_no_match` must be set to true |
3733

3834
## Outputs
3935

40-
Varies by usage, but is based on [`key-name`](#key-name) specified in environment variables and the `input-file` specified.
36+
Varies by usage, but is based on the [`key-name`](#key-name)s supplied in environment variables and the `input-file`.
4137

4238
## Usage Examples
4339

@@ -47,9 +43,16 @@ Varies by usage, but is based on [`key-name`](#key-name) specified in environmen
4743
on:
4844
workflow_dispatch:
4945
inputs:
50-
environment:
51-
description: The environment to deploy to.
46+
destination:
47+
description: The destination to deploy to.
5248
required: true
49+
options:
50+
- dev
51+
- qa
52+
- stage
53+
- stage-secondary
54+
- prod
55+
- prod-secondary
5356

5457
jobs:
5558
setup:
@@ -58,7 +61,7 @@ jobs:
5861
env-scope: ${{ steps.env-scope.output.ENVIRONMENT }}
5962

6063
steps:
61-
- name: Set environment scope
64+
- name: Set environment
6265
id: env-scope
6366
uses: im-open/set-environment-variables-by-scope@v1.1.4
6467
with:
@@ -67,10 +70,10 @@ jobs:
6770
error_on_no_match: true
6871
custom_error_message: 'The environment must be Dev, QA, Stage or Prod'
6972
env:
70-
ENVIRONMENT@dev d development: dev
71-
ENVIRONMENT@qa a: qa
72-
ENVIRONMENT@stage s stg: stage
73-
ENVIRONMENT@prod p production: prod
73+
ENVIRONMENT@dev: dev
74+
ENVIRONMENT@qa: qa
75+
ENVIRONMENT@stage stage-secondary: stage
76+
ENVIRONMENT@prod prod-secondary: prod
7477

7578
- run: echo "The current environment is ${{ env.ENVIRONMENT }}."
7679

@@ -116,23 +119,23 @@ inFileName2@dev qa stage prod demo: 'file value for all environments'
116119
117120
### Usage Instructions
118121
119-
The format of the `env` variable and the `input-file` contents are the same and follows this format:
122+
The format of each input environment variable and each entry in the `input-file` are the same and follows this format:
120123

121124
`[key-name]@[scope-array]: [key-value]`
122125

123-
#### `key-name`
126+
#### _`key-name`_
124127

125-
The resulting environment variable name. It does accept a wide variety of name convention formats including spaces, dashes, periods, and underscores. While it's been tested and found to be fairly flexible, it is possible to make environment variable names that aren't usable by one or more of the target action runner operating systems.
128+
The resulting environment variable and output name. It can contain practically any 'printable' characters including spaces, dashes, colons, periods, and underscores. While it's been tested and found to be fairly flexible, it is possible to make environment variable names that aren't usable by one or more of the target action runner operating systems.
126129

127-
**_Key names intended for use as output variables that are referenced in the workflow can only contain letters, numbers, dashes and underscores._** Any other punctuation or space characters cannot be processed and will cause errors when attempting to reference them later in a workflow.
130+
**_Key names intended for use as output variables can only contain letters, numbers, dashes and underscores._** Any other punctuation or space characters cannot be processed and will cause errors when attempting to reference them later in a workflow.
128131

129-
#### `scope-array`
132+
#### _`scope-array`_
130133

131-
A space delimited array of scope strings that the environment variable could be valid for. The filter by which keys/values will be selected is the `scope` action input. The scope value comparison is _not_ case sensitive.
134+
A space delimited array of scope strings. When the value of the `scope` input matches one of these strings, the _`key-name`_ environment variable and output will be set to _`key-value`_. The scope value comparison is _not_ case sensitive.
132135

133-
#### `key-value`
136+
#### _`key-value`_
134137

135-
The value that the environment variable will be set to if it's found to meet the scope criteria.
138+
The value that the environment variable and output will be set to if the scope is found in the _`scope-array`_.
136139

137140
This set of environment variable or input file entries:
138141

@@ -142,10 +145,30 @@ env:
142145
db_server@stage prod: db-server.domain.com
143146
```
144147

145-
Produces an environment variable, `${{ env.db_server }}`, with a value of `db-server.domain.com`, if the action `scope` is set to `stage`.
148+
Produces an environment variable, `${{ env.db_server }}`, with a value of `db-server.domain.com`, if the action's `scope` is set to `stage` or `prod`.
149+
150+
#### Input Environment Variables
151+
152+
When using environment variables for input, this action looks at all current environment variables. They could have been exported by a previous step, or set for the job or workflow. _Any_ environment variable with an `@` in it's name is used as a possible candidate for scoped output.
153+
154+
GitHub actions expressions can be used in the _`key-value`_ when supplying input environment variables in the step's `env` block as in the following example:
155+
156+
```yaml
157+
- name: Build DB Connection
158+
uses: im-open/set-environment-variables-by-scope@v1.1.4
159+
with:
160+
scope: ${{ needs.setup.outputs.env-scope }}
161+
env:
162+
SQL_CONNECTION_STRING@dev: 'Data Source=dev.sql-server.domain.com;Initial Catalog=dev-demo-db;User Id=dev-db-sa-user;Password=${{ secrets.SQL_USER_SECRET }};'
163+
SQL_CONNECTION_STRING@qa: 'Data Source=qa.sql-server.domain.com;Initial Catalog=qa-demo-db;User Id=qa-db-sa-user;Password=${{ secrets.SQL_USER_SECRET }};'
164+
SQL_CONNECTION_STRING@stage: 'Data Source=stage.sql-server.domain.com;Initial Catalog=stage-demo-db;User Id=stage-db-sa-user;Password=${{ secrets.SQL_USER_SECRET }};'
165+
SQL_CONNECTION_STRING@prod: 'Data Source=sql-server.domain.com;Initial Catalog=demo-db;User Id=db-sa-user;Password=${{ secrets.SQL_USER_SECRET }};'
166+
```
167+
168+
** Note the resulting environment variables this step will set aren't yet available to expressions in the step's own `env` block.** The resulting environment variables are not available until the next step in the job. If you need scoped variable values to be based on the values of other scoped variables you can use this action in consecutive steps.
146169

147170
#### Input File Format
148-
The contents of an `input-file` is YAML based and has all the elements at the root level. It's contents would be formatted like this:
171+
The `input-file` must be YAML format and has all the elements at the root level. It's contents would be formatted like this:
149172

150173
```yaml
151174
db_server@dev qa: lower-env.db-server.domain.com
@@ -161,37 +184,12 @@ something.used.in.build@stage: stage-value
161184
something.used.in.build@prod: prod-value
162185
```
163186

187+
**Note that _`key-value`_\s in input files do _not_ support GitHub action expressions, unlike envrionment variables described previously.**
188+
164189
#### `error-on-no-match`
165190

166191
`error-on-no-match` is intended to alert that no env or output variable has been found based on the input scope. This is beneficial in troubleshooting if a scope *should* produce some form of output. Also a warning will show for any keys that have been included but doesn't provide a value for the input scope criteria.
167192

168-
#### Repository Secrets
169-
170-
Repository secrets can be used with the environment variable creation action, but only through environment vars. This is because the input file does not receive any secret replacement parsing during the action execution.
171-
172-
An example of this would be the inclusion of a secret and user id in a SQL connection string:
173-
174-
```yaml
175-
176-
jobs:
177-
deploy-db:
178-
runs-on: [ubuntu-20.04]
179-
environment: ${{ needs.setup.outputs.env-scope }}
180-
181-
steps:
182-
- name: Build DB Connection
183-
uses: im-open/set-environment-variables-by-scope@v1.1.4
184-
with:
185-
scope: ${{ needs.setup.outputs.env-scope }}
186-
env:
187-
SQL_CONNECTION_STRING@dev: 'Data Source=dev.sql-server.domain.com;Initial Catalog=dev-demo-db;User Id=dev-db-sa-user;Password=${{ env.SQL_USER_SECRET }};'
188-
SQL_CONNECTION_STRING@qa: 'Data Source=qa.sql-server.domain.com;Initial Catalog=qa-demo-db;User Id=qa-db-sa-user;Password=${{ env.SQL_USER_SECRET }};'
189-
SQL_CONNECTION_STRING@stage: 'Data Source=stage.sql-server.domain.com;Initial Catalog=stage-demo-db;User Id=stage-db-sa-user;Password=${{ env.SQL_USER_SECRET }};'
190-
SQL_CONNECTION_STRING@prod: 'Data Source=sql-server.domain.com;Initial Catalog=demo-db;User Id=db-sa-user;Password=${{ env.SQL_USER_SECRET }};'
191-
```
192-
193-
If the scope of `QA` were specified, an environment variable `SQL_CONNECTION_STRING` with the specified value containing a replacement for the `SQL_USER_SECRET` from GitHub Repository's `QA` secret environment.
194-
195193
## Contributing
196194

197195
When creating new PRs please ensure:
@@ -243,6 +241,6 @@ This project has adopted the [im-open's Code of Conduct](https://github.com/im-o
243241

244242
## License
245243

246-
Copyright &copy; 2021, Extend Health, LLC. Code released under the [MIT license](LICENSE).
244+
Copyright &copy; 2023, Extend Health, LLC. Code released under the [MIT license](LICENSE).
247245

248246
[git-version-lite]: https://github.com/im-open/git-version-lite

0 commit comments

Comments
 (0)