{"id":13484868,"url":"https://github.com/whoan/docker-build-with-cache-action","last_synced_at":"2025-05-16T18:10:21.682Z","repository":{"id":54177259,"uuid":"210221075","full_name":"whoan/docker-build-with-cache-action","owner":"whoan","description":":octocat: Build and push docker images caching each stage to reduce build time","archived":false,"fork":false,"pushed_at":"2025-04-26T21:53:36.000Z","size":162,"stargazers_count":345,"open_issues_count":6,"forks_count":57,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-07T01:01:54.288Z","etag":null,"topics":["actions","docker","docker-cache","docker-image","docker-registry","github-actions","github-actions-docker","hacktoberfest"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/whoan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"open_collective":"whoan","patreon":"whoan","ko_fi":"whoan","liberapay":"whoan","custom":"https://blockchair.com/bitcoin/address/1Gn3NmQKPXuDpjpSs8wmrcH9VoYGFToNE5"}},"created_at":"2019-09-22T22:11:50.000Z","updated_at":"2025-04-26T21:53:33.000Z","dependencies_parsed_at":"2024-10-25T10:18:45.240Z","dependency_job_id":"6ce7fbf9-b775-4f91-b871-f6f8f6e81013","html_url":"https://github.com/whoan/docker-build-with-cache-action","commit_stats":{"total_commits":201,"total_committers":12,"mean_commits":16.75,"dds":"0.11940298507462688","last_synced_commit":"272668ad2d5539177fb43c97b5e6a7df446933c4"},"previous_names":[],"tags_count":63,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoan%2Fdocker-build-with-cache-action","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoan%2Fdocker-build-with-cache-action/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoan%2Fdocker-build-with-cache-action/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whoan%2Fdocker-build-with-cache-action/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whoan","download_url":"https://codeload.github.com/whoan/docker-build-with-cache-action/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254582907,"owners_count":22095518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actions","docker","docker-cache","docker-image","docker-registry","github-actions","github-actions-docker","hacktoberfest"],"created_at":"2024-07-31T17:01:37.022Z","updated_at":"2025-05-16T18:10:21.662Z","avatar_url":"https://github.com/whoan.png","language":"Shell","funding_links":["https://opencollective.com/whoan","https://patreon.com/whoan","https://ko-fi.com/whoan","https://liberapay.com/whoan","https://blockchair.com/bitcoin/address/1Gn3NmQKPXuDpjpSs8wmrcH9VoYGFToNE5"],"categories":["Shell","Community Resources"],"sub_categories":["Deployment"],"readme":"![](https://github.com/whoan/hello-world/workflows/docker-hub/badge.svg)\n![](https://github.com/whoan/hello-world/workflows/git-hub/badge.svg)\n![](https://github.com/whoan/hello-world/workflows/aws-ecr/badge.svg)\n![](https://github.com/whoan/hello-world/workflows/docker-compose/badge.svg)\n\n# Docker build-with-cache action\n\nThis action builds your docker image and caches the stages (supports multi-stage builds) to improve building times in subsequent builds.\n\nBy default, it pushes the image with all the stages to a registry (needs username and password), but you can disable this feature by setting `push_image_and_stages` to `false`.\n\nBuilt-in support for the most known registries:\n\n- Docker Hub\n- AWS ECR (private and public)\n- GitHub's (old and new registry)\n- Google Cloud's (currently not under test)\n\n## :star2: Action supercharged\n\n- Docker updated to 24.0.6\n- BuildKit is enabled for faster/parallel builds\n- Cache also works with BuildKit enabled except for **old** GitHub Docker Registry (docker.pkg.github.com). You can either migrate to ghcr.io or disable BuildKit to use the old registry:\n\n      - name: Build with DOCKER_BUILDKIT disabled for old GitHub Docker Registry\n        uses: whoan/docker-build-with-cache-action@master\n        env:\n          DOCKER_BUILDKIT: 0\n        with:\n          registry: docker.pkg.github.com\n        ...\n\n## Inputs\n\n### Required\n\n- **image_name**: Image name (e.g. *node*).\n\nor\n\n- **compose_file**: path to Docker Compose file. You will need to configure this action multiple times if you have a compose file which uses more than one registry.\n\n\u003e :star2: New in v5.10.0: Now you can use [overrides](https://docs.docker.com/compose/extends/#multiple-compose-files) for your compose file(s) like this:  \n  `docker-compose.yml \u003e docker-compose.override.yml \u003e docker-compose.override2.yml`\n\n### Optional\n\n- **image_tag**: Tag(s) of the image. Allows multiple comma-separated tags (e.g. `one,another`) (default: `latest`).  \n  If you set **compose_file** and the image(s) already has/have a tag, this is ignored.\n\n- **context**: Docker context (default: `./`). If a **compose_file** is provided, it will be the context prefixed to any additional context read from the compose file. Look at #133 for more details.\n\n- **registry**: Docker registry (default: *Docker Hub's registry*). You need a registry to use the cache functionality.\n\n- **username**: Docker registry's user (needed to push images, or to pull from a private repository).\n\n- **password**: Docker registry's password (needed to push images, or to pull from a private repository).\n\n- **session**: Extra auth parameters. For AWS ECR, means setting AWS_SESSION_TOKEN environment variable.\n\n- **push_git_tag**: In addition to `image_tag`, you can also push the git tag in your [branch tip][branch tip] (default: `false`).\n\n- **pull_image_and_stages**: Set to `false` to avoid pulling from the registry or to build from scratch (default: `true`).\n\n- **stages_image_name**: Set custom name for stages/cache image (default: `${image_name}-stages`). Tags are ignored.\n\n- **push_image_and_stages**: Test a command before pushing. Use `false` to not push at all (default: `true`).\n\n    This input also supports 2 special values, which are useful if your workflow can be triggered by different events:\n\n    - `on:push`: Push only if the workflow was triggered by a push.\n    - `on:pull_request`: Push only if the workflow was triggered by a pull_request.\n\n- **services_regex**: Regex to filter services from compose file. Only valid when **compose_file** was provided. Default is `.+` (all services).\n\n[branch tip]: https://stackoverflow.com/questions/16080342/what-is-a-branch-tip-in-git\n\n#### Ignored if `compose_file` is set\n\n- **image_name**\n\n- **dockerfile**: Dockerfile filename path (default: `\"$context\"/Dockerfile`).\n\n- **build_extra_args**: Extra params for `docker build` (e.g. `\"--build-arg=hello=world\"`).  \n  \u003e :star2: New in v5.11.0: If you need extra args with newlines or spaces, use json format like this:  \n    `build_extra_args: '{\"--build-arg\": \"myarg=Hello\\nWorld\"}'`\n\n  \u003e :star2: If you need multiple args with same key, use an array as the value of the key like this:  \n    `build_extra_args: '{\"--build-arg\": [\"foo=bar\", \"one=two\"]}'`\n\n## Outputs\n\n- **FULL_IMAGE_NAME**: Full name of the Docker Image with the Registry (if provided) and Namespace included.  \ne.g.: `docker.pkg.github.com/whoan/hello-world/hello-world`\n\n## How it works\n\nThe action does the following every time it is triggered:\n\n- (Optional) Pull previously pushed [stages](https://docs.docker.com/develop/develop-images/multistage-build/) (if any) from the specified `registry` (default: https://hub.docker.com)\n- Build the image using cache (i.e. the pulled stages)\n- Tag the image\n- (Optional) Push the image with the tag(s) specified in `image_tag`\n- (Optional) Push each stage to the registry with names like `\u003cimage_name\u003e-stages:\u003c1,2,3,...\u003e`\n- (Optional) Push the git tag as `\u003cimage_name\u003e:\u003cgit_tag\u003e` if you set `push_git_tag: true`\n\n## Examples\n\nFind working minimal examples for the most known registries in [this repo](https://github.com/whoan/hello-world/tree/master/.github/workflows).\n\n### Docker Hub\n\n\u003e If you don't specify a registry, Docker Hub is the default one\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: whoan\n    password: \"${{ secrets.DOCKER_HUB_PASSWORD }}\"\n    image_name: hello-world\n```\n\n### GitHub Registry\n\n\u003e [GitHub automatically creates a GITHUB_TOKEN secret to use in your workflow](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret). If you are going to use the new GitHub Registry (ghcr.io), be sure to use a Personal Access Token (as the password) with \"write:packages\" and \"read:packages\" scopes. More info [here](https://docs.github.com/en/packages/getting-started-with-github-container-registry/migrating-to-github-container-registry-for-docker-images#migrating-a-docker-image-using-the-docker-cli).\n\n\u003e If you push the image to a **public** repository's GitHub Registry, please be aware that it will be impossible to delete it because of GitHub's policy (see [Deleting a package](https://help.github.com/en/packages/publishing-and-managing-packages/deleting-a-package)).\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: whoan\n    password: \"${{ secrets.GITHUB_TOKEN }}\"\n    registry: docker.pkg.github.com\n    #or\n    #registry: ghcr.io\n    image_name: hello-world\n```\n\n### Google Cloud Registry\n\n\u003e More info [here](https://cloud.google.com/container-registry/docs/advanced-authentication#json-key) on how to get GCloud JSON key.\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: _json_key\n    password: \"${{ secrets.GCLOUD_JSON_KEY }}\"\n    registry: gcr.io\n    image_name: hello-world\n```\n\n### AWS ECR\n\n\u003e You don't even need to create the repositories in advance, as this action takes care of that for you! (you'll need the `CreateRepository` permission)\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: \"${{ secrets.AWS_ACCESS_KEY_ID }}\"  # no need to provide it if you already logged in with aws-actions/configure-aws-credentials\n    password: \"${{ secrets.AWS_SECRET_ACCESS_KEY }}\"  # no need to provide it if you already logged in with aws-actions/configure-aws-credentials\n    session:  \"${{ secrets.AWS_SESSION_TOKEN }}\"  # if you need role assumption. no need to provide it if you already logged in with aws-actions/configure-aws-credentials\n    # private registry\n    registry: 861729690598.dkr.ecr.us-west-1.amazonaws.com\n    # or public registry\n    #registry: public.ecr.aws\n    image_name: hello-world\n```\n\n### From a compose file\n\n\u003e The compose file is parsed and the action will run once for each detected image. The registry is also detected from the image name, and if none is provided, DockerHub is assumed.\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: whoan\n    password: \"${{ secrets.DOCKER_HUB_PASSWORD }}\"\n    compose_file: docker-compose.yml\n```\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: whoan\n    password: \"${{ secrets.GITHUB_TOKEN }}\"\n    registry: docker.pkg.github.com\n    compose_file: docker-compose.yml\n```\n\nWith a compose file override:\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: whoan\n    password: \"${{ secrets.DOCKER_HUB_PASSWORD }}\"\n    compose_file: docker-compose.yml \u003e docker-compose.override.yml\n```\n\nFiltering services by regex:\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: whoan\n    password: \"${{ secrets.GITHUB_TOKEN }}\"\n    registry: docker.pkg.github.com\n    compose_file: docker-compose.yml\n    services_regex: '(service_1|extra_service.*)' # eg: builds services called exactly \"service_1\" plus the ones which start with \"extra_service\" and may have extra chars after\n```\n\n### Example with more options\n\n```yml\n- uses: whoan/docker-build-with-cache-action@v5\n  with:\n    username: whoan\n    password: \"${{ secrets.GITHUB_TOKEN }}\"\n    image_name: whoan/docker-images/node\n    image_tag: alpine-slim,another-tag,latest\n    push_git_tag: true\n    registry: docker.pkg.github.com\n    context: node-alpine-slim\n    dockerfile: custom.Dockerfile\n    build_extra_args: \"--compress=true --build-arg=hello=world\"\n    push_image_and_stages: docker run my_awesome_image:latest  # eg: push only if docker run succeed\n```\n\n## Cache is not working?\n\n- Be specific with the base images. e.g.: if you start from an image with the `latest` tag, it may download different versions when the action is triggered, and it will invalidate the cache.\n- If you are using Buildkit, the stages won't be pushed to the registry. This might be supported in a future version.\n- Some docker limitations might cause the cache not to be used correctly. More information [in this SO answer](https://stackoverflow.com/questions/54574821/docker-build-not-using-cache-when-copying-gemfile-while-using-cache-from/56024061#56024061).\n\n## Tests\n\nThe tests for this action are run in a [separate repo](https://github.com/whoan/hello-world) as I need to set credentials for each registry with GitHub secrets and doing so in this repo is not practical.\n\n## License\n\n[MIT](https://github.com/whoan/docker-build-with-cache-action/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhoan%2Fdocker-build-with-cache-action","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhoan%2Fdocker-build-with-cache-action","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhoan%2Fdocker-build-with-cache-action/lists"}