Skip to content

Commit

Permalink
feat(build-docker-image): add action
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Dec 9, 2021
1 parent 52b7218 commit dedad05
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

This is a collection of reusable composite actions for GitHub Actions workflows.

- [Node](#node)
- [npm-install](#npm-install)
- [setup-node](#setup-node)
- [yarn-install](#yarn-install)
- [PHP](#php)
- [composer-install](#composer-install)
- [Testing](#testing)
- [update-coverage](#update-coverage)
- [Docker](#docker)
- [build-docker-image](#build-docker-image)

## Node

### npm-install

[Source](npm-install/action.yml)

1. Runs [setup-node]
2. Handles cache
3. Runs `npm ci`
Expand All @@ -20,6 +33,8 @@ This is a collection of reusable composite actions for GitHub Actions workflows.
### setup-node
[Source](setup-node/action.yml)
1. Sets up node@14
- You can change the version by passing `node-version`.

Expand All @@ -34,6 +49,8 @@ This is a collection of reusable composite actions for GitHub Actions workflows.

### yarn-install

[Source](yarn-install/action.yml)

1. Runs [setup-node]
2. Handles cache
3. Runs `yarn install --frozen-lockfile`
Expand All @@ -50,6 +67,8 @@ This is a collection of reusable composite actions for GitHub Actions workflows.

### composer-install

[Source](composer-install/action.yml)

1. Sets up php@7.2 with composer v2
- You can change the php version by passing `php-version`.
- You can change the composer version or install any other tools by passing `tools`.
Expand All @@ -73,6 +92,8 @@ This is a collection of reusable composite actions for GitHub Actions workflows.

### update-coverage

[Source](update-coverage/action.yml)

1. Runs [codecov/codecov-action]
- Needs a [Codecov] token in `token`.

Expand All @@ -84,7 +105,48 @@ This is a collection of reusable composite actions for GitHub Actions workflows.
token: ${{ secrets.CODECOV_TOKEN }}
```

## Docker

### build-docker-image

[Source](build-docker-image/action.yml)

Builds a docker image from a Dockerfile. Layers are cached and pruned between jobs based on git ref and tag.

#### Inputs

| required | name | description | Example | Default |
|----------|---------------|----------------------------------------------|-------------------------------------------|--------------------------|
| Yes | `image` | Image name | `my-name/my-image` | – |
| 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 | `prune_after` | Amount of time until which images get pruned | `24h` | `260h` (2 weeks) |

#### Outputs

| name | description | Example |
|----------------|-----------------------------|-------------------------------|
| `tagged_image` | Created image name with tag | `my-name/my-image:1639002200` |
| `tag` | Tag of the created image | `1639002200` |

#### Example

```yaml
- uses: myparcelnl/actions/build-docker-image@v1
id: docker
with:
image: myparcel/php-sdk
dockerfile: Dockerfile
context: .
build_args: --target test
- run: docker run ${{ steps.docker.outputs.tagged_image }}
```

[Codecov]: https://codecov.io
[build-docker-image]: #build-docker-image
[codecov/codecov-action]: https://github.com/codecov/codecov-action
[composer-install]: #composer-install
[npm-install]: #npm-install
Expand Down
84 changes: 84 additions & 0 deletions build-docker-image/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Build Docker image
description: Build Docker image from cache

inputs:
image:
description: 'Image name'
required: true

key:
description: 'Cache key'
required: false
default: ${{ github.workflow }}

dockerfile:
description: 'Path to dockerfile'
required: false
default: Dockerfile

context:
description: 'Directory to build from'
required: false
default: .

build_args:
description: 'Arguments to pass to docker build'
required: false
default: ''

prune_after:
description: 'Amount of time after which images get pruned'
required: false
default: '260h'

outputs:
tagged_image:
description: 'Created image name with tag'
value: ${{ steps.prep.outputs.tagged_image }}
tag:
description: 'Tag of the created image'
value: ${{ steps.prep.outputs.tag }}

runs:
using: composite
steps:
- name: 'Prepare'
id: prep
run: |
IMAGE="myparcel/php-sdk"
TAG=$(echo $GITHUB_SHA | head -c7)
echo ::set-output name=tag::${TAG}
echo ::set-output name=tagged_image::${IMAGE}:${TAG}
shell: bash

- uses: satackey/action-docker-layer-caching@v0.0.11
with:
key: ${{ inputs.key }}-{hash}
restore-keys: |
${{ inputs.key }}-
- name: 'Build image'
shell: bash
run: >
docker build ${{ inputs.build_args }} \
--label ref=${{ github.ref }} \
--label tag=${{ steps.prep.outputs.tag }} \
--tag ${{ steps.prep.outputs.tagged_image }} \
--file ${{ inputs.dockerfile }} \
${{ inputs.context }}
- name: 'Prune previous images'
shell: bash
run: >
docker image prune \
--all \
--force \
--filter "label=ref=${{ github.ref }}" \
--filter "label!=tag=${{ steps.prep.outputs.tag }}"
- name: 'Prune stale images'
shell: bash
run: >
docker image prune \
--force \
--filter "until=${{ inputs.prune_after }}"

0 comments on commit dedad05

Please sign in to comment.