Skip to content

Commit f0b35f5

Browse files
Merge pull request #21 from im-open/update-readme
Update README.md
2 parents 01b3edd + ee7a5f4 commit f0b35f5

File tree

1 file changed

+60
-63
lines changed

1 file changed

+60
-63
lines changed

README.md

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
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

77
- [Inputs](#inputs)
8-
- [Environment Variables](#environment-variables)
98
- [Outputs](#outputs)
109
- [Usage Examples](#usage-examples)
1110
- [Usage Instructions](#usage-instructions)
12-
- [`key-name`](#key-name)
13-
- [`scope-array`](#scope-array)
14-
- [`key-value`](#key-value)
11+
- [_`key-name`_](#key-name)
12+
- [_`scope-array`_](#scope-array)
13+
- [_`key-value`_](#key-value)
14+
- [Input Environment Variables](#input-environment-variables)
1515
- [Input File Format](#input-file-format)
1616
- [`error-on-no-match`](#error-on-no-match)
17-
- [Repository Secrets](#repository-secrets)
1817
- [Contributing](#contributing)
1918
- [Recompiling](#recompiling)
2019
- [Incrementing the Version](#incrementing-the-version)
@@ -23,21 +22,17 @@ This action takes specially formatted environment variables and/or an input file
2322

2423
## Inputs
2524

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.
25+
| Parameter | Is Required | Description |
26+
| ------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
27+
| `scope` | true | The scope is used to identify which value to select for each key name. See the usage instructions below. |
28+
| `input-file` | false | A specially formatted YAML file containing possible environment variable candidates with their associated scopes. |
29+
| `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. |
30+
| `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. |
31+
| `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 |
3732

3833
## Outputs
3934

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

4237
## Usage Examples
4338

@@ -47,9 +42,16 @@ Varies by usage, but is based on [`key-name`](#key-name) specified in environmen
4742
on:
4843
workflow_dispatch:
4944
inputs:
50-
environment:
51-
description: The environment to deploy to.
45+
destination:
46+
description: The destination to deploy to.
5247
required: true
48+
options:
49+
- dev
50+
- qa
51+
- stage
52+
- stage-secondary
53+
- prod
54+
- prod-secondary
5355

5456
jobs:
5557
setup:
@@ -58,7 +60,7 @@ jobs:
5860
env-scope: ${{ steps.env-scope.output.ENVIRONMENT }}
5961

6062
steps:
61-
- name: Set environment scope
63+
- name: Set environment
6264
id: env-scope
6365
uses: im-open/set-environment-variables-by-scope@v1.1.4
6466
with:
@@ -67,10 +69,10 @@ jobs:
6769
error_on_no_match: true
6870
custom_error_message: 'The environment must be Dev, QA, Stage or Prod'
6971
env:
70-
ENVIRONMENT@dev d development: dev
71-
ENVIRONMENT@qa a: qa
72-
ENVIRONMENT@stage s stg: stage
73-
ENVIRONMENT@prod p production: prod
72+
ENVIRONMENT@dev: dev
73+
ENVIRONMENT@qa: qa
74+
ENVIRONMENT@stage stage-secondary: stage
75+
ENVIRONMENT@prod prod-secondary: prod
7476

7577
- run: echo "The current environment is ${{ env.ENVIRONMENT }}."
7678

@@ -116,23 +118,23 @@ inFileName2@dev qa stage prod demo: 'file value for all environments'
116118
117119
### Usage Instructions
118120
119-
The format of the `env` variable and the `input-file` contents are the same and follows this format:
121+
The format of each input environment variable and each entry in the `input-file` are the same and follows this format:
120122

121123
`[key-name]@[scope-array]: [key-value]`
122124

123-
#### `key-name`
125+
#### _`key-name`_
124126

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.
127+
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.
126128

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.
129+
**_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.
128130

129-
#### `scope-array`
131+
#### _`scope-array`_
130132

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.
133+
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.
132134

133-
#### `key-value`
135+
#### _`key-value`_
134136

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

137139
This set of environment variable or input file entries:
138140

@@ -142,10 +144,30 @@ env:
142144
db_server@stage prod: db-server.domain.com
143145
```
144146

145-
Produces an environment variable, `${{ env.db_server }}`, with a value of `db-server.domain.com`, if the action `scope` is set to `stage`.
147+
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`.
148+
149+
#### Input Environment Variables
150+
151+
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.
152+
153+
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:
154+
155+
```yaml
156+
- name: Build DB Connection
157+
uses: im-open/set-environment-variables-by-scope@v1.1.4
158+
with:
159+
scope: ${{ needs.setup.outputs.env-scope }}
160+
env:
161+
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 }};'
162+
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 }};'
163+
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 }};'
164+
SQL_CONNECTION_STRING@prod: 'Data Source=sql-server.domain.com;Initial Catalog=demo-db;User Id=db-sa-user;Password=${{ secrets.SQL_USER_SECRET }};'
165+
```
166+
167+
** 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.
146168

147169
#### 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:
170+
The `input-file` must be YAML format and has all the elements at the root level. It's contents would be formatted like this:
149171

150172
```yaml
151173
db_server@dev qa: lower-env.db-server.domain.com
@@ -161,37 +183,12 @@ something.used.in.build@stage: stage-value
161183
something.used.in.build@prod: prod-value
162184
```
163185

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

166190
`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.
167191

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-
195192
## Contributing
196193

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

244241
## License
245242

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

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

0 commit comments

Comments
 (0)