From 936de614552345218bbf5a10a76415a6549a790c Mon Sep 17 00:00:00 2001 From: Jover Date: Tue, 23 May 2023 16:24:54 -0700 Subject: [PATCH] Add bin/json-to-envvars and bin/yaml-to-envvars MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modifies the "Set environment variables" step from the pathogen-repo-ci workflow¹ into two separate scripts since we will use the same method to set environment variables to other reusable workflows. I decided to create `json-to-envvars` as a separate script that `yaml-to-envvars` calls because I will be using it to set secrets as environment variables as well. The `--varnames` option will allow us to be explicit about which secrets we want to set as environment variables. ¹ https://github.com/nextstrain/.github/blob/cc6f4385a45bd6ed114ab4840416fd90cc46cd1b/.github/workflows/pathogen-repo-ci.yaml#L183-L198 --- README.md | 2 ++ bin/json-to-envvars | 55 +++++++++++++++++++++++++++++++++++++++++++++ bin/yaml-to-envvars | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100755 bin/json-to-envvars create mode 100755 bin/yaml-to-envvars diff --git a/README.md b/README.md index 9a43ebc..cdbfd0a 100644 --- a/README.md +++ b/README.md @@ -96,3 +96,5 @@ See also GitHub's [documentation on starter workflows](https://docs.github.com/e Executable scripts that are used in our workflows. - [write-envdir](bin/write-envdir) +- [json-to-envvars](bin/json-to-envvars) +- [yaml-to-envvars](bin/yaml-to-envvars) diff --git a/bin/json-to-envvars b/bin/json-to-envvars new file mode 100755 index 0000000..5fbb10b --- /dev/null +++ b/bin/json-to-envvars @@ -0,0 +1,55 @@ +#!/bin/bash +# usage: json-to-envvars [--varnames=] +# +# Transforms JSON object from stdin like this: +# +# { +# "ENV1": "ABC", +# "ENV2": "DEF" +# } +# +# into text like this: +# +# ENV1<<__EOF__ +# ABC +# __EOF__ +# ENV2<<__EOF__ +# DEF +# __EOF__ +# +# which is suitable for appending to the $GITHUB_ENV file in order to set +# the environment variables for subsequent steps. +# +# Only specified will be included in the output. +# If no are provided, all key/value pairs will be included. +# should be a string of variable names separated a space, e.g. "ENV1 ENV2" +# +# 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 + +varnames="" + +for arg; do + case "$arg" in + --varnames=*) + varnames="${arg#*=}" + shift;; + *) + break;; + esac +done + +varnames=$(jq --raw-input 'split(" ")' <<< "$varnames") + +jq --raw-output --argjson varnames "$varnames" ' + to_entries + | if ($varnames | length) > 0 then map(select(.key|IN($varnames[]))) else . end + | map("\(.key)<<__EOF__\n\(.value)\n__EOF__") + | join("\n") +' diff --git a/bin/yaml-to-envvars b/bin/yaml-to-envvars new file mode 100755 index 0000000..030fdb0 --- /dev/null +++ b/bin/yaml-to-envvars @@ -0,0 +1,40 @@ +#!/bin/bash +# +# Transforms YAML from stdin like this: +# +# FOO: bar +# I_CANT_BELIEVE: "it's not YAML" +# would_you_believe: | +# it's +# not +# yaml +# +# first into the equivalent JSON (with yq) and then into text (with jq) +# like this: +# +# FOO<<__EOF__ +# bar +# __EOF__ +# I_CANT_BELIEVE<<__EOF__ +# it's not YAML +# __EOF__ +# would_you_believe<<__EOF__ +# it's +# not +# yaml +# __EOF__ +# +# which is suitable for appending to the $GITHUB_ENV file in order to set +# the environment variables for subsequent steps. +# +# 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 +bin="$(dirname "$0")" + +yq --output-format json . | "$bin/json-to-envvars"