From be2e50c9ec67fa87b5cfc92de6b365439ef6bae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alo=C3=AFs=20Micard?= Date: Tue, 15 Oct 2019 20:36:26 +0200 Subject: [PATCH 1/9] Allow customization of build docker image tag --- README.md | 5 +++-- entrypoint.sh | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bbfd662..07e5404 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ This Action will automatically tag each image as follows: Where: - `Image_Name` is provided by the user as an input. -- `shortSHA` is the first 12 characters of the GitHub SHA that triggered the action. +- `shortSHA` is either the first 12 characters of the GitHub commit SHA or the value of IMAGE_TAG env variable ## Usage @@ -77,6 +77,7 @@ jobs: ## Optional Inputs 1. `cache`: if value is `true`, attempts to use the last pushed image as a cache. Default value is `false`. +2. `IMAGE_TAG`: if value is set, use provided value. Default value is the first 12 characters of the GitHub SHA that triggered the action. ## Outputs @@ -85,4 +86,4 @@ You can reference the outputs of an action using [expression syntax](https://hel 1. `IMAGE_SHA_NAME`: This is the `{Image_Name}:{shortSHA}` as described above. 2. `IMAGE_URL`: This is the URL on GitHub where you can view your hosted Docker images. This will always be located at `https://github.com/{OWNER}/{REPOSITORY}/packages` in reference to the repository where the action was called. -These outputs are merely provided as convenience incase you want to use these values in subsequent steps. +These outputs are merely provided as convenience incase you want to use these values in subsequent steps. \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 597db58..cf9be72 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -32,6 +32,11 @@ if [[ -z "$INPUT_BUILD_CONTEXT" ]]; then exit 1 fi +if [[ -z "$INPUT_IMAGE_TAG" ]]; then + shortSHA=$(echo "${GITHUB_SHA}" | cut -c1-12) +elif + shortSHA=$INPUT_IMAGE_TAG +fi # The following environment variables will be provided by the environment automatically: GITHUB_REPOSITORY, GITHUB_SHA @@ -39,7 +44,6 @@ fi echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin docker.pkg.github.com # Set Local Variables -shortSHA=$(echo "${GITHUB_SHA}" | cut -c1-12) BASE_NAME="docker.pkg.github.com/${GITHUB_REPOSITORY}/${INPUT_IMAGE_NAME}" SHA_NAME="${BASE_NAME}:${shortSHA}" From f854f5ac4dfa7c59a99823965db2cd304e5da7a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alo=C3=AFs=20Micard?= Date: Tue, 15 Oct 2019 22:11:05 +0200 Subject: [PATCH 2/9] Fix elif -> else --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index cf9be72..57b54ff 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -34,7 +34,7 @@ fi if [[ -z "$INPUT_IMAGE_TAG" ]]; then shortSHA=$(echo "${GITHUB_SHA}" | cut -c1-12) -elif +else shortSHA=$INPUT_IMAGE_TAG fi From 4385a90655749f9fe2818000425f270de0f6d5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alo=C3=AFs=20Micard?= Date: Tue, 15 Oct 2019 22:13:13 +0200 Subject: [PATCH 3/9] Rename shortSHA -> IMAGE_TAG --- README.md | 6 +++--- entrypoint.sh | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 07e5404..7ce0614 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,11 @@ The GitHub Package Registry allows you to develop your code and host your packag This Action will automatically tag each image as follows: - {Image_Name}:{shortSHA} + {Image_Name}:{IMAGE_TAG} Where: - `Image_Name` is provided by the user as an input. -- `shortSHA` is either the first 12 characters of the GitHub commit SHA or the value of IMAGE_TAG env variable +- `IMAGE_TAG` is either the first 12 characters of the GitHub commit SHA or the value of INPUT_IMAGE_TAG env variable ## Usage @@ -83,7 +83,7 @@ jobs: You can reference the outputs of an action using [expression syntax](https://help.github.com/en/articles/contexts-and-expression-syntax-for-github-actions), as illustrated in the Example Pipeline above. -1. `IMAGE_SHA_NAME`: This is the `{Image_Name}:{shortSHA}` as described above. +1. `IMAGE_SHA_NAME`: This is the `{Image_Name}:{IMAGE_TAG}` as described above. 2. `IMAGE_URL`: This is the URL on GitHub where you can view your hosted Docker images. This will always be located at `https://github.com/{OWNER}/{REPOSITORY}/packages` in reference to the repository where the action was called. These outputs are merely provided as convenience incase you want to use these values in subsequent steps. \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 57b54ff..ba6deb7 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -33,9 +33,9 @@ if [[ -z "$INPUT_BUILD_CONTEXT" ]]; then fi if [[ -z "$INPUT_IMAGE_TAG" ]]; then - shortSHA=$(echo "${GITHUB_SHA}" | cut -c1-12) + IMAGE_TAG=$(echo "${GITHUB_SHA}" | cut -c1-12) else - shortSHA=$INPUT_IMAGE_TAG + IMAGE_TAG=$INPUT_IMAGE_TAG fi # The following environment variables will be provided by the environment automatically: GITHUB_REPOSITORY, GITHUB_SHA @@ -45,7 +45,7 @@ echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin dock # Set Local Variables BASE_NAME="docker.pkg.github.com/${GITHUB_REPOSITORY}/${INPUT_IMAGE_NAME}" -SHA_NAME="${BASE_NAME}:${shortSHA}" +SHA_NAME="${BASE_NAME}:${IMAGE_TAG}" # Add Arguments For Caching BUILDPARAMS="" From 135f2136f4098861fbeae9d8dceedd50810efd19 Mon Sep 17 00:00:00 2001 From: Patrick Poetz Date: Thu, 21 Nov 2019 08:45:25 +0100 Subject: [PATCH 4/9] Introduced CI for action testing. Added feature to use Git tags as Docker tags. introduce CI simplified identification, introduced ci and fixed metadata non interactive docker login assuming the right context simplification is difficult simplification is difficult 2 do it the complicated way test image_tag function publish 2nd version echoing tags pointing to current commit build tag string fixed a few typos fixed another typo create additional tags based on GIT Tags pointing to the current commit added feature which adds GIT tags pointing to the current commit as docker tags --- .github/docker/Dockerfile | 3 +++ .github/workflows/main.yml | 22 ++++++++++++++++++++++ .gitignore | 2 ++ action.yaml | 11 +++++++---- entrypoint.sh | 11 ++++++++++- 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 .github/docker/Dockerfile create mode 100644 .github/workflows/main.yml create mode 100644 .gitignore diff --git a/.github/docker/Dockerfile b/.github/docker/Dockerfile new file mode 100644 index 0000000..e2ccf2a --- /dev/null +++ b/.github/docker/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine:3.10.3 + +ENTRYPOINT ["echo", "'hello world'"] \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..d80edaf --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,22 @@ +name: Test Docker Image Creation + +on: [push] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v1 + with: + fetch-depth: 1 + - name: Build and Publish Docker image + uses: saubermacherag/gpr-docker-publish@master + with: + USERNAME: x-access-token + PASSWORD: ${{ secrets.GITHUB_TOKEN }} + IMAGE_NAME: 'gpr-docker-publish-test' + IMAGE_TAG: 'v0.0' + DOCKERFILE_PATH: '.github/docker/Dockerfile' + BUILD_CONTEXT: './' diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62e7820 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Created by .ignore support plugin (hsz.mobi) +.idea \ No newline at end of file diff --git a/action.yaml b/action.yaml index ce38d15..454224b 100644 --- a/action.yaml +++ b/action.yaml @@ -10,19 +10,22 @@ inputs: require: true image_name: description: name of the image. Example - myContainer - require: true + required: true build_context: description: the path in your repo that will serve as the build context - require: true + required: true default: './' dockerfile_path: description: the full path (including the filename) to the dockerfile that you want to build - require: true + required: true default: ./Dockerfile cache: description: attempt to use last image as the cache - require: false + required: false default: false + image_tag: + description: Optional input to set a tag name instead of a hash. + required: false outputs: IMAGE_SHA_NAME: description: name of the Docker Image including the tag diff --git a/entrypoint.sh b/entrypoint.sh index ba6deb7..02128a8 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -47,6 +47,15 @@ echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin dock BASE_NAME="docker.pkg.github.com/${GITHUB_REPOSITORY}/${INPUT_IMAGE_NAME}" SHA_NAME="${BASE_NAME}:${IMAGE_TAG}" +# Build additional tags based on the GIT Tags pointing to the current commit +ADDITIONAL_TAGS= +for git_tag in $(git tag -l --points-at HEAD) +do + echo "Processing ${git_tag}" + ADDITIONAL_TAGS="${ADDITIONAL_TAGS} -t ${BASE_NAME}:${git_tag}" +done +echo "following additional tags will be created: ${ADDITIONAL_TAGS}" + # Add Arguments For Caching BUILDPARAMS="" if [ "${INPUT_CACHE}" == "true" ]; then @@ -58,7 +67,7 @@ if [ "${INPUT_CACHE}" == "true" ]; then fi # Build The Container -docker build $BUILDPARAMS -t ${SHA_NAME} -t ${BASE_NAME} -f ${INPUT_DOCKERFILE_PATH} ${INPUT_BUILD_CONTEXT} +docker build $BUILDPARAMS -t ${SHA_NAME} -t ${BASE_NAME}${ADDITIONAL_TAGS} -f ${INPUT_DOCKERFILE_PATH} ${INPUT_BUILD_CONTEXT} # Push two versions, with and without the SHA docker push ${BASE_NAME} From a23d8916c5f484348c2a96fd94754d06d7794b28 Mon Sep 17 00:00:00 2001 From: Patrick Poetz Date: Thu, 21 Nov 2019 12:26:40 +0100 Subject: [PATCH 5/9] updated README.md --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7ce0614..64f5a49 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ Where: - `Image_Name` is provided by the user as an input. - `IMAGE_TAG` is either the first 12 characters of the GitHub commit SHA or the value of INPUT_IMAGE_TAG env variable +Additionally it will use Git Tags pointing to the HEAD commit to create docker tags accordingly. +E.g. Git Tag v1.1 will result in an additional docker tag v1.1. + ## Usage @@ -52,6 +55,7 @@ jobs: USERNAME: ${{ secrets.DOCKER_USERNAME }} PASSWORD: ${{ secrets.DOCKER_PASSWORD }} IMAGE_NAME: 'test-docker-action' + IMAGE_TAG: 'v0.0' DOCKERFILE_PATH: 'argo/gpu.Dockerfile' BUILD_CONTEXT: 'argo/' @@ -68,8 +72,8 @@ jobs: ### Mandatory Inputs -1. `USERNAME` the login username, most likely your github handle. This username must have write access to the repo where the action is called. -2. `PASSWORD` Your GitHub password that has write access to the repo where this action is called. +1. `USERNAME` the login username, most likely your github handle. This username must have write access to the repo where the action is called. `x-access-token` should suffice. +2. `PASSWORD` Your GitHub password that has write access to the repo where this action is called. `${{ secrets.GITHUB_TOKEN }}` should suffice. 3. `IMAGE_NAME` is the name of the image you would like to push 4. `DOCKERFILE_PATH`: The full path (including the filename) relative to the root of the repository that contains the Dockerfile that specifies your build. 5. `BUILD_CONTEXT`: The directory for the build context. See these [docs](https://docs.docker.com/engine/reference/commandline/build/) for more information on the definition of build context. From 67a677409829ac6630b14b9c328ab1f316816494 Mon Sep 17 00:00:00 2001 From: Patrick Poetz Date: Thu, 21 Nov 2019 15:38:46 +0100 Subject: [PATCH 6/9] alternatively use dockerhub instead of gpr --- .github/workflows/main.yml | 11 ++++++++++- README.md | 2 ++ action.yaml | 3 +++ entrypoint.sh | 20 +++++++++++++++----- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d80edaf..3dd7053 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: uses: actions/checkout@v1 with: fetch-depth: 1 - - name: Build and Publish Docker image + - name: Build and Publish Docker image in GPR uses: saubermacherag/gpr-docker-publish@master with: USERNAME: x-access-token @@ -20,3 +20,12 @@ jobs: IMAGE_TAG: 'v0.0' DOCKERFILE_PATH: '.github/docker/Dockerfile' BUILD_CONTEXT: './' + - name: Build and Publish Docker image in GPR + uses: saubermacherag/gpr-docker-publish@master + with: + USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + PASSWORD: ${{ secrets.DOCKERHUB_PAT }} + IMAGE_TAG: 'v0.0' + DOCKERFILE_PATH: '.github/docker/Dockerfile' + BUILD_CONTEXT: './' + DOCKERHUB_REPOSITORY: 'pinkrobin/docker-ansible-alpine' \ No newline at end of file diff --git a/README.md b/README.md index 64f5a49..f526a52 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ jobs: 1. `cache`: if value is `true`, attempts to use the last pushed image as a cache. Default value is `false`. 2. `IMAGE_TAG`: if value is set, use provided value. Default value is the first 12 characters of the GitHub SHA that triggered the action. +3. `DOCKERHUB_REPOSITORY`: if value is set, you don't need to set `IMAGE_NAME`. It will push the image to the given dockerhub repository instead of using GPR. +Why? Because Github Actions don't support downloading images without authentication at the moment. See: https://github.community/t5/GitHub-Actions/docker-pull-from-public-GitHub-Package-Registry-fail-with-quot/m-p/32782 ## Outputs diff --git a/action.yaml b/action.yaml index 454224b..bd0b051 100644 --- a/action.yaml +++ b/action.yaml @@ -26,6 +26,9 @@ inputs: image_tag: description: Optional input to set a tag name instead of a hash. required: false + dockerhub_repository: + description: Optional input to push image to dockerhub repository instead of GPR. + required: false outputs: IMAGE_SHA_NAME: description: name of the Docker Image including the tag diff --git a/entrypoint.sh b/entrypoint.sh index 02128a8..19a210a 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -17,8 +17,8 @@ if [[ -z "$INPUT_PASSWORD" ]]; then exit 1 fi -if [[ -z "$INPUT_IMAGE_NAME" ]]; then - echo "Set the IMAGE_NAME input." +if [[ -z "$INPUT_IMAGE_NAME" && -z "$INPUT_DOCKERHUB_REPOSITORY" ]]; then + echo "Set either the IMAGE_NAME or a valid DOCKERHUB_REPOSITORY." exit 1 fi @@ -40,11 +40,17 @@ fi # The following environment variables will be provided by the environment automatically: GITHUB_REPOSITORY, GITHUB_SHA +if [[ -z "$INPUT_DOCKERHUB_REPOSITORY" ]]; then + DOCKER_REGISTRY=docker.pkg.github.com + BASE_NAME="${DOCKER_REGISTRY}/${GITHUB_REPOSITORY}/${INPUT_IMAGE_NAME}" +else + BASE_NAME="${INPUT_DOCKERHUB_REPOSITORY}" +fi + # send credentials through stdin (it is more secure) -echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin docker.pkg.github.com +echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${DOCKER_REGISTRY} # Set Local Variables -BASE_NAME="docker.pkg.github.com/${GITHUB_REPOSITORY}/${INPUT_IMAGE_NAME}" SHA_NAME="${BASE_NAME}:${IMAGE_TAG}" # Build additional tags based on the GIT Tags pointing to the current commit @@ -74,4 +80,8 @@ docker push ${BASE_NAME} docker push ${SHA_NAME} echo "::set-output name=IMAGE_SHA_NAME::${SHA_NAME}" -echo "::set-output name=IMAGE_URL::https://github.com/${GITHUB_REPOSITORY}/packages" +if [[ -z "$INPUT_DOCKERHUB_REPOSITORY" ]]; then + echo "::set-output name=IMAGE_URL::https://github.com/${GITHUB_REPOSITORY}/packages" +else + echo "::set-output name=IMAGE_URL::https://hub.docker.com/r/${INPUT_DOCKERHUB_REPOSITORY}" +fi From d2af0b287156e9ed7b904c31508fc9992680042f Mon Sep 17 00:00:00 2001 From: Patrick Poetz Date: Thu, 21 Nov 2019 15:45:14 +0100 Subject: [PATCH 7/9] add documentation --- .github/workflows/main.yml | 4 ++-- README.md | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3dd7053..5247c9b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,7 +20,7 @@ jobs: IMAGE_TAG: 'v0.0' DOCKERFILE_PATH: '.github/docker/Dockerfile' BUILD_CONTEXT: './' - - name: Build and Publish Docker image in GPR + - name: Build and Publish Docker image to Dockerhub instead of GPR uses: saubermacherag/gpr-docker-publish@master with: USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} @@ -28,4 +28,4 @@ jobs: IMAGE_TAG: 'v0.0' DOCKERFILE_PATH: '.github/docker/Dockerfile' BUILD_CONTEXT: './' - DOCKERHUB_REPOSITORY: 'pinkrobin/docker-ansible-alpine' \ No newline at end of file + DOCKERHUB_REPOSITORY: 'pinkrobin/gpr-docker-publish-example' \ No newline at end of file diff --git a/README.md b/README.md index f526a52..2516e3a 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,17 @@ jobs: DOCKERFILE_PATH: 'argo/gpu.Dockerfile' BUILD_CONTEXT: 'argo/' + #To access another docker registry like dockerhub you'll have to add `DOCKERHUB_UERNAME` and `DOCKERHUB_PAT` in github secrets. + - name: Build and Publish Docker image to Dockerhub instead of GPR + uses: saubermacherag/gpr-docker-publish@master + with: + USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + PASSWORD: ${{ secrets.DOCKERHUB_PAT }} + IMAGE_TAG: 'v0.0' + DOCKERFILE_PATH: '.github/docker/Dockerfile' + BUILD_CONTEXT: './' + DOCKERHUB_REPOSITORY: 'pinkrobin/gpr-docker-publish-example' + # This second step is illustrative and shows how to reference the # output variables. This is completely optional. - name: Show outputs of pervious step From d9cbb6d718cb4963d2e4b03fd8bad24871cee895 Mon Sep 17 00:00:00 2001 From: Patrick Poetz Date: Mon, 25 Nov 2019 12:13:30 +0100 Subject: [PATCH 8/9] Images can be pushed to GPR or Dockerhub supply github token for test dockerhub specific settings try to make tests "fork agnostic" try to make tests "fork agnostic". still don't know if this works action names cannot be fork agnostic fixed typo --- .github/workflows/dockerhub.yml | 22 +++++++++++++++++ .github/workflows/{test.yaml => gpr.yaml} | 11 +++++---- .github/workflows/main.yml | 29 ----------------------- README.md | 8 ++++--- action.yml | 3 +++ entrypoint.sh | 21 ++++++++++------ 6 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/dockerhub.yml rename .github/workflows/{test.yaml => gpr.yaml} (61%) delete mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/dockerhub.yml b/.github/workflows/dockerhub.yml new file mode 100644 index 0000000..d384c19 --- /dev/null +++ b/.github/workflows/dockerhub.yml @@ -0,0 +1,22 @@ +name: Test Dockerhub Push +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + steps: + + - name: Copy Repo Files + uses: actions/checkout@v1 + with: + fetch-depth: 1 + - name: Build and Publish Docker image to Dockerhub + uses: saubermacherag/gpr-docker-publish@master + with: + TAG: 'my-optional-tag-name' + DOCKERFILE_PATH: '.github/docker/Dockerfile' + BUILD_CONTEXT: '.' + DOCKERHUB_REPOSITORY: 'pinkrobin/gpr-docker-publish-example' + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + env: + REGISTRY_TOKEN: ${{ secrets.DOCKERHUB_PAT }} \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/gpr.yaml similarity index 61% rename from .github/workflows/test.yaml rename to .github/workflows/gpr.yaml index 2274240..3f8bf0c 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/gpr.yaml @@ -1,4 +1,4 @@ -name: Tests +name: Test GPR Push on: push jobs: @@ -7,14 +7,15 @@ jobs: steps: - name: Copy Repo Files - uses: actions/checkout@master - + uses: actions/checkout@v1 + with: + fetch-depth: 1 - name: Publish Docker Image to GPR - uses: machine-learning-apps/gpr-docker-publish@master + uses: saubermacherag/gpr-docker-publish@master with: IMAGE_NAME: 'test-docker-action-v2' TAG: 'my-optional-tag-name' DOCKERFILE_PATH: 'Dockerfile' BUILD_CONTEXT: '.' env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REGISTRY_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index f840155..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Test Docker Image Creation - -on: [push] - -jobs: - - build: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v1 - with: - fetch-depth: 1 - - name: Build and Publish Docker image in GPR - uses: saubermacherag/gpr-docker-publish@master - with: - IMAGE_NAME: 'gpr-docker-publish-test' - TAG: 'v0.0' - DOCKERFILE_PATH: '.github/docker/Dockerfile' - BUILD_CONTEXT: '.' - - name: Build and Publish Docker image to Dockerhub instead of GPR - uses: saubermacherag/gpr-docker-publish@master - with: - TAG: 'v0.0' - DOCKERFILE_PATH: '.github/docker/Dockerfile' - BUILD_CONTEXT: '.' - DOCKERHUB_REPOSITORY: 'pinkrobin/gpr-docker-publish-example' - env: - GITHUB_TOKEN: ${{ secrets.DOCKERHUB_PAT }} \ No newline at end of file diff --git a/README.md b/README.md index 6b67f21..1bad440 100644 --- a/README.md +++ b/README.md @@ -60,18 +60,19 @@ jobs: DOCKERFILE_PATH: 'argo/gpu.Dockerfile' BUILD_CONTEXT: 'argo/' env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REGISTRY_TOKEN: ${{ secrets.GITHUB_TOKEN }} #To access another docker registry like dockerhub you'll have to add `DOCKERHUB_UERNAME` and `DOCKERHUB_PAT` in github secrets. - name: Build and Publish Docker image to Dockerhub instead of GPR uses: saubermacherag/gpr-docker-publish@master with: - USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - PASSWORD: ${{ secrets.DOCKERHUB_PAT }} IMAGE_TAG: 'v0.0' DOCKERFILE_PATH: '.github/docker/Dockerfile' BUILD_CONTEXT: './' DOCKERHUB_REPOSITORY: 'pinkrobin/gpr-docker-publish-example' + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + env: + REGISTRY_TOKEN: ${{ secrets.DOCKERHUB_PAT }} # This second step is illustrative and shows how to reference the # output variables. This is completely optional. @@ -96,6 +97,7 @@ jobs: 2. `tag`: a custom tag you wish to assign to the image. 3. `DOCKERHUB_REPOSITORY`: if value is set, you don't need to set `IMAGE_NAME`. It will push the image to the given dockerhub repository instead of using GPR. Why? Because Github Actions don't support downloading images without authentication at the moment. See: https://github.community/t5/GitHub-Actions/docker-pull-from-public-GitHub-Package-Registry-fail-with-quot/m-p/32782 +4. `DOCKERHUB_USERNAME`: required when `DOCKERHUB_REPOSITORY` set to true. ## Outputs diff --git a/action.yml b/action.yml index 2a1abbb..a77b716 100644 --- a/action.yml +++ b/action.yml @@ -23,6 +23,9 @@ inputs: dockerhub_repository: description: Optional input to push image to dockerhub repository instead of GPR. required: false + dockerhub_username: + description: Required if dockerhub_repository set to true. + required: false outputs: IMAGE_SHA_NAME: description: name of the Docker Image including the tag diff --git a/entrypoint.sh b/entrypoint.sh index 5361cd5..2cac12d 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -7,8 +7,8 @@ set -e #check inputs -if [[ -z "$GITHUB_TOKEN" ]]; then - echo "You must supply the environment variable GITHUB_TOKEN." +if [[ -z "$REGISTRY_TOKEN" ]]; then + echo "You must supply the environment variable REGISTRY_TOKEN." exit 1 fi @@ -33,15 +33,22 @@ fi if [[ -z "$INPUT_DOCKERHUB_REPOSITORY" ]]; then DOCKER_REGISTRY=docker.pkg.github.com BASE_NAME="${DOCKER_REGISTRY}/${GITHUB_REPOSITORY}/${INPUT_IMAGE_NAME}" + # send credentials through stdin (it is more secure) + user=$(curl -s -H "Authorization: token ${REGISTRY_TOKEN}" https://api.github.com/user | jq -r .login) + # lowercase the username + username="$(echo ${user} | tr "[:upper:]" "[:lower:]")" else + if [ -z "$INPUT_DOCKERHUB_USERNAME" ] + then + echo "If you use Docker Hub as repository please provide your username as DOCKERHUB_USERNAME." + exit 1 + fi + username="${INPUT_DOCKERHUB_USERNAME}" BASE_NAME="${INPUT_DOCKERHUB_REPOSITORY}" fi -# send credentials through stdin (it is more secure) -user=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/user | jq -r .login) -# lowercase the username -username="$(echo ${user} | tr "[:upper:]" "[:lower:]")" -echo ${GITHUB_TOKEN} | docker login -u "${username}" --password-stdin ${DOCKER_REGISTRY} + +echo ${REGISTRY_TOKEN} | docker login -u "${username}" --password-stdin ${DOCKER_REGISTRY} # Set Local Variables shortSHA=$(echo "${GITHUB_SHA}" | cut -c1-12) From ed381301db93a2e8f6c72e30d17a02413bdf4fda Mon Sep 17 00:00:00 2001 From: Patrick Poetz Date: Mon, 25 Nov 2019 13:49:54 +0100 Subject: [PATCH 9/9] use path argument to be fork agnostic --- .github/workflows/dockerhub.yml | 2 +- .github/workflows/gpr.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dockerhub.yml b/.github/workflows/dockerhub.yml index d384c19..58263e8 100644 --- a/.github/workflows/dockerhub.yml +++ b/.github/workflows/dockerhub.yml @@ -11,7 +11,7 @@ jobs: with: fetch-depth: 1 - name: Build and Publish Docker image to Dockerhub - uses: saubermacherag/gpr-docker-publish@master + uses: ./ with: TAG: 'my-optional-tag-name' DOCKERFILE_PATH: '.github/docker/Dockerfile' diff --git a/.github/workflows/gpr.yaml b/.github/workflows/gpr.yaml index 3f8bf0c..57acb9a 100644 --- a/.github/workflows/gpr.yaml +++ b/.github/workflows/gpr.yaml @@ -11,7 +11,7 @@ jobs: with: fetch-depth: 1 - name: Publish Docker Image to GPR - uses: saubermacherag/gpr-docker-publish@master + uses: ./ with: IMAGE_NAME: 'test-docker-action-v2' TAG: 'my-optional-tag-name'