Skip to content

Commit

Permalink
feat(docker): add action to build using registry
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Jun 15, 2022
1 parent 2afbe6f commit 0ff99ff
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Will use every patch update within `v2.1.x`.
- [update-coverage](#update-coverage)
- [Docker](#docker)
- [build-docker-image](#build-docker-image)
- [build-docker-image-reg](#build-docker-image-reg)
- [Releasing](#releasing)
- [semantic-release](#semantic-release)
- [Git](#git)
Expand Down Expand Up @@ -194,6 +195,45 @@ jobs based on git ref and tag.
- run: docker run ${{ steps.docker.outputs.tagged-image }}
```

### build-docker-image-reg

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

Builds a docker image from a Dockerfile. Layers are cached and pruned between
jobs using a registry.

#### Inputs

| required | name | description | Example | Default |
|----------|---------------------|------------------------------------|---------------------------------------|--------------|
| Yes | `image` | Image name | `my-name/my-image` ||
| No | `dockerfile` | Path to dockerfile | `./docker/prod.Dockerfile` | `Dockerfile` |
| No | `context` | Directory to build from | `./docker` | `.` |
| No | `target` | Target stage to build | `--target prod` ||
| No | `registry` | Packages registry to use | `docker.io` | `ghcr.io` |
| Yes | `registry-username` | Username to log into registry with | `${{ secrets.DOCKER_REGISTRY_USER }}` ||
| Yes | `registry-password` | Password to log into registry with | `${{ secrets.DOCKER_REGISTRY_PASS }}` ||

#### 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@v2
id: docker
with:
image: myparcel/php-sdk
registry-username: ${{ github.actor }}
registry-password: ${{ secrets.GITHUB_TOKEN }}

- run: docker run ${{ steps.docker.outputs.tagged-image }}
```

## Releasing

### semantic-release
Expand Down Expand Up @@ -283,13 +323,14 @@ If run with `minor: true`:
- Will add `v2` to the current commit
- Will add `v2.3` to the current commit

If run without `minor: true`, or with `minor: false`:
If run without `minor: true`, or with `minor: false`:

- Will add `v2` to the current commit.

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

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

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

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

target:
description: 'Target stage to build'
required: false

registry:
description: 'Packages registry to use'
required: false
default: 'ghcr.io'

registry-user:
description: 'Username to log into registry with'
required: true

registry-password:
description: 'Password to log into registry with'
required: true

outputs:
tagged-image:
description: 'Created image name with tag'
value: ${{ steps.prepare.outputs.tagged-image }}

tags:
description: 'Tags of the created image'
value: ${{ steps.meta.outputs.tags }}

labels:
description: 'Labels of the created image'
value: ${{ steps.meta.outputs.labels }}

runs:
using: composite
steps:
- name: 'Prepare'
id: prepare
run: |
IMAGE=$(echo ${{ inputs.image }})
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

- uses: actions/checkout@v3

- uses: docker/login-action@v2
with:
registry: ${{ inputs.registry }}
username: ${{ inputs.registry-user }}
password: ${{ inputs.registry-password }}

- uses: docker/metadata-action@v4
id: meta
with:
images: ${{ inputs.registry }}/${{ inputs.image }}

- uses: int128/docker-build-cache-config-action@v1
id: cache
with:
image: ${{ inputs.registry }}/${{ inputs.image }}/cache

- uses: docker/setup-buildx-action@v2

- uses: docker/build-push-action@v3
with:
context: ${{ inputs.context }}
push: true
target: ${{ inputs.target }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: ${{ steps.cache.outputs.cache-from }}
cache-to: ${{ steps.cache.outputs.cache-to }}

0 comments on commit 0ff99ff

Please sign in to comment.