|
| 1 | +name: Convert legacy environment variables |
| 2 | +description: | |
| 3 | + Converts the legacy KEY=value format, where any line that doesn't start a new KEY= |
| 4 | + continues the previous value (e.g. SSH keys), to $GITHUB_ENV format for export-env-vars. |
| 5 | + KEY<<DELIM blocks in the input are passed through, so anything $GITHUB_ENV accepts is |
| 6 | + also accepted. With mask set to 'true', every line of the output is masked. |
| 7 | + Warns if continuation lines are used. |
| 8 | +
|
| 9 | +inputs: |
| 10 | + value: |
| 11 | + description: Newline-separated KEY=value pairs in the legacy format, or $GITHUB_ENV format |
| 12 | + required: false |
| 13 | + default: '' |
| 14 | + mask: |
| 15 | + description: "'true' to mask every line of the output, 'false' if the input contains no secrets" |
| 16 | + required: true |
| 17 | + |
| 18 | +outputs: |
| 19 | + value: |
| 20 | + description: The input in $GITHUB_ENV format, every variable written as a KEY<<DELIM block |
| 21 | + value: ${{ steps.convert.outputs.value }} |
| 22 | + |
| 23 | +runs: |
| 24 | + using: composite |
| 25 | + steps: |
| 26 | + - name: Convert to GITHUB_ENV format |
| 27 | + id: convert |
| 28 | + shell: bash |
| 29 | + env: |
| 30 | + INPUT_VALUE: ${{ inputs.value }} |
| 31 | + INPUT_MASK: ${{ inputs.mask }} |
| 32 | + run: | |
| 33 | + # The runner doesn't enforce required inputs, and a missing mask would otherwise |
| 34 | + # mean no masking, so fail unless it is set explicitly. |
| 35 | + case "$INPUT_MASK" in |
| 36 | + true | false) ;; |
| 37 | + *) |
| 38 | + echo "::error title=convert-legacy-env-vars::The mask input must be 'true' or 'false', got '$INPUT_MASK'" |
| 39 | + exit 1 |
| 40 | + ;; |
| 41 | + esac |
| 42 | +
|
| 43 | + # Every variable is written as a KEY<<DELIM block. CRs are left in place for |
| 44 | + # export-env-vars, which strips them and warns about them. |
| 45 | + # awk prints its warning to stderr: only its stdout is captured, and the runner |
| 46 | + # processes workflow commands on stderr as well as stdout. |
| 47 | + converted=$(awk -v delim="EOF_$(cat /proc/sys/kernel/random/uuid)" ' |
| 48 | + # Write the current variable as a block. Trailing blank lines are dropped, as |
| 49 | + # node-pr always did; blank lines inside a value are kept. The delimiter is |
| 50 | + # random so no value can end the block early. |
| 51 | + function flush() { |
| 52 | + if (key == "") return |
| 53 | + sub(/(\n[[:space:]]*)+$/, "", value) |
| 54 | + print key "<<" delim |
| 55 | + print value |
| 56 | + print delim |
| 57 | + key = "" |
| 58 | + value = "" |
| 59 | + } |
| 60 | +
|
| 61 | + # Inside a KEY<<DELIM block from the input: pass it through unchanged. |
| 62 | + block != "" { |
| 63 | + print |
| 64 | + if ($0 == block) block = "" |
| 65 | + next |
| 66 | + } |
| 67 | +
|
| 68 | + # KEY<<DELIM header (<< before any =, the same rule as the runner): |
| 69 | + # finish the current variable, then pass the block through. |
| 70 | + match($0, /^[^=]*<</) && index(substr($0, 1, RLENGTH), "=") == 0 { |
| 71 | + flush() |
| 72 | + print |
| 73 | + block = substr($0, RLENGTH + 1) |
| 74 | + next |
| 75 | + } |
| 76 | +
|
| 77 | + # KEY=value starts a new variable. |
| 78 | + /^[A-Za-z_][A-Za-z0-9_]*=/ { |
| 79 | + flush() |
| 80 | + i = index($0, "=") |
| 81 | + key = substr($0, 1, i - 1) |
| 82 | + value = substr($0, i + 1) |
| 83 | + next |
| 84 | + } |
| 85 | +
|
| 86 | + # Any other line continues the current variable. A non-blank continuation line |
| 87 | + # means the legacy multiline format is in use (blank lines are just trailing |
| 88 | + # newlines, which are common and harmless). |
| 89 | + key != "" { |
| 90 | + value = value "\n" $0 |
| 91 | + if ($0 ~ /[^[:space:]]/) legacy = 1 |
| 92 | + next |
| 93 | + } |
| 94 | +
|
| 95 | + # Before the first variable: pass through and let the runner validate it |
| 96 | + # (blank lines are skipped, anything else is a format error). |
| 97 | + { print } |
| 98 | +
|
| 99 | + END { |
| 100 | + flush() |
| 101 | + if (legacy) { |
| 102 | + print "::warning title=convert-legacy-env-vars::Input uses continuation lines for multiline values,", |
| 103 | + "a legacy format that can misread base64 lines as new variables.", |
| 104 | + "Use a KEY<<DELIMITER block for multiline values instead." > "/dev/stderr" |
| 105 | + } |
| 106 | + } |
| 107 | + ' <<< "$INPUT_VALUE") |
| 108 | +
|
| 109 | + # The converted text is passed on through `with:` (e.g. to export-env-vars), where it |
| 110 | + # is shown in the log and no longer matches the original secret, so GitHub would not |
| 111 | + # mask it. Mask the whole output with a single add-mask: the runner decodes %25, %0D |
| 112 | + # and %0A in the value, then masks the whole value and each of its lines (ignoring |
| 113 | + # empty ones). sed -z reads the whole output as one record so newlines can be escaped. |
| 114 | + # Skipped if there is nothing to mask, as the runner warns about empty add-mask values. |
| 115 | + if [ "$INPUT_MASK" = "true" ] && [[ "$converted" =~ [^[:space:]] ]]; then |
| 116 | + echo "::add-mask::$(sed -z 's/%/%25/g; s/\r/%0D/g; s/\n/%0A/g' <<< "$converted")" |
| 117 | + fi |
| 118 | +
|
| 119 | + out_delim="EOF_$(cat /proc/sys/kernel/random/uuid)" |
| 120 | + { |
| 121 | + echo "value<<$out_delim" |
| 122 | + printf '%s\n' "$converted" |
| 123 | + echo "$out_delim" |
| 124 | + } >> "$GITHUB_OUTPUT" |
0 commit comments