Skip to content

Commit

Permalink
Add GitHub cache example
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
CrazyMax committed Aug 29, 2020
1 parent fbd19d2 commit c124ff0
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 10 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,68 @@ jobs:
if: always()
uses: crazy-max/ghaction-dump-context@v1

github-cache:
runs-on: ubuntu-latest
steps:
-
name: Run local registry
run: |
docker run -d -p 5000:5000 registry:2
-
name: Checkout
uses: actions/checkout@v2.3.1
-
name: Set up QEMU
uses: docker/setup-qemu-action@master
with:
platforms: all
-
name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@master
with:
driver-opt: network=host
buildkitd-flags: --allow-insecure-entitlement security.insecure
-
name: Cache Docker layers
uses: actions/cache@v2
id: cache
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
-
name: Build and push
id: docker_build
uses: ./
with:
context: ./test
file: ./test/Dockerfile-multi-golang
builder: ${{ steps.buildx.outputs.name }}
platforms: linux/amd64,linux/arm64
allow: network.host,security.insecure
push: true
tags: |
localhost:5000/name/app:latest
localhost:5000/name/app:1.0.0
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
-
name: Inspect
run: |
docker buildx imagetools inspect localhost:5000/name/app:1.0.0
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Cache hit
run: echo ${{ steps.cache.outputs.cache-hit }}
-
name: Dump context
if: always()
uses: crazy-max/ghaction-dump-context@v1

git-context:
runs-on: ubuntu-latest
strategy:
Expand Down
80 changes: 70 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

GitHub Action to build and push Docker images.

> :bulb: See also our [setup-buildx](https://github.com/docker/setup-buildx-action)
> and [setup-qemu](https://github.com/docker/setup-qemu-action) actions
> :bulb: See also:
> * [login](https://github.com/docker/login-action) action
> * [setup-buildx](https://github.com/docker/setup-buildx-action) action
> * [setup-qemu](https://github.com/docker/setup-qemu-action) action
![Screenshot](.github/build-push-action.png)

Expand All @@ -18,6 +20,7 @@ ___
* [Quick start](#quick-start)
* [Multi-platform image](#multi-platform-image)
* [Git context](#git-context)
* [Leverage GitHub cache](#leverage-github-cache)
* [Complete workflow](#complete-workflow)
* [Customizing](#customizing)
* [inputs](#inputs)
Expand Down Expand Up @@ -51,13 +54,13 @@ jobs:
uses: actions/checkout@v2
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@master
with:
platforms: all
-
name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@master
-
name: Login to DockerHub
uses: docker/login-action@v1
Expand Down Expand Up @@ -95,13 +98,13 @@ jobs:
uses: actions/checkout@v2
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@master
with:
platforms: all
-
name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@master
-
name: Login to DockerHub
uses: docker/login-action@v1
Expand Down Expand Up @@ -140,13 +143,13 @@ jobs:
steps:
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@master
with:
platforms: all
-
name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@master
with:
version: latest
-
Expand All @@ -170,6 +173,63 @@ jobs:
GIT_AUTH_TOKEN: ${{ github.token }}
```

### Leverage GitHub cache

You can leverage [GitHub cache](https://docs.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows)
using [@actions/cache](https://github.com/actions/cache) with this action.

```yaml
name: ci
on:
push:
branches: master
jobs:
main:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up QEMU
uses: docker/setup-qemu-action@master
with:
platforms: all
-
name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@master
-
name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
-
name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
builder: ${{ steps.buildx.outputs.name }}
push: true
tags: user/app:latest
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
```

### Complete workflow

* On `pull_request` event, Docker image `name/app:edge` is **built**.
Expand Down Expand Up @@ -216,13 +276,13 @@ jobs:
echo ::set-output name=tags::${TAGS}
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@master
with:
platforms: all
-
name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@master
-
name: Login to DockerHub
if: github.event_name != 'pull_request'
Expand Down

0 comments on commit c124ff0

Please sign in to comment.