Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add compatibility with the new Github Environment Files #27

Merged
merged 44 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c41db42
test paths
kohlerdominik Feb 27, 2024
9bb90d8
fixed typo
kohlerdominik Feb 27, 2024
05d9965
get folder
kohlerdominik Feb 27, 2024
7ef9ed2
more output
kohlerdominik Feb 27, 2024
3842cbe
Allow Input and Output from Githubs env files.
kohlerdominik Feb 27, 2024
0548fa9
Added test for environment files
kohlerdominik Feb 27, 2024
95eb9e5
Added file creation for all mounted files
kohlerdominik Feb 27, 2024
e426b9b
Create files sync
kohlerdominik Feb 27, 2024
d4ebab7
Get directory contents
kohlerdominik Feb 27, 2024
35a4fbc
compiled action
kohlerdominik Feb 27, 2024
b346e3b
show tmp files
kohlerdominik Feb 27, 2024
7026910
chown env files
kohlerdominik Feb 27, 2024
b186642
chmod for env files
kohlerdominik Feb 27, 2024
1f9b444
test with runner path
kohlerdominik Feb 27, 2024
8dc872e
test with runner path
kohlerdominik Feb 27, 2024
7e72764
test with runner paths
kohlerdominik Feb 27, 2024
aeae658
test
kohlerdominik Feb 27, 2024
e2c4711
test
kohlerdominik Feb 27, 2024
72587d7
test
kohlerdominik Feb 27, 2024
a020c60
fixed permissions the hard way
kohlerdominik Feb 27, 2024
deb2955
removed fix
kohlerdominik Feb 27, 2024
f3e9ed0
fix
kohlerdominik Feb 27, 2024
59dec33
show permissions on hst
kohlerdominik Feb 27, 2024
e1bc19e
test another
kohlerdominik Feb 27, 2024
66f64d7
fixed permissions
kohlerdominik Feb 27, 2024
84b80ac
really fix permissions
kohlerdominik Feb 27, 2024
285f6f4
another permission-fix
kohlerdominik Feb 27, 2024
1f8d5eb
permission fix....
kohlerdominik Feb 27, 2024
16704d4
test with alpine
kohlerdominik Feb 27, 2024
ba42bf1
check in other dir
kohlerdominik Feb 27, 2024
9508287
try another tempdir
kohlerdominik Feb 27, 2024
cd2e37b
fix permissions
kohlerdominik Feb 27, 2024
e9582f5
cleanup and fix
kohlerdominik Feb 27, 2024
868dfe9
fixed state test
kohlerdominik Feb 27, 2024
35c15c9
github state
kohlerdominik Feb 27, 2024
1fc3b73
better test coverage
kohlerdominik Feb 27, 2024
daa0229
better test coverage
kohlerdominik Feb 27, 2024
9be2238
better test coverage
kohlerdominik Feb 27, 2024
b43875c
better test coverage
kohlerdominik Feb 27, 2024
f1ad514
better test coverage
kohlerdominik Feb 27, 2024
d92d193
Final fix
kohlerdominik Feb 27, 2024
0db2914
Test for Tempdir
kohlerdominik Feb 27, 2024
2c23417
Test for Tempdir
kohlerdominik Feb 27, 2024
afdc85e
Added to readme
kohlerdominik Feb 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
Expand All @@ -51,4 +51,4 @@
"es6": true,
"jest/globals": true
}
}
}
46 changes: 37 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
uses: ./
with:
image: ubuntu:latest
run: echo "::set-output name=success::true"
run: echo "success=true" >> $GITHUB_OUTPUT
- name: "Validate Test: Run a command in a public image"
run: test.sh equal "${{ steps.test-simple.outputs.success }}" "true"

Expand All @@ -53,7 +53,6 @@ jobs:
[ "${PWD}" != "/" ] && echo "ERROR: Workdir is not /" && exit 1
# check if the container is attached to the local docker network:
ping -c 1 nginx


- name: "Test: Multiline run fails on error (should have error)"
continue-on-error: true
Expand All @@ -63,9 +62,9 @@ jobs:
image: ubuntu:latest
run: |
echo "start"
echo "::set-output name=beforeError::true"
echo "beforeError=true" >> $GITHUB_OUTPUT
cat non-existing-file
echo "::set-output name=afterError::true"
echo "afterError=true" >> $GITHUB_OUTPUT
echo "end"
- name: "Validate Test: Multiline run fails on error"
run: |
Expand All @@ -78,7 +77,7 @@ jobs:
with:
image: ubuntu:latest
shell: /bin/bash
run: echo "::set-output name=bash-version::${BASH_VERSION}"
run: echo "bash-version=${BASH_VERSION}" >> $GITHUB_OUTPUT
- name: "Validate Test: Shell input"
run: test.sh not-empty "${{ steps.test-shell-input.outputs.bash-version }}"

Expand All @@ -91,8 +90,8 @@ jobs:
GITHUB_JOB
CUSTOM_VAR=foo
run: |
echo "::set-output name=github-job::${GITHUB_JOB}"
echo "::set-output name=custom-var::${CUSTOM_VAR}"
echo "github-job=${GITHUB_JOB}" >> $GITHUB_OUTPUT
echo "custom-var=${CUSTOM_VAR}" >> $GITHUB_OUTPUT
- name: "Validate Test: Env input"
run: |
test.sh equal "${{ steps.test-env-input.outputs.github-job }}" "${GITHUB_JOB}"
Expand Down Expand Up @@ -122,10 +121,20 @@ jobs:
workdir: "/var/mywork"
run: |
echo "present" > /var/mywork/test-file
echo "::set-output name=file-content::$(cat ./test-file)"
echo "file-content=$(cat ./test-file)" >> $GITHUB_OUTPUT
- name: "Validate Test: Workdir input"
run: test.sh equal "${{ steps.test-workdir-input.outputs.file-content }}" "present"

- name: "Test: Tempdir input"
id: test-tempdir-input
uses: ./
with:
image: ubuntu:latest
tempdir: "/var/mytemp"
run: |
ls /var/mytemp/command*
ls /var/mytemp/set_output*

- name: "Test: Options input"
id: test-options-input
uses: ./
Expand All @@ -135,9 +144,28 @@ jobs:
--name=TestContainer
--label test
-e TEST_VAR=bar
run: echo "::set-output name=test-var::${TEST_VAR}"
run: echo "test-var=${TEST_VAR}" >> $GITHUB_OUTPUT
- name: "Validate Test: Options input"
run: test.sh equal "${{ steps.test-options-input.outputs.test-var }}" "bar"

- name: "Test: Setting Environment Files (env, output, summary, path)"
id: test-runner-env
uses: ./
with:
image: ubuntu:latest
volumes: ${{ github.workspace }}:/workspace
run: |
echo "test_env=IsSet" >> "$GITHUB_ENV"
echo "test_path=/IsSet" >> "$GITHUB_PATH"
echo "test_output=IsSet" >> "$GITHUB_OUTPUT"

echo "test_state=IsSet" >> "$GITHUB_STATE"
echo "### Step summary worked! :rocket:" >> "$GITHUB_STEP_SUMMARY"
- name: "Validate Test: Setting Environment Files (env, output, summary, path)"
run: |
test.sh equal "$test_env" "IsSet"
test.sh contains "$PATH" "IsSet"
test.sh equal "${{ steps.test-runner-env.outputs.test_output }}" "IsSet"

- name: Authorize docker for private image
uses: docker/login-action@v3
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ See also [Docker Login Action](https://github.com/marketplace/actions/docker-log
```
See also [Docker Push Action](https://github.com/marketplace/actions/build-and-push-docker-images)

### Use Github Environment Files
```yaml
- uses: kohlerdominik/docker-run-action@v1
with:
image: ubuntu:latest
volumes: ${{ github.workspace }}:/workspace
run: |
echo "MY_VARIABLE=Hello World" >> "$GITHUB_ENV"
echo "result=success" >> "$GITHUB_OUTPUT"

echo "### We did it! :rocket:" >> "$GITHUB_STEP_SUMMARY"
```
See also [Environment Files](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files)

### Customize the action
```yaml
- uses: kohlerdominik/docker-run-action@v1
Expand All @@ -61,6 +75,8 @@ See also [Docker Push Action](https://github.com/marketplace/actions/build-and-p
shell: /bin/bash
# change workdir (default directory for the shell)
workdir: /workspace
# change tempdir (do this only for very good reason)
tempdir: /tmp/_action
# pass or create environment variables
environment: |
GITHUB_REF
Expand Down Expand Up @@ -105,4 +121,4 @@ Contributions are very welcome. All functionality is covered with a test, so if
npm run all
```
* Push your changes.
* If you're ready, create a Pull Request.
* If you're ready, create a Pull Request.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ inputs:
description: 'Custom volume mounts'
workdir:
required: false
description: "Custom workdir"
description: 'Custom workdir'
tempdir:
required: false
description: 'Custom tempdir'
default: /tmp/_action
options:
required: false
description: 'Additional run options'
Expand Down
77 changes: 67 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docker-run-action",
"version": "1.2.0",
"version": "2.0.0",
"private": true,
"description": "Run commands in a docker container.",
"main": "lib/main.js",
Expand Down
36 changes: 27 additions & 9 deletions src/dockerRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ import * as exec from '@actions/exec'
import * as fs from 'fs'
import {v4 as uuidv4} from 'uuid'
import * as input from './input'

export const COMMAND_FILE_NAME = `${uuidv4()}.sh`
export const LOCAL_COMMAND_PATH = `${process.env.RUNNER_TEMP}/${COMMAND_FILE_NAME}`
export const CONTAINER_COMMAND_PATH = `/tmp/${COMMAND_FILE_NAME}`
import {FileMap, Mapping} from './fileMap'

export async function runContainer(): Promise<void> {
fs.writeFileSync(LOCAL_COMMAND_PATH, input.get('run'), {mode: 0o755})
// prepare the files for the command
const fileMap = new FileMap(input.get('tempdir'))

fileMap.pushRunnerPath('GITHUB_ENV', process.env.GITHUB_ENV)
fileMap.pushRunnerPath('GITHUB_PATH', process.env.GITHUB_PATH)
fileMap.pushRunnerPath('GITHUB_OUTPUT', process.env.GITHUB_OUTPUT)
fileMap.pushRunnerPath('GITHUB_STATE', process.env.GITHUB_STATE)
fileMap.pushRunnerPath('GITHUB_STEP_SUMMARY', process.env.GITHUB_STEP_SUMMARY)
const command = fileMap.pushRunnerPath(
'CONTAINER_COMMAND',
`${process.env.RUNNER_TEMP}/command_${uuidv4()}`
)

fs.writeFileSync(command!.runner.path, input.get('run'), {mode: 0o755})
core.info(`
Wrote instruction file to "${LOCAL_COMMAND_PATH}"
Wrote instruction file to "${command!.runner.path}"
with these instructions:
----- START OF FILE -----
${fs.readFileSync(LOCAL_COMMAND_PATH).toString()}
${fs.readFileSync(command!.runner.path).toString()}
----- END OF FILE -----`)

// run the command
await exec.exec('docker', [
`run`,
// default network
Expand All @@ -27,14 +38,21 @@ ${fs.readFileSync(LOCAL_COMMAND_PATH).toString()}
...(input.has('workdir') ? [`--workdir=${input.get('workdir')}`] : []),
// environment options
...input.getEnvironment(),
...fileMap.map(
(item: Mapping, key: string): string =>
`--env=${key}=${item.container.path}`
),
// volume options
`--volume=${LOCAL_COMMAND_PATH}:${CONTAINER_COMMAND_PATH}`,
...fileMap.map(
(item: Mapping): string =>
`--volume=${item.runner.path}:${item.container.path}`
),
...input.getVolumes(),
// other options
...input.getSplittet('options'),
input.get('image'),
input.get('shell'),
'-e',
CONTAINER_COMMAND_PATH
command!.container.path
])
}
Loading
Loading