Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
pull_request:

jobs:
ci:
ghcr:
runs-on: "ubuntu-20.04" # https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on

steps:
Expand All @@ -31,3 +31,35 @@ jobs:
docker rmi ${IMAGE}
docker pull ${IMAGE}
docker image inspect ${IMAGE} | jq '.[].Config.Labels' | grep "${GITHUB_SHA}"

ghcr_and_docker_io:
runs-on: "ubuntu-20.04" # https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on

steps:
- uses: actions/checkout@v2

- name: Run an action
uses: ./
with:
image_name: ${{ github.repository }}
github_token: ${{ secrets.GITHUB_TOKEN }}
docker_io_token: ${{ secrets.DOCKER_IO_ACCESS_TOKEN }} # see https://hub.docker.com/settings/security

- name: Make sure that an image has been built
env:
IMAGE: ${{ github.repository }}
run: |
docker images | grep ${IMAGE}

- name: Make sure that an image has been pushed and is properly labelled
env:
IMAGE: ${{ github.repository }}
run: |
docker rmi ghcr.io/${IMAGE}
docker rmi docker.io/${IMAGE}

docker pull ghcr.io/${IMAGE}
docker pull docker.io/${IMAGE}

docker image inspect ghcr.io/${IMAGE} | jq '.[].Config.Labels' | grep "${GITHUB_SHA}"
docker image inspect docker.io/${IMAGE} | jq '.[].Config.Labels' | grep "${GITHUB_SHA}"
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ Create a new GitHub Actions workflow as follows:
name: Build and publish a Docker image to ghcr.io
on:

# publish on releases, e.g. v2.1.13 (tagged as "2.1.13" - "v" prefix is removed)
# publish on releases, e.g. v2.1.13 (image tagged as "2.1.13" - "v" prefix is removed)
release:
types: [ published ]

# publish on pushes to the main branch (tagged as "latest")
# publish on pushes to the main branch (image tagged as "latest")
push:
branches:
- master
Expand All @@ -36,10 +36,12 @@ jobs:

# https://github.com/marketplace/actions/push-to-ghcr
- name: Build and publish a Docker image for ${{ github.repository }}
uses: macbre/push-to-ghcr@v2
uses: macbre/push-to-ghcr@master
with:
image_name: ${{ github.repository }}
github_token: ${{ secrets.GITHUB_TOKEN }}
# optionally push to the Docker Hub (docker.io)
# docker_io_token: ${{ secrets.DOCKER_IO_ACCESS_TOKEN }} # see https://hub.docker.com/settings/security
```

This action assumes that your `Dockerfile` is in the root directory of your repository.
Expand All @@ -65,3 +67,23 @@ Additonally, `BUILD_DATE` and `GITHUB_SHA` build args are set resulting with the
BUILD_DATE=2021-07-01T12:52:03Z
GITHUB_SHA=26b095f37cdf56a632aa2235345d4174b26e1d66
```

## Optional pushes to Docker Hub (docker.io)

On 18th June 2021 [Docker Hub discontinued Autobuilds on the free tier](https://www.docker.com/blog/changes-to-docker-hub-autobuilds/). However, you can use this action to additionally push to docker.io repository.

1. You will need an access tokens created via https://hub.docker.com/settings/security.
2. Store it in your GitHub repository secrets, e.g. as `DOCKER_IO_ACCESS_TOKEN`.
3. Provide additional option in `with` section in action invocation:

```yaml
# (...)
- name: Build and publish a Docker image for ${{ github.repository }}
uses: macbre/push-to-ghcr@master
with:
image_name: ${{ github.repository }}
github_token: ${{ secrets.GITHUB_TOKEN }}
docker_io_token: ${{ secrets.DOCKER_IO_ACCESS_TOKEN }} # optionally push to the Docker Hub (docker.io)\
```

Your image will be pushed to both ghcr.io and docker.io repositories using the name provided as `image_name`.
47 changes: 40 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
required: true
default: "ghcr.io"

docker_io_token:
description: "Your docker.io token created via https://hub.docker.com/settings/security"
required: false

runs:
using: "composite"
steps:
Expand All @@ -42,7 +46,7 @@ runs:
echo "COMMIT_TAG=latest" >> $GITHUB_ENV
fi

- name: "Build and push the Docker image"
- name: "Build the Docker image"
shell: bash
run: |
# expand commands
Expand All @@ -68,6 +72,7 @@ runs:
--build-arg GITHUB_SHA=${GITHUB_SHA} \
\
--tag ${{ inputs.repository }}/${{ inputs.image_name }}:${{ env.COMMIT_TAG }} \
--tag docker.io/${{ inputs.image_name }}:${{ env.COMMIT_TAG }} \
\
--label org.label-schema.build-date=${BUILD_DATE} \
--label org.label-schema.vcs-url=${GITHUB_URL} \
Expand All @@ -76,16 +81,44 @@ runs:
--label org.opencontainers.image.created=${BUILD_DATE} \
--label org.opencontainers.image.source=${GITHUB_URL} \
--label org.opencontainers.image.revision=${GITHUB_SHA}

echo
echo "Pushing the Docker image ..."
echo

docker push ${{ inputs.repository }}/${{ inputs.image_name }}:${{ env.COMMIT_TAG }}

echo
echo "Inspecting the image ..."
echo

docker images
docker image inspect ${{ inputs.repository }}/${{ inputs.image_name }}:${{ env.COMMIT_TAG }} | jq '.[].Config.Labels'

- name: "Push the Docker image to ghcr.io"
shell: bash
run: |
# expand commands
set -x

echo
echo "Pushing the Docker image to ghcr.io ..."
echo

docker push ${{ inputs.repository }}/${{ inputs.image_name }}:${{ env.COMMIT_TAG }}

- name: "Push the Docker image to docker.io"
shell: bash
env:
DOCKER_TOKEN: ${{ inputs.docker_io_token }}
run: |
if [ -z "${DOCKER_TOKEN}" ]; then
echo
echo "NOT pushing the Docker image to docker.io ... Provide 'docker_io_token' if needed."
echo
else
# expand commands
set -x

echo
echo "Pushing the Docker image to docker.io ..."
echo

echo "${DOCKER_TOKEN}" | docker login docker.io -u "${{ github.actor }}" --password-stdin

docker push docker.io/${{ inputs.image_name }}:${{ env.COMMIT_TAG }}
fi