├── Dockerfile ├── LICENSE ├── README.md └── entrypoint.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM docker:stable 2 | 3 | LABEL "name"="Github-Action-One-Click-Docker" 4 | LABEL "maintainer"="Peter Pang " 5 | LABEL "version"="1.0.0" 6 | 7 | LABEL "com.github.actions.icon"="package" 8 | LABEL "com.github.actions.color"="blue" 9 | LABEL "com.github.actions.name"="One Click Docker" 10 | LABEL "com.github.actions.description"="Execute the steps of docker login, build, tag and push in one action" 11 | COPY LICENSE README.md / 12 | 13 | COPY entrypoint.sh /entrypoint.sh 14 | 15 | ENTRYPOINT ["/entrypoint.sh"] 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2019 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Github Action One Click Docker 2 | 3 | There are official Github Actions for Docker related tasks: https://github.com/actions/docker. However it's too slow to have multiple actions that pull & run multiple docker images for those actions one by one. Especially when you are doing simple "build & push" most of the time. 4 | 5 | This Github Action does a fixed list of actions: 6 | 7 | 1. (optional) filter the branch for action 8 | 2. login to your Docker Registry 9 | 3. build your docker image 10 | 4. tag the image with `latest`, `sha` of the commit, `ref` of the commit, or your own tag 11 | 5. push all tags of the image to your registry 12 | 13 | Such that you don't have to run 5 different actions, which require the same environment setup (basically Docker) to be repeated again and again. 14 | 15 | 16 | ## Usage 17 | 18 | **Arguments** passed to the action will be passed to the docker build process. If the argument is left empty, then the docker build process run with the default mode 19 | 20 | Below are the required Secrets / Env: 21 | 22 | - `DOCKER_USERNAME` - the username used to log in to your Docker registry. 23 | - `DOCKER_PASSWORD` - the password used to log in to your Docker registry. 24 | 25 | Below are the optional Secrets / Env: 26 | 27 | - `BRANCH_FILTER` - the branch to allow the action, default to allow all branches 28 | - `DOCKER_REGISTRY_URL` - the registry to login & push image to, default to be Docker Hub 29 | - `DOCKER_NAMESPACE` - the namespace on the registry, default to be your Github username 30 | - `DOCKER_IMAGE_NAME` - the name of the docker image to be built, default to be the namge of the Github repository 31 | - `DOCKER_IMAGE_TAG` - the tag of the docker image to be pushed, default to be 3 tags (`latest`, `sha` of the commit, `ref` of the commit) 32 | 33 | Below are some additional optional env: 34 | 35 | - `DOCKER_TAG_APPEND` - if this value is set, the default `ref` tag will be appended with this value (e.g. `DOCKER_TAG_APPEND=asia-business` for a git master branch push will produce this docker image tag: `master_asia-business`) 36 | 37 | 38 | ## Example 39 | 40 | - simple use case: build image with default tags and push to Docker Hub, where your account name is the same as Github 41 | 42 | ``` 43 | action "build-and-push-to-docker-hub" { 44 | uses = "pangzineng/Github-Action-One-Click-Docker@master" 45 | secrets = [ 46 | "DOCKER_USERNAME", 47 | "DOCKER_PASSWORD" 48 | ] 49 | } 50 | ``` 51 | 52 | - advance use case: custom build path, custom tag and custom private registry with custom namespace 53 | 54 | ``` 55 | action "build-and-push-to-my-registry" { 56 | uses = "pangzineng/Github-Action-One-Click-Docker@master" 57 | args = "-f my.Dockerfile ./my/custom/folder/" 58 | secrets = [ 59 | "DOCKER_USERNAME", 60 | "DOCKER_PASSWORD", 61 | "DOCKER_REGISTRY_URL", 62 | "DOCKER_NAMESPACE" 63 | ] 64 | env = { 65 | BRANCH_FILTER = "prod/release" 66 | DOCKER_IMAGE_NAME = "my-image-name" 67 | DOCKER_IMAGE_TAG = "my-image-tag" 68 | } 69 | } 70 | ``` 71 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | ## mapping of var from user input or default value 4 | 5 | USERNAME=${GITHUB_REPOSITORY%%/*} 6 | REPOSITORY=${GITHUB_REPOSITORY#*/} 7 | 8 | ref_tmp=${GITHUB_REF#*/} ## throw away the first part of the ref (GITHUB_REF=refs/heads/master or refs/tags/2019/03/13) 9 | ref_type=${ref_tmp%%/*} ## extract the second element of the ref (heads or tags) 10 | ref_value=${ref_tmp#*/} ## extract the third+ elements of the ref (master or 2019/03/13) 11 | 12 | ## if needed to filter the branch 13 | if [ -n "${BRANCH_FILTER+set}" ] && [ "$ref_value" != "$BRANCH_FILTER" ] 14 | then 15 | exit 78 ## exit neutral 16 | fi 17 | 18 | GIT_TAG=${ref_value//\//-} ## replace `/` with `-` in ref for docker tag requirement (master or 2019-03-13) 19 | if [ -n "${DOCKER_TAG_APPEND+set}" ] ## right append to tag if specified 20 | then 21 | GIT_TAG=${GIT_TAG}_${DOCKER_TAG_APPEND} 22 | fi 23 | 24 | REGISTRY=${DOCKER_REGISTRY_URL} ## use default Docker Hub as registry unless specified 25 | NAMESPACE=${DOCKER_NAMESPACE:-$USERNAME} ## use github username as docker namespace unless specified 26 | IMAGE_NAME=${DOCKER_IMAGE_NAME:-$REPOSITORY} ## use github repository name as docker image name unless specified 27 | IMAGE_TAG=${DOCKER_IMAGE_TAG:-$GIT_TAG} ## use git ref value as docker image tag unless specified 28 | 29 | ## login if needed 30 | if [ -n "${DOCKER_PASSWORD+set}" ] 31 | then 32 | sh -c "docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $REGISTRY" 33 | fi 34 | 35 | ## build the image locally 36 | sh -c "docker build -t $IMAGE_NAME ${*:-.}" ## pass in the build command from user input, otherwise build in default mode 37 | 38 | ## tag the image with registry and versions 39 | if [ -n "${REGISTRY}" ] 40 | then 41 | REGISTRY_IMAGE="$REGISTRY/$NAMESPACE/$IMAGE_NAME" 42 | else 43 | REGISTRY_IMAGE="$NAMESPACE/$IMAGE_NAME" 44 | fi 45 | # push all the tags to registry 46 | if [ -n "${DOCKER_IMAGE_TAG+set}" ] 47 | then 48 | sh -c "docker tag $IMAGE_NAME $REGISTRY_IMAGE:$DOCKER_IMAGE_TAG" 49 | sh -c "docker push $REGISTRY_IMAGE:$DOCKER_IMAGE_TAG" 50 | else 51 | sh -c "docker tag $IMAGE_NAME $REGISTRY_IMAGE:$IMAGE_TAG" 52 | sh -c "docker tag $IMAGE_NAME $REGISTRY_IMAGE:${GITHUB_SHA:0:7}" 53 | sh -c "docker tag $IMAGE_NAME $REGISTRY_IMAGE:latest" 54 | sh -c "docker push $REGISTRY_IMAGE:$IMAGE_TAG" 55 | sh -c "docker push $REGISTRY_IMAGE:${GITHUB_SHA:0:7}" 56 | sh -c "docker push $REGISTRY_IMAGE:latest" 57 | fi 58 | --------------------------------------------------------------------------------