2021-02-17 12:53:15 -05:00
# Handle tags and labels
2022-01-30 12:12:55 -05:00
If you want an "automatic" tag management and [OCI Image Format Specification ](https://github.com/opencontainers/image-spec/blob/master/annotations.md )
2021-05-10 12:01:34 -04:00
for labels, you can do it in a dedicated step. The following workflow will use the [Docker metadata action ](https://github.com/docker/metadata-action )
2021-02-17 12:53:15 -05:00
to handle tags and labels based on GitHub actions events and Git metadata.
```yaml
name: ci
on:
schedule:
2022-01-30 12:12:55 -05:00
- cron: '0 10 * * *'
2021-02-17 12:53:15 -05:00
push:
branches:
- '**'
tags:
- 'v*.*.*'
pull_request:
branches:
2022-01-18 08:57:27 -05:00
- 'main'
2021-02-17 12:53:15 -05:00
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Docker meta
2021-03-29 08:33:52 -04:00
id: meta
2022-05-05 13:24:32 -04:00
uses: docker/metadata-action@v4
2021-02-17 12:53:15 -05:00
with:
# list of Docker images to use as base name for tags
images: |
name/app
ghcr.io/username/app
2021-03-29 08:33:52 -04:00
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
2021-02-17 12:53:15 -05:00
-
name: Set up QEMU
2022-05-05 13:24:32 -04:00
uses: docker/setup-qemu-action@v2
2021-02-17 12:53:15 -05:00
-
name: Set up Docker Buildx
2022-05-05 13:24:32 -04:00
uses: docker/setup-buildx-action@v2
2021-02-17 12:53:15 -05:00
-
name: Login to DockerHub
if: github.event_name != 'pull_request'
2022-05-05 13:24:32 -04:00
uses: docker/login-action@v2
2021-02-17 12:53:15 -05:00
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Login to GHCR
if: github.event_name != 'pull_request'
2022-05-05 13:24:32 -04:00
uses: docker/login-action@v2
2021-02-17 12:53:15 -05:00
with:
registry: ghcr.io
2021-04-03 14:41:31 -04:00
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
2021-02-17 12:53:15 -05:00
-
name: Build and push
2022-05-05 13:24:32 -04:00
uses: docker/build-push-action@v3
2021-02-17 12:53:15 -05:00
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
2021-03-29 08:33:52 -04:00
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2021-02-17 12:53:15 -05:00
```