Skip to content

Commit

Permalink
👷 Publish to dockerhub from GitHub Actions
Browse files Browse the repository at this point in the history
Signed-off-by: mathieu.brunot <mathieu.brunot@monogramm.io>
  • Loading branch information
madmath03 committed Aug 30, 2021
1 parent dacc80e commit 042f81a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
26 changes: 24 additions & 2 deletions .github/workflows/hooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
version: ['1.6', '1.7', 'develop', 'main', ]
variant: ['alpine', 'mainline']
Expand All @@ -21,11 +22,32 @@ jobs:
- name: Check Out Repo
uses: actions/checkout@v2

- name: Execute CI hooks
- name: Execute CI build hooks
id: docker_build
run: |
cd ./images/${{ matrix.version }}/${{ matrix.variant }}/
./hooks/run
./hooks/run build
- name: Display docker images
run: docker images

- name: Execute CI test hooks
id: docker_test
run: |
cd ./images/${{ matrix.version }}/${{ matrix.variant }}/
./hooks/run test
- name: Execute CI push hooks
id: docker_push
if: ${{ github.event_name == 'push' && ( github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' ) }}
run: |
cd ./images/${{ matrix.version }}/${{ matrix.variant }}/
# Export variables to login and push to GitHub Container Registry
#export DOCKER_REGISTRY=ghcr.io
#export DOCKER_LOGIN=${{ github.repository_owner }}
#export DOCKER_PASSWORD=${{ secrets.GITHUB_REGISTRY_TOKEN }}
#./hooks/run push
# Export variables to login and push to DockerHub Registry
export DOCKER_LOGIN=${{ secrets.DOCKERHUB_REGISTRY_LOGIN }}
export DOCKER_PASSWORD=${{ secrets.DOCKERHUB_REGISTRY_TOKEN }}
./hooks/run push
24 changes: 22 additions & 2 deletions template/hooks/post_push
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,31 @@
# See documentation for details:
# https://docs.docker.com/docker-hub/builds/advanced/

if [ -n "${DOCKER_REGISTRY}" ]; then
export DOCKER_BASE_IMAGE=${DOCKER_REGISTRY}/${DOCKER_REPO}
else
export DOCKER_BASE_IMAGE=${DOCKER_REPO}
fi

if [ -f ./.dockertags ]; then
for tag in $(cat ./.dockertags); do
if [ ! "${tag}" = "${DOCKER_TAG}" ]; then
docker tag "$IMAGE_NAME" "$DOCKER_REPO:$tag"
docker push "$DOCKER_REPO:$tag"
docker tag "$IMAGE_NAME" "$DOCKER_BASE_IMAGE:$tag"
docker push "$DOCKER_BASE_IMAGE:$tag"
fi
done
fi

# Push image with tag set to commit hash
docker tag "$IMAGE_NAME" "$DOCKER_BASE_IMAGE:$SOURCE_COMMIT"
docker push "$DOCKER_BASE_IMAGE:$SOURCE_COMMIT"

if [ -n "${DOCKER_PASSWORD}" ] && [ -n "${DOCKER_LOGIN}" ]; then
if [ -n "${DOCKER_REGISTRY}" ]; then
echo "Logout of Docker Registry ${DOCKER_REGISTRY}..."
docker logout "${DOCKER_REGISTRY}"
else
echo "Logout of Docker default Registry..."
docker logout
fi
fi
14 changes: 14 additions & 0 deletions template/hooks/pre_push
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# See documentation for details:
# https://docs.docker.com/docker-hub/builds/advanced/

if [ -n "${DOCKER_PASSWORD}" ] && [ -n "${DOCKER_LOGIN}" ]; then
if [ -n "${DOCKER_REGISTRY}" ]; then
echo "Login to Docker Registry ${DOCKER_REGISTRY}..."
docker login -u "${DOCKER_LOGIN}" -p "${DOCKER_PASSWORD}" "${DOCKER_REGISTRY}"
else
echo "Login to Docker default Registry..."
docker login -u "${DOCKER_LOGIN}" -p "${DOCKER_PASSWORD}"
fi
fi
6 changes: 6 additions & 0 deletions template/hooks/push
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# See documentation for details:
# https://docs.docker.com/docker-hub/builds/advanced/

docker push "$IMAGE_NAME"
7 changes: 6 additions & 1 deletion template/hooks/run
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ COMMIT_MSG=$(git log -1 --format=%s)
export DOCKER_REPO=
export DOCKERFILE_PATH=Dockerfile
export DOCKER_TAG=
export IMAGE_NAME=${DOCKER_REPO}:${DOCKER_TAG}

if [ -n "${DOCKER_REGISTRY}" ]; then
export IMAGE_NAME=${DOCKER_REGISTRY}/${DOCKER_REPO}:${DOCKER_TAG}
else
export IMAGE_NAME=${DOCKER_REPO}:${DOCKER_TAG}
fi

# Execute hooks in given order
IFS=',' read -ra STEPS <<< "${1:-build,test}"
Expand Down

0 comments on commit 042f81a

Please sign in to comment.