action.yml raw
1 name: 'Run in Docker with environment'
2 description: 'Run a command in a Docker container, while passing explicitly set environment variables into the container.'
3 inputs:
4 dockerfile:
5 description: 'A Dockerfile that defines an image'
6 required: true
7 scope:
8 description: 'A cached image scope'
9 required: true
10 command:
11 description: 'A command to run in a container'
12 required: true
13 runs:
14 using: "composite"
15 steps:
16 - uses: docker/setup-buildx-action@v4
17
18 - uses: docker/build-push-action@v7
19 id: main_builder
20 continue-on-error: true
21 with:
22 context: .
23 file: ${{ inputs.dockerfile }}
24 load: true
25 cache-from: type=gha,scope=${{ inputs.scope }}
26
27 - uses: docker/build-push-action@v7
28 id: retry_builder
29 if: steps.main_builder.outcome == 'failure'
30 with:
31 context: .
32 file: ${{ inputs.dockerfile }}
33 load: true
34 cache-from: type=gha,scope=${{ inputs.scope }}
35
36 - # Workaround for https://github.com/google/sanitizers/issues/1614 .
37 # The underlying issue has been fixed in clang 18.1.3.
38 run: sudo sysctl -w vm.mmap_rnd_bits=28
39 shell: bash
40
41 - # Tell Docker to pass environment variables in `env` into the container.
42 run: >
43 docker run \
44 $(echo '${{ toJSON(env) }}' | jq -r 'keys[] | "--env \(.) "') \
45 --volume ${{ github.workspace }}:${{ github.workspace }} \
46 --workdir ${{ github.workspace }} \
47 ${{ case(steps.main_builder.outcome == 'success', steps.main_builder.outputs.imageid, steps.retry_builder.outputs.imageid) }} \
48 bash -c "
49 git config --global --add safe.directory ${{ github.workspace }}
50 ${{ inputs.command }}
51 "
52 shell: bash
53