Add option for building pull requests

This commit is contained in:
Lars Gohr
2019-09-07 21:02:59 +02:00
parent 91a3945f7b
commit a48ee1845e
3 changed files with 65 additions and 1 deletions

View File

@@ -80,3 +80,16 @@ with:
password: ${{ secrets.DOCKER_PASSWORD }} password: ${{ secrets.DOCKER_PASSWORD }}
cache: true 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
```

View File

@@ -27,6 +27,14 @@ if [ $(echo ${GITHUB_REF} | sed -e "s/refs\/tags\///g") != ${GITHUB_REF} ]; then
BRANCH="latest" BRANCH="latest"
fi; 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} echo ${INPUT_PASSWORD} | docker login -u ${INPUT_USERNAME} --password-stdin ${INPUT_REGISTRY}
DOCKERNAME="${INPUT_NAME}:${BRANCH}" DOCKERNAME="${INPUT_NAME}:${BRANCH}"

View File

@@ -13,6 +13,7 @@ teardown() {
unset INPUT_REGISTRY unset INPUT_REGISTRY
unset INPUT_CACHE unset INPUT_CACHE
unset GITHUB_SHA unset GITHUB_SHA
unset INPUT_PULL_REQUESTS
} }
@test "it pushes master branch to latest" { @test "it pushes master branch to latest" {
@@ -24,6 +25,7 @@ teardown() {
Called mock with: build -t my/repository:latest . Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest Called mock with: push my/repository:latest
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -36,6 +38,7 @@ Called mock with: logout"
Called mock with: build -t my/repository:myBranch . Called mock with: build -t my/repository:myBranch .
Called mock with: push my/repository:myBranch Called mock with: push my/repository:myBranch
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -48,18 +51,20 @@ Called mock with: logout"
Called mock with: build -t my/repository:latest . Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest Called mock with: push my/repository:latest
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@test "it pushes specific Dockerfile to latest" { @test "it pushes specific Dockerfile to latest" {
export INPUT_DOCKERFILE='MyDockerFileName' 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 local expected="Called mock with: login -u USERNAME --password-stdin
Called mock with: build -f MyDockerFileName -t my/repository:latest . Called mock with: build -f MyDockerFileName -t my/repository:latest .
Called mock with: push my/repository:latest Called mock with: push my/repository:latest
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -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:latest
Called mock with: push my/repository:19700101010112169e Called mock with: push my/repository:19700101010112169e
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -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:latest
Called mock with: push my/repository:19700101010112169e Called mock with: push my/repository:19700101010112169e
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -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:latest
Called mock with: push my/repository:19700101010112169e Called mock with: push my/repository:19700101010112169e
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -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:latest
Called mock with: push my/repository:19700101010112169e Called mock with: push my/repository:19700101010112169e
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -138,6 +147,7 @@ Called mock with: logout"
Called mock with: build -t my/repository:latest . Called mock with: build -t my/repository:latest .
Called mock with: push my/repository:latest Called mock with: push my/repository:latest
Called mock with: logout" Called mock with: logout"
echo $output
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -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: build --cache-from my/repository:latest -t my/repository:latest .
Called mock with: push my/repository:latest Called mock with: push my/repository:latest
Called mock with: logout" 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" ] [ "$output" = "$expected" ]
} }
@@ -160,6 +197,8 @@ Called mock with: logout"
run /entrypoint.sh run /entrypoint.sh
local expected="Unable to find the repository name. Did you set with.name?" local expected="Unable to find the repository name. Did you set with.name?"
echo $output
[ "$status" -eq 1 ]
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -169,6 +208,8 @@ Called mock with: logout"
run /entrypoint.sh run /entrypoint.sh
local expected="Unable to find the username. Did you set with.username?" local expected="Unable to find the username. Did you set with.username?"
echo $output
[ "$status" -eq 1 ]
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }
@@ -178,5 +219,7 @@ Called mock with: logout"
run /entrypoint.sh run /entrypoint.sh
local expected="Unable to find the password. Did you set with.password?" local expected="Unable to find the password. Did you set with.password?"
echo $output
[ "$status" -eq 1 ]
[ "$output" = "$expected" ] [ "$output" = "$expected" ]
} }