Skip to content

Commit

Permalink
Add option for building pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Gohr committed Sep 7, 2019
1 parent 91a3945 commit a48ee18
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ with:
password: ${{ secrets.DOCKER_PASSWORD }}
cache: true
```

### pull_requests
Use `pull_requests` if you would like to publish pull requests.
The images will be published under 'pr{NUMBER_OF_PR}{ACTION_IN_REF}'
> CAUTION: Only activate this option, when you are really sure, that nobody will be able to do something bad in your name within the PR.

```yaml
with:
name: myDocker/repository
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
pull_requests: true
```
8 changes: 8 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then
BRANCH="latest"
fi;

if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/pull\///g") != ${GITHUB_REF} ]; then
if [ -z "${INPUT_PULL_REQUESTS}" ]; then
echo "The build was triggered within a pull request, but was not configured to build pull requests. Please see with.pull_requests"
exit 1
fi
BRANCH="pr$(echo ${GITHUB_REF} | sed -e "s/refs\/pull\///g" | sed -e "s/\///g")"
fi;

echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY}

DOCKERNAME="${INPUT_NAME}:${BRANCH}"
Expand Down
45 changes: 44 additions & 1 deletion test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ teardown() {
unset INPUT_REGISTRY
unset INPUT_CACHE
unset GITHUB_SHA
unset INPUT_PULL_REQUESTS
}

@test "it pushes master branch to latest" {
Expand All @@ -24,6 +25,7 @@ teardown() {
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

Expand All @@ -36,6 +38,7 @@ Called mock with: logout"
Called mock with: build -t my/repository:myBranch .
Called mock with: push my/repository:myBranch
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

Expand All @@ -48,18 +51,20 @@ Called mock with: logout"
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

@test "it pushes specific Dockerfile to latest" {
export INPUT_DOCKERFILE='MyDockerFileName'

run /entrypoint.sh
run /entrypoint.sh export GITHUB_REF='refs/heads/master'

local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -f MyDockerFileName -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

Expand All @@ -75,6 +80,7 @@ Called mock with: build -t my/repository:latest -t my/repository:197001010101121
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

Expand All @@ -92,6 +98,7 @@ Called mock with: build --cache-from my/repository:latest -t my/repository:lates
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

Expand All @@ -108,6 +115,7 @@ Called mock with: build -f MyDockerFileName -t my/repository:latest -t my/reposi
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

Expand All @@ -126,6 +134,7 @@ Called mock with: build -f MyDockerFileName --cache-from my/repository:latest -t
Called mock with: push my/repository:latest
Called mock with: push my/repository:19700101010112169e
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

Expand All @@ -138,6 +147,7 @@ Called mock with: logout"
Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

Expand All @@ -151,6 +161,33 @@ Called mock with: pull my/repository:latest
Called mock with: build --cache-from my/repository:latest -t my/repository:latest .
Called mock with: push my/repository:latest
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

@test "it pushes pull requests when configured" {
export GITHUB_REF='refs/pull/24/merge'
export INPUT_PULL_REQUESTS='true'

run /entrypoint.sh

local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -t my/repository:pr24merge .
Called mock with: push my/repository:pr24merge
Called mock with: logout"
echo $output
[ "$output" = "$expected" ]
}

@test "it errors on pull requests when not configured" {
export GITHUB_REF='refs/pull/24/merge'
unset INPUT_PULL_REQUESTS

run /entrypoint.sh

local expected="The build was triggered within a pull request, but was not configured to build pull requests. Please see with.pull_requests"
echo $output
[ "$status" -eq 1 ]
[ "$output" = "$expected" ]
}

Expand All @@ -160,6 +197,8 @@ Called mock with: logout"
run /entrypoint.sh

local expected="Unable to find the repository name. Did you set with.name?"
echo $output
[ "$status" -eq 1 ]
[ "$output" = "$expected" ]
}

Expand All @@ -169,6 +208,8 @@ Called mock with: logout"
run /entrypoint.sh

local expected="Unable to find the username. Did you set with.username?"
echo $output
[ "$status" -eq 1 ]
[ "$output" = "$expected" ]
}

Expand All @@ -178,5 +219,7 @@ Called mock with: logout"
run /entrypoint.sh

local expected="Unable to find the password. Did you set with.password?"
echo $output
[ "$status" -eq 1 ]
[ "$output" = "$expected" ]
}

0 comments on commit a48ee18

Please sign in to comment.