From 505b28d9411b53efd4f0e9cc1ba9e28531ae238f Mon Sep 17 00:00:00 2001 From: Lars Gohr Date: Thu, 22 Aug 2019 22:07:56 +0200 Subject: [PATCH] Add docker login and logout, due to the data is not passed from the previous step. Remove old workflow, as it's not backward compatible. Use name instead of args for the image name --- .github/workflows/test_integration.yml | 8 +-- README.md | 66 ++++++++------------ entrypoint.sh | 8 ++- test.sh | 86 ++++++++++++++++++++------ 4 files changed, 101 insertions(+), 67 deletions(-) diff --git a/.github/workflows/test_integration.yml b/.github/workflows/test_integration.yml index cd6d79c..0745b69 100644 --- a/.github/workflows/test_integration.yml +++ b/.github/workflows/test_integration.yml @@ -5,12 +5,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@master - - name: Login to Registry - run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} - name: Publish to Registry uses: elgohr/Publish-Docker-Github-Action@master with: - args: lgohr/publish-docker-github-action + name: lgohr/publish-docker-github-action + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} snapshot: true - - name: Logout - run: docker logout diff --git a/README.md b/README.md index 9b2367b..d24cfcf 100644 --- a/README.md +++ b/README.md @@ -15,24 +15,40 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@master - - name: Login to Registry - run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} - name: Publish to Registry uses: elgohr/Publish-Docker-Github-Action@master with: - args: myDocker/repository - - name: Logout - run: docker logout + name: myDocker/repository + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} ``` +## Mandatory Arguments + +`name` is the name of the image you would like to push +`username` the login username for the registry +`password` the login password for the registry + #### Optional Arguments +Use `registry` for pushing to a custom registry + +```yaml +with: + name: myDocker/repository + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + registry: "docker.pkg.github.com" +``` + Use `snapshot` to push an additional image, which is tagged with the git sha. When you would like to think about versioning images, this might be useful. ```yaml with: - args: myDocker/repository + name: myDocker/repository + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} snapshot: true ``` @@ -41,40 +57,8 @@ This might be useful when you have multiple DockerImages. ```yaml with: - args: myDocker/repository + name: myDocker/repository + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} dockerfile: MyDockerFileName ``` - - -### Old workflow -```hcl -workflow "Publish Docker" { - on = "push" - resolves = ["logout"] -} - -action "login" { - uses = "actions/docker/login@master" - secrets = ["DOCKER_PASSWORD", "DOCKER_USERNAME"] -} - -action "publish" { - uses = "elgohr/Publish-Docker-Github-Action@master" - args = "myDocker/repository" - needs = ["login"] -} - -action "logout" { - uses = "actions/docker/cli@master" - args = "logout" - needs = ["publish"] -} -``` - -## Docker builds - -Please see https://github.com/elgohr/Publish-Docker-Github-Action/packages - -## Argument - -You need to provide the desired docker repository to the action. diff --git a/entrypoint.sh b/entrypoint.sh index 710d4ea..d4930fc 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,5 @@ #!/bin/sh -DOCKER_REPOSITORY=$* BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g") if [ "${BRANCH}" == "master" ]; then @@ -12,16 +11,17 @@ if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then BRANCH="latest" fi; -DOCKERNAME="${DOCKER_REPOSITORY}:${BRANCH}" +DOCKERNAME="${INPUT_NAME}:${BRANCH}" CUSTOMDOCKERFILE="" if [ ! -z "${INPUT_DOCKERFILE}" ]; then CUSTOMDOCKERFILE="-f ${INPUT_DOCKERFILE}" fi +docker login -u ${INPUT_USERNAME} -p ${INPUT_PASSWORD} ${INPUT_REGISTRY} if [ "${INPUT_SNAPSHOT}" == "true" ]; then - SHA_DOCKER_NAME="${DOCKER_REPOSITORY}:${GITHUB_SHA}" + SHA_DOCKER_NAME="${INPUT_NAME}:${GITHUB_SHA}" docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} . docker push ${DOCKERNAME} docker push ${SHA_DOCKER_NAME} @@ -29,3 +29,5 @@ else docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} . docker push ${DOCKERNAME} fi + +docker logout diff --git a/test.sh b/test.sh index d1fd90f..07d59d3 100755 --- a/test.sh +++ b/test.sh @@ -8,9 +8,14 @@ function cleanEnvironment() { function itPushesMasterBranchToLatest() { export GITHUB_REF='refs/heads/master' - local result=$(exec /entrypoint.sh 'my/repository') - local expected="Called mock with: build -t my/repository:latest . -Called mock with: push my/repository:latest" + export INPUT_USERNAME='USERNAME' + export INPUT_PASSWORD='PASSWORD' + export INPUT_NAME='my/repository' + local result=$(exec /entrypoint.sh) + local expected="Called mock with: login -u USERNAME -p PASSWORD +Called mock with: build -t my/repository:latest . +Called mock with: push my/repository:latest +Called mock with: logout" if [ "$result" != "$expected" ]; then echo "Expected: $expected Got: $result" @@ -20,9 +25,14 @@ Called mock with: push my/repository:latest" function itPushesBranchAsNameOfTheBranch() { export GITHUB_REF='refs/heads/myBranch' - local result=$(exec /entrypoint.sh 'my/repository') - local expected="Called mock with: build -t my/repository:myBranch . -Called mock with: push my/repository:myBranch" + export INPUT_USERNAME='USERNAME' + export INPUT_PASSWORD='PASSWORD' + export INPUT_NAME='my/repository' + local result=$(exec /entrypoint.sh) + local expected="Called mock with: login -u USERNAME -p PASSWORD +Called mock with: build -t my/repository:myBranch . +Called mock with: push my/repository:myBranch +Called mock with: logout" if [ "$result" != "$expected" ]; then echo "Expected: $expected Got: $result" @@ -32,9 +42,14 @@ Called mock with: push my/repository:myBranch" function itPushesReleasesToLatest() { export GITHUB_REF='refs/tags/myRelease' - local result=$(exec /entrypoint.sh 'my/repository') - local expected="Called mock with: build -t my/repository:latest . -Called mock with: push my/repository:latest" + export INPUT_USERNAME='USERNAME' + export INPUT_PASSWORD='PASSWORD' + export INPUT_NAME='my/repository' + local result=$(exec /entrypoint.sh) + local expected="Called mock with: login -u USERNAME -p PASSWORD +Called mock with: build -t my/repository:latest . +Called mock with: push my/repository:latest +Called mock with: logout" if [ "$result" != "$expected" ]; then echo "Expected: $expected Got: $result" @@ -45,9 +60,14 @@ Called mock with: push my/repository:latest" function itPushesSpecificDockerfileReleasesToLatest() { export GITHUB_REF='refs/tags/myRelease' export INPUT_DOCKERFILE='MyDockerFileName' - local result=$(exec /entrypoint.sh 'my/repository') - local expected="Called mock with: build -f MyDockerFileName -t my/repository:latest . -Called mock with: push my/repository:latest" + export INPUT_USERNAME='USERNAME' + export INPUT_PASSWORD='PASSWORD' + export INPUT_NAME='my/repository' + local result=$(exec /entrypoint.sh) + local expected="Called mock with: login -u USERNAME -p PASSWORD +Called mock with: build -f MyDockerFileName -t my/repository:latest . +Called mock with: push my/repository:latest +Called mock with: logout" if [ "$result" != "$expected" ]; then echo "Expected: $expected Got: $result" @@ -60,10 +80,15 @@ function itPushesBranchByShaInAddition() { export GITHUB_REF='refs/tags/myRelease' export INPUT_SNAPSHOT='true' export GITHUB_SHA='COMMIT_SHA' - local result=$(exec /entrypoint.sh 'my/repository') - local expected="Called mock with: build -t my/repository:latest -t my/repository:COMMIT_SHA . + export INPUT_USERNAME='USERNAME' + export INPUT_PASSWORD='PASSWORD' + export INPUT_NAME='my/repository' + local result=$(exec /entrypoint.sh) + local expected="Called mock with: login -u USERNAME -p PASSWORD +Called mock with: build -t my/repository:latest -t my/repository:COMMIT_SHA . Called mock with: push my/repository:latest -Called mock with: push my/repository:COMMIT_SHA" +Called mock with: push my/repository:COMMIT_SHA +Called mock with: logout" if [ "$result" != "$expected" ]; then echo "Expected: $expected Got: $result" @@ -77,10 +102,34 @@ function itPushesBranchByShaInAdditionWithSpecificDockerfile() { export INPUT_SNAPSHOT='true' export INPUT_DOCKERFILE='MyDockerFileName' export GITHUB_SHA='COMMIT_SHA' - local result=$(exec /entrypoint.sh 'my/repository') - local expected="Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/repository:COMMIT_SHA . + export INPUT_USERNAME='USERNAME' + export INPUT_PASSWORD='PASSWORD' + export INPUT_NAME='my/repository' + local result=$(exec /entrypoint.sh) + local expected="Called mock with: login -u USERNAME -p PASSWORD +Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/repository:COMMIT_SHA . Called mock with: push my/repository:latest -Called mock with: push my/repository:COMMIT_SHA" +Called mock with: push my/repository:COMMIT_SHA +Called mock with: logout" + if [ "$result" != "$expected" ]; then + echo "Expected: $expected + Got: $result" + exit 1 + fi + cleanEnvironment +} + +function itLogsIntoAnotherRegistryIfConfigured() { + export GITHUB_REF='refs/tags/myRelease' + export INPUT_USERNAME='USERNAME' + export INPUT_PASSWORD='PASSWORD' + export INPUT_REGISTRY='https://myRegistry' + export INPUT_NAME='my/repository' + local result=$(exec /entrypoint.sh) + local expected="Called mock with: login -u USERNAME -p PASSWORD https://myRegistry +Called mock with: build -t my/repository:latest . +Called mock with: push my/repository:latest +Called mock with: logout" if [ "$result" != "$expected" ]; then echo "Expected: $expected Got: $result" @@ -95,3 +144,4 @@ itPushesReleasesToLatest itPushesSpecificDockerfileReleasesToLatest itPushesBranchByShaInAddition itPushesBranchByShaInAdditionWithSpecificDockerfile +itLogsIntoAnotherRegistryIfConfigured