Skip to content

Commit 53fd9ea

Browse files
authored
Merge pull request #12 from philips-software/feature/add-github-default-environment-variables
feat: add default GitHub environment variables
2 parents 9bb0af1 + bb0deb6 commit 53fd9ea

6 files changed

Lines changed: 94 additions & 8 deletions

File tree

.github/workflows/test.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ jobs:
5353
run: >-
5454
echo "Hello world";
5555
exit %ERRORLEVEL%;
56+
5657
- name: Test_Environment
5758
uses: ./
5859
with:
@@ -63,6 +64,15 @@ jobs:
6364
run: >-
6465
c:\map\test\run_test.ps1 -testPath "c:\map\test\environment.Tests.ps1";
6566
67+
- name: Test_Default_Environment
68+
uses: ./
69+
with:
70+
image: ${{ matrix.docker_container }}
71+
entrypoint: powershell.exe
72+
mapping_path: 'c:\map'
73+
run: >-
74+
c:\map\test\run_test.ps1 -testPath "c:\map\test\default_environment.Tests.ps1";
75+
6676
- name: Test_WorkPath
6777
uses: ./
6878
with:
@@ -100,7 +110,7 @@ jobs:
100110
validate:
101111
runs-on: ubuntu-latest
102112
name: Validate
103-
container: mcr.microsoft.com/powershell:latest
113+
container: mcr.microsoft.com/powershell:7.5-ubuntu-24.04
104114

105115
steps:
106116
- name: Checkout
@@ -114,6 +124,7 @@ jobs:
114124
Install-Module -Name Pester -Force -SkipPublisherCheck;
115125
Update-Module -Name Pester -Force;
116126
Install-Module -Name PSScriptAnalyzer -Force;
127+
Import-Module PSScriptAnalyzer -ErrorAction Stop
117128
- name: Linter src
118129
shell: pwsh
119130
run: >-

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# Action for running commands in a windows docker
2+
# Action for running commands in a windows docker container
33

44
## State
55

@@ -29,8 +29,14 @@ Very simple hello world example:
2929
./run-test-script.ps1;
3030
```
3131

32-
On default, the github workspace directory is mapped to the docker volume path
33-
and work path.
32+
## Default environment
33+
34+
This action tries to stay close to GitHub's Action environment, and
35+
as such all GitHub defined [default environment variables](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables) are passed
36+
to the container by default.
37+
38+
By default, the github workspace directory is mapped to the docker volume path
39+
and work path.
3440

3541
<!-- action-docs-inputs -->
3642
## Inputs

action.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ runs:
6565
- name: check
6666
if: runner.os != 'Windows'
6767
run: |
68-
echo "::error file=run-windows-docker-container-action/action.yml,line=52,col=1,endColumn=1::This action can only run on windows";
68+
echo "::error file=run-windows-docker-container-action/action.yml,line=68,col=1,endColumn=1::This action can only run on windows";
6969
exit -1
7070
shell: bash
7171
- name: settings
7272
id: settings
7373
run: >-
74-
${{github.action_path}}/src/parse_input_paths.ps1 -githubWorkSpace "${{ github.workspace }}" -workspacePath "@${{ inputs.workspace_path }}" -mappingPath "@${{ inputs.mapping_path }}" -workPath "@${{ inputs.work_path }}";
75-
${{github.action_path}}/src/parse_input_extra_args.ps1 -envNames "@${{ inputs.env_names }}" -entryPoint "@${{ inputs.entrypoint }}" -extraArgs "@${{ inputs.extra_args }}";
74+
${{ github.action_path }}/src/parse_input_paths.ps1 -githubWorkSpace "${{ github.workspace }}" -workspacePath "@${{ inputs.workspace_path }}" -mappingPath "@${{ inputs.mapping_path }}" -workPath "@${{ inputs.work_path }}";
75+
${{ github.action_path }}/src/parse_input_extra_args.ps1 -envNames "@${{ inputs.env_names }}" -entryPoint "@${{ inputs.entrypoint }}" -extraArgs "@${{ inputs.extra_args }}";
76+
${{ github.action_path }}/src/assign_default_environment_variables.ps1;
7677
shell: powershell
7778
- name: docker_login
7879
id: docker_login
@@ -92,7 +93,9 @@ runs:
9293
shell: powershell
9394
- name: Run
9495
run: >-
95-
docker run ${{ steps.settings.outputs.extra_args }}
96+
docker run
97+
${{ steps.settings.outputs.default_environment_variables }}
98+
${{ steps.settings.outputs.extra_args }}
9699
--rm
97100
--memory=${{ inputs.memory }}
98101
-v ${{ steps.settings.outputs.workspace_path }}:${{ steps.settings.outputs.mapping_path }}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
Describe "assign_default_environment_variables" {
3+
BeforeEach{
4+
function toGitHub () {
5+
param(
6+
$output_name,
7+
$output_value
8+
)
9+
Write-Output "$output_name=$output_value";
10+
}
11+
Mock toGitHub -MockWith { Write-Output "$output_name=$output_value";}
12+
}
13+
14+
Context "when processing default GitHub environment variables" {
15+
It "should output all default GitHub environment variables as --env arguments" {
16+
$result = .\assign_default_environment_variables.ps1
17+
18+
$defaultGitHubEnvVars = @(
19+
"CI", "GITHUB_ACTION", "GITHUB_ACTION_PATH", "GITHUB_ACTION_REPOSITORY",
20+
"GITHUB_ACTIONS", "GITHUB_ACTOR", "GITHUB_API_URL", "GITHUB_BASE_REF",
21+
"GITHUB_ENV", "GITHUB_EVENT_NAME", "GITHUB_EVENT_PATH", "GITHUB_HEAD_REF",
22+
"GITHUB_JOB", "GITHUB_OUTPUT", "GITHUB_PATH", "GITHUB_REF", "GITHUB_REF_NAME",
23+
"GITHUB_REF_PROTECTED", "GITHUB_REF_TYPE", "GITHUB_REPOSITORY",
24+
"GITHUB_REPOSITORY_OWNER", "GITHUB_RETENTION_DAYS", "GITHUB_RUN_ATTEMPT",
25+
"GITHUB_RUN_ID", "GITHUB_RUN_NUMBER", "GITHUB_SERVER_URL", "GITHUB_SHA",
26+
"GITHUB_STEP_SUMMARY", "GITHUB_WORKFLOW", "GITHUB_WORKSPACE", "RUNNER_ARCH",
27+
"RUNNER_DEBUG", "RUNNER_NAME", "RUNNER_OS", "RUNNER_TEMP", "RUNNER_TOOL_CACHE"
28+
)
29+
30+
# Assert: Verify the output contains all default environment variables
31+
foreach ($envVar in $defaultGitHubEnvVars) {
32+
$result | Should -Match "--env $envVar"
33+
}
34+
35+
# Assert: Verify the output variable is set correctly
36+
$result | Should -Match "default_environment_variables="
37+
}
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
$invocationPath = $PSScriptRoot
2+
$setOutputVariable = Join-Path $invocationPath "set_output_variable.ps1"
3+
$splitToArgs = Join-Path $invocationPath "split_to_args.ps1"
4+
$trimArg = Join-Path $invocationPath "trim_arg.ps1"
5+
6+
$defaultGitHubEnvVars = @(
7+
"CI", "GITHUB_ACTION", "GITHUB_ACTION_PATH", "GITHUB_ACTION_REPOSITORY",
8+
"GITHUB_ACTIONS", "GITHUB_ACTOR", "GITHUB_API_URL", "GITHUB_BASE_REF",
9+
"GITHUB_ENV", "GITHUB_EVENT_NAME", "GITHUB_EVENT_PATH", "GITHUB_HEAD_REF",
10+
"GITHUB_JOB", "GITHUB_OUTPUT", "GITHUB_PATH", "GITHUB_REF", "GITHUB_REF_NAME",
11+
"GITHUB_REF_PROTECTED", "GITHUB_REF_TYPE", "GITHUB_REPOSITORY",
12+
"GITHUB_REPOSITORY_OWNER", "GITHUB_RETENTION_DAYS", "GITHUB_RUN_ATTEMPT",
13+
"GITHUB_RUN_ID", "GITHUB_RUN_NUMBER", "GITHUB_SERVER_URL", "GITHUB_SHA",
14+
"GITHUB_STEP_SUMMARY", "GITHUB_WORKFLOW", "GITHUB_WORKSPACE", "RUNNER_ARCH",
15+
"RUNNER_DEBUG", "RUNNER_NAME", "RUNNER_OS", "RUNNER_TEMP", "RUNNER_TOOL_CACHE"
16+
)
17+
18+
$defaultGitHubEnv = & $splitToArgs -argument_name "--env" -env_arrays $defaultGitHubEnvVars
19+
$trimmedArgs = & $trimArg -arg $defaultGitHubEnv
20+
& $setOutputVariable -name "default_environment_variables" -val "@$trimmedArgs"

test/default_environment.Tests.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Describe "default environment" {
2+
Context "default environment should always be set" {
3+
It "CI should be true" {
4+
Write-Output $env:CI | Should -Be "true"
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)