Skip to content

Commit

Permalink
feat(docker): add args as label to improve caching
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Dec 23, 2021
1 parent 24348a9 commit 96803da
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Builds a docker image from a Dockerfile. Layers are cached and pruned between jo
| Yes | `key` | Cache key | `my-image-${{ hashFiles('Dockerfile') }}` | `${{ github.workflow }}` |
| No | `dockerfile` | Path to dockerfile | `./docker/prod.Dockerfile` | `Dockerfile` |
| No | `context` | Directory to build from | `./docker` | `.` |
| No | `build_args` | Arguments to pass to docker build | `--target prod` | |
| No | `docker_args` | Arguments to pass to docker build | `--target prod` | |
| No | `prune_after` | Amount of time until which images get pruned | `24h` | `260h` (2 weeks) |

#### Outputs
Expand All @@ -140,7 +140,7 @@ Builds a docker image from a Dockerfile. Layers are cached and pruned between jo
image: myparcel/php-sdk
dockerfile: Dockerfile
context: .
build_args: --target test
docker_args: --target test

- run: docker run ${{ steps.docker.outputs.tagged_image }}
```
Expand Down
8 changes: 6 additions & 2 deletions build-docker-image/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ inputs:
required: false
default: .

build_args:
docker_args:
description: 'Arguments to pass to docker build'
required: false
default: ''
Expand All @@ -46,7 +46,9 @@ runs:
id: prep
run: |
IMAGE="myparcel/php-sdk"
ARGS=$(echo ${{ inputs.docker_args }} | shasum | head -c 40)
TAG=$(echo $GITHUB_SHA | head -c7)
echo ::set-output name=args::${ARGS}
echo ::set-output name=tag::${TAG}
echo ::set-output name=tagged_image::${IMAGE}:${TAG}
shell: bash
Expand All @@ -60,7 +62,8 @@ runs:
- name: 'Build image'
shell: bash
run: >
docker build ${{ inputs.build_args }} \
docker build ${{ inputs.docker_args }} \
--label args=${{ steps.prep.outputs.args }} \
--label ref=${{ github.ref }} \
--label tag=${{ steps.prep.outputs.tag }} \
--tag ${{ steps.prep.outputs.tagged_image }} \
Expand All @@ -74,6 +77,7 @@ runs:
--all \
--force \
--filter "label=ref=${{ github.ref }}" \
--filter "label=args=${{ steps.prep.outputs.args }}" \
--filter "label!=tag=${{ steps.prep.outputs.tag }}"
- name: 'Prune stale images'
Expand Down

0 comments on commit 96803da

Please sign in to comment.