Skip to content

Conversation

rnro
Copy link
Contributor

@rnro rnro commented Sep 19, 2025

Allow the specification of custom environment variables in our matrix workflow and those which use it.

Motivation:

It can be useful to supply custom environment variables to control build and runtime options.

Modifications:

Allow users to supply a string of custom environment variable key-value pairs which are supplied to the docker container running the matrix commands.

To attempt to avoid issues with this string concatenation the change also allows the user to specify custom list entry and key-value-pair separators.

To avoid parameter bloat the env vars are set for all Swift versions, hopefully that's sufficient for now.

Result:

Adopters can

Examples of this working on Linux and Windows, note the presence of the test environment variables I injected:

e.g.

Executing Docker command: docker run -v /home/runner/work/swift-nio/swift-nio:/swift-nio -w /swift-nio -e CI=true -e GITHUB_ACTIONS=true -e SWIFT_VERSION=6.0 -e workspace=/swift-nio -e TEST_DEBUG=1 -e TEST_LOG_LEVEL=info swift:6.0-jammy bash -c swift --version && swift test -Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error

@rnro rnro force-pushed the matrix_custom_env_vars branch 5 times, most recently from 206c9f8 to 7105a5f Compare September 19, 2025 15:57
@rnro rnro force-pushed the matrix_custom_env_vars branch from 7105a5f to 7a9a174 Compare September 19, 2025 16:35
@rnro rnro marked this pull request as ready for review September 19, 2025 16:52
@rnro rnro requested a review from czechboy0 September 19, 2025 16:52
@rnro rnro added the semver/none No version bump required. label Sep 19, 2025
@rnro rnro removed the request for review from czechboy0 September 22, 2025 08:36
Copy link
Contributor

@simonjbeaumont simonjbeaumont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks like the logic is OK, but can we simplify the solution.

What's the motivation for these custom formats? It seems to introduce quite a bit of complexity if it's not strictly necessary.

linux_env_var_list_separator:
type: string
description: "Separator between environment variable pairs for Linux."
default: ","
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the PR description:

To attempt to avoid issues with this string concatenation the change also allows the user to specify custom list entry and key-value-pair separators.

Do we really think this is worth the added complexity. Why would it not be feasible for this layer (the GitHub YAML) to mandate that env vars are passed as a comma-separated key=value pairs?

Or better yet,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree with this, since this ends up as JSON anyway I've pushed a change which instead takes a JSON map to define the key-value pairs.

- id: generate-matrix
run: |
# Parse environment variables using the parse_env_vars script
env_vars_json=$(curl -s --retry 3 https://raw.githubusercontent.com/apple/swift-nio/main/scripts/parse_env_vars.sh | ENV_VARS="$MATRIX_ENV_VARS" ENV_VAR_PAIR_SEPARATOR="$MATRIX_ENV_VAR_PAIR_SEPARATOR" ENV_VAR_LIST_SEPARATOR="$MATRIX_ENV_VAR_LIST_SEPARATOR" bash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this warrant a script? Potentially if we standardise of key1=value1,key2=value2,... this could be a simple jq command.

Something like this?

echo "a=b,c=d" | jq -R 'split(",") | map(split("=")) | map({key: .[0], value: .[1]}) | from_entries'

persist-credentials: false
submodules: true
- name: Export environment variables
if: ${{ matrix.config.env != '' && matrix.config.env != '{}'}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, isn't the schema defining env as type object? If so, is the first boolean statement in this expression necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relatedly, I think we're really starting to grow beyond the current method. I understand that "inputs" only have restricted types (string, choice, boolean). Clearly it'd be out of scope for this PR, but would moving these to be Actions-proper support having richer inputs? e.g.

uses: our-thing/thing-action@v1
with:
  env:
     - this: is
     - a: proper
     - map: type
     - just: like
     - normal: env

@Lukasa Lukasa merged commit 154706e into main Sep 22, 2025
57 of 60 checks passed
@Lukasa Lukasa deleted the matrix_custom_env_vars branch September 22, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/none No version bump required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants