-
Notifications
You must be signed in to change notification settings - Fork 11
/
json-to-envvars
executable file
·45 lines (44 loc) · 1.37 KB
/
json-to-envvars
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
# usage: json-to-envvars [[var1 [var2 [var3 […]]]]]
#
# Transforms JSON object from stdin like this:
#
# {
# "ENV1": "ABC",
# "ENV2": "DEF",
# "ENV3": null
# }
#
# into text like this:
#
# ENV1<<__EOF__
# ABC
# __EOF__
# ENV2<<__EOF__
# DEF
# __EOF__
# ENV3<<__EOF__
#
# __EOF__
#
# which is suitable for appending to the $GITHUB_ENV file in order to set
# the environment variables for subsequent steps. Note that null values are
# converted to empty strings so that they don't get intepreted as the value "null".
#
# Only specified variables names will be included in the output.
# If no variable names are provided, all key/value pairs will be included.
#
# See the GitHub docs for more info on this heredoc-like syntax, which is
# used here to avoid quoting issues in arbitrary env var values:
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
#
# Modified from the pathogne-repo-ci workflow:
# https://github.com/nextstrain/.github/blob/cc6f4385a45bd6ed114ab4840416fd90cc46cd1b/.github/workflows/pathogen-repo-ci.yaml#L145-L196
#
set -eou pipefail
jq --raw-output '
to_entries
| if ($ARGS.positional | length) > 0 then map(select(.key|IN($ARGS.positional[]))) else . end
| map("\(.key)<<__EOF__\n\(if .value != null then .value else "" end)\n__EOF__")
| join("\n")
' --args "$@"