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

This commit is contained in:
Lars Gohr
2019-08-22 22:07:56 +02:00
parent 964b205576
commit 505b28d941
4 changed files with 101 additions and 67 deletions

View File

@@ -5,12 +5,10 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@master - uses: actions/checkout@master
- name: Login to Registry
run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
- name: Publish to Registry - name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@master uses: elgohr/Publish-Docker-Github-Action@master
with: with:
args: lgohr/publish-docker-github-action name: lgohr/publish-docker-github-action
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
snapshot: true snapshot: true
- name: Logout
run: docker logout

View File

@@ -15,24 +15,40 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@master - uses: actions/checkout@master
- name: Login to Registry
run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
- name: Publish to Registry - name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@master uses: elgohr/Publish-Docker-Github-Action@master
with: with:
args: myDocker/repository name: myDocker/repository
- name: Logout username: ${{ secrets.DOCKER_USERNAME }}
run: docker logout 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 #### 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. 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. When you would like to think about versioning images, this might be useful.
```yaml ```yaml
with: with:
args: myDocker/repository name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
snapshot: true snapshot: true
``` ```
@@ -41,40 +57,8 @@ This might be useful when you have multiple DockerImages.
```yaml ```yaml
with: with:
args: myDocker/repository name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
dockerfile: MyDockerFileName 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.

View File

@@ -1,6 +1,5 @@
#!/bin/sh #!/bin/sh
DOCKER_REPOSITORY=$*
BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g") BRANCH=$(echo ${GITHUB_REF} | sed -e "s/refs\/heads\///g")
if [ "${BRANCH}" == "master" ]; then if [ "${BRANCH}" == "master" ]; then
@@ -12,16 +11,17 @@ if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then
BRANCH="latest" BRANCH="latest"
fi; fi;
DOCKERNAME="${DOCKER_REPOSITORY}:${BRANCH}" DOCKERNAME="${INPUT_NAME}:${BRANCH}"
CUSTOMDOCKERFILE="" CUSTOMDOCKERFILE=""
if [ ! -z "${INPUT_DOCKERFILE}" ]; then if [ ! -z "${INPUT_DOCKERFILE}" ]; then
CUSTOMDOCKERFILE="-f ${INPUT_DOCKERFILE}" CUSTOMDOCKERFILE="-f ${INPUT_DOCKERFILE}"
fi fi
docker login -u ${INPUT_USERNAME} -p ${INPUT_PASSWORD} ${INPUT_REGISTRY}
if [ "${INPUT_SNAPSHOT}" == "true" ]; then 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 build $CUSTOMDOCKERFILE -t ${DOCKERNAME} -t ${SHA_DOCKER_NAME} .
docker push ${DOCKERNAME} docker push ${DOCKERNAME}
docker push ${SHA_DOCKER_NAME} docker push ${SHA_DOCKER_NAME}
@@ -29,3 +29,5 @@ else
docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} . docker build $CUSTOMDOCKERFILE -t ${DOCKERNAME} .
docker push ${DOCKERNAME} docker push ${DOCKERNAME}
fi fi
docker logout

86
test.sh
View File

@@ -8,9 +8,14 @@ function cleanEnvironment() {
function itPushesMasterBranchToLatest() { function itPushesMasterBranchToLatest() {
export GITHUB_REF='refs/heads/master' export GITHUB_REF='refs/heads/master'
local result=$(exec /entrypoint.sh 'my/repository') export INPUT_USERNAME='USERNAME'
local expected="Called mock with: build -t my/repository:latest . export INPUT_PASSWORD='PASSWORD'
Called mock with: push my/repository:latest" 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 if [ "$result" != "$expected" ]; then
echo "Expected: $expected echo "Expected: $expected
Got: $result" Got: $result"
@@ -20,9 +25,14 @@ Called mock with: push my/repository:latest"
function itPushesBranchAsNameOfTheBranch() { function itPushesBranchAsNameOfTheBranch() {
export GITHUB_REF='refs/heads/myBranch' export GITHUB_REF='refs/heads/myBranch'
local result=$(exec /entrypoint.sh 'my/repository') export INPUT_USERNAME='USERNAME'
local expected="Called mock with: build -t my/repository:myBranch . export INPUT_PASSWORD='PASSWORD'
Called mock with: push my/repository:myBranch" 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 if [ "$result" != "$expected" ]; then
echo "Expected: $expected echo "Expected: $expected
Got: $result" Got: $result"
@@ -32,9 +42,14 @@ Called mock with: push my/repository:myBranch"
function itPushesReleasesToLatest() { function itPushesReleasesToLatest() {
export GITHUB_REF='refs/tags/myRelease' export GITHUB_REF='refs/tags/myRelease'
local result=$(exec /entrypoint.sh 'my/repository') export INPUT_USERNAME='USERNAME'
local expected="Called mock with: build -t my/repository:latest . export INPUT_PASSWORD='PASSWORD'
Called mock with: push my/repository:latest" 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 if [ "$result" != "$expected" ]; then
echo "Expected: $expected echo "Expected: $expected
Got: $result" Got: $result"
@@ -45,9 +60,14 @@ Called mock with: push my/repository:latest"
function itPushesSpecificDockerfileReleasesToLatest() { function itPushesSpecificDockerfileReleasesToLatest() {
export GITHUB_REF='refs/tags/myRelease' export GITHUB_REF='refs/tags/myRelease'
export INPUT_DOCKERFILE='MyDockerFileName' export INPUT_DOCKERFILE='MyDockerFileName'
local result=$(exec /entrypoint.sh 'my/repository') export INPUT_USERNAME='USERNAME'
local expected="Called mock with: build -f MyDockerFileName -t my/repository:latest . export INPUT_PASSWORD='PASSWORD'
Called mock with: push my/repository:latest" 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 if [ "$result" != "$expected" ]; then
echo "Expected: $expected echo "Expected: $expected
Got: $result" Got: $result"
@@ -60,10 +80,15 @@ function itPushesBranchByShaInAddition() {
export GITHUB_REF='refs/tags/myRelease' export GITHUB_REF='refs/tags/myRelease'
export INPUT_SNAPSHOT='true' export INPUT_SNAPSHOT='true'
export GITHUB_SHA='COMMIT_SHA' export GITHUB_SHA='COMMIT_SHA'
local result=$(exec /entrypoint.sh 'my/repository') export INPUT_USERNAME='USERNAME'
local expected="Called mock with: build -t my/repository:latest -t my/repository:COMMIT_SHA . 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: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 if [ "$result" != "$expected" ]; then
echo "Expected: $expected echo "Expected: $expected
Got: $result" Got: $result"
@@ -77,10 +102,34 @@ function itPushesBranchByShaInAdditionWithSpecificDockerfile() {
export INPUT_SNAPSHOT='true' export INPUT_SNAPSHOT='true'
export INPUT_DOCKERFILE='MyDockerFileName' export INPUT_DOCKERFILE='MyDockerFileName'
export GITHUB_SHA='COMMIT_SHA' export GITHUB_SHA='COMMIT_SHA'
local result=$(exec /entrypoint.sh 'my/repository') export INPUT_USERNAME='USERNAME'
local expected="Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/repository:COMMIT_SHA . 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: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 if [ "$result" != "$expected" ]; then
echo "Expected: $expected echo "Expected: $expected
Got: $result" Got: $result"
@@ -95,3 +144,4 @@ itPushesReleasesToLatest
itPushesSpecificDockerfileReleasesToLatest itPushesSpecificDockerfileReleasesToLatest
itPushesBranchByShaInAddition itPushesBranchByShaInAddition
itPushesBranchByShaInAdditionWithSpecificDockerfile itPushesBranchByShaInAdditionWithSpecificDockerfile
itLogsIntoAnotherRegistryIfConfigured