Skip to content

Commit

Permalink
Fix: Add compatibility with the new Github Environment Files (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlerdominik authored Feb 27, 2024
1 parent 53dd9d8 commit 9dc5fa2
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 36 deletions.
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

0 comments on commit 9dc5fa2

Please sign in to comment.