diff --git a/README.md b/README.md index 1e20729..ecc681f 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,17 @@ with: snapshot: true ``` +### default_branch +Use `default_branch` when you want to use a different branch than `master` as the default branch. + +```yaml +with: + name: owner/repository/image + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + default_branch: trunk +``` + ### dockerfile Use `dockerfile` when you would like to explicitly build a Dockerfile. This might be useful when you have multiple DockerImages. diff --git a/action.yml b/action.yml index 47c1566..290d2ee 100644 --- a/action.yml +++ b/action.yml @@ -20,6 +20,9 @@ inputs: snapshot: description: 'Use snapshot to push an additional image' required: false + default_branch: + description: 'Set the default branch of your repository (default: master)' + required: false dockerfile: description: 'Use dockerfile when you would like to explicitly build a Dockerfile' required: false diff --git a/entrypoint.sh b/entrypoint.sh index f8ec655..02b783a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -95,7 +95,7 @@ translateDockerTag() { if hasCustomTag; then TAGS=$(echo "${INPUT_NAME}" | cut -d':' -f2) INPUT_NAME=$(echo "${INPUT_NAME}" | cut -d':' -f1) - elif isOnMaster; then + elif isOnDefaultBranch; then TAGS="latest" elif isGitTag && usesBoolean "${INPUT_TAG_SEMVER}" && isSemver "${GITHUB_REF}"; then TAGS=$(echo "${GITHUB_REF}" | sed -e "s/refs\/tags\///g" | sed -E "s/v?([0-9]+)\.([0-9]+)\.([0-9]+)(-[a-zA-Z]+(\.[0-9]+)?)?/\1.\2.\3\4 \1.\2\4 \1\4/g") @@ -114,8 +114,12 @@ hasCustomTag() { [ $(echo "${INPUT_NAME}" | sed -e "s/://g") != "${INPUT_NAME}" ] } -isOnMaster() { - [ "${BRANCH}" = "master" ] +isOnDefaultBranch() { + local DEFAULT_BRANCH="master" + if uses "${INPUT_DEFAULT_BRANCH}"; then + DEFAULT_BRANCH="${INPUT_DEFAULT_BRANCH}" + fi + [ "${BRANCH}" = "${DEFAULT_BRANCH}" ] } isGitTag() { diff --git a/test.bats b/test.bats index 00fcd96..c312d72 100755 --- a/test.bats +++ b/test.bats @@ -614,6 +614,21 @@ teardown() { /usr/local/bin/docker logout" } +@test "it can change the default branch" { + export GITHUB_REF='refs/heads/trunk' + export INPUT_DEFAULT_BRANCH='trunk' + + run /entrypoint.sh + + expectStdOutContains "::set-output name=tag::latest" + + expectMockCalledContains "/usr/local/bin/docker login -u USERNAME --password-stdin +/usr/local/bin/docker build -t my/repository:latest . +/usr/local/bin/docker push my/repository:latest +/usr/local/bin/docker inspect --format={{index .RepoDigests 0}} my/repository:latest +/usr/local/bin/docker logout" +} + expectStdOutIs() { local expected=$(echo "${1}" | tr -d '\n') local got=$(echo "${output}" | tr -d '\n')