Skip to content

Commit

Permalink
Add docker login and logout, due to the data is not passed from the p…
Browse files Browse the repository at this point in the history
…revious step. Remove old workflow, as it's not backward compatible. Use name instead of args for the image name
  • Loading branch information
Lars Gohr committed Aug 22, 2019
1 parent 964b205 commit 505b28d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 67 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/test_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 25 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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.
8 changes: 5 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/sh

DOCKER_REPOSITORY=$*
BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g")

if [ "${BRANCH}" == "master" ]; then
Expand All @@ -12,20 +11,23 @@ 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}
else
docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} .
docker push ${DOCKERNAME}
fi

docker logout
86 changes: 68 additions & 18 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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: 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: push my/repository:COMMIT_SHA"
Called mock with: logout"
if [ "$result" != "$expected" ]; then
echo "Expected: $expected
Got: $result"
Expand All @@ -95,3 +144,4 @@ itPushesReleasesToLatest
itPushesSpecificDockerfileReleasesToLatest
itPushesBranchByShaInAddition
itPushesBranchByShaInAdditionWithSpecificDockerfile
itLogsIntoAnotherRegistryIfConfigured

0 comments on commit 505b28d

Please sign in to comment.