diff --git a/README.md b/README.md index 6d30245..3af2e9e 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ or - `on:push`: Push only if the workflow was triggered by a push. - `on:pull_request`: Push only if the workflow was triggered by a pull_request. +- **services_regex**: Regex to filter services from compose file. Only valid when **compose_file** was provided. Default is `.+` (all services). + [branch tip]: https://stackoverflow.com/questions/16080342/what-is-a-branch-tip-in-git #### Ignored if `compose_file` is set @@ -182,6 +184,18 @@ With a compose file override: compose_file: docker-compose.yml > docker-compose.override.yml ``` +Filtering services by regex: + +```yml +- uses: whoan/docker-build-with-cache-action@v5 + with: + username: whoan + password: "${{ secrets.GITHUB_TOKEN }}" + registry: docker.pkg.github.com + compose_file: docker-compose.yml + 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 +``` + ### Example with more options ```yml diff --git a/action.yml b/action.yml index 601ce65..5636329 100644 --- a/action.yml +++ b/action.yml @@ -10,6 +10,10 @@ inputs: compose_file: description: Path to compose file required: false + services_regex: + description: (Optional) Regex to filter services from compose file + required: false + default: '.+' username: description: (Optional) Docker's registry user (needed to push image to repository, or to pull from private repository) required: false diff --git a/entrypoint.sh b/entrypoint.sh index da2017f..4539bb7 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -71,8 +71,15 @@ _gather_images() { local registry=$INPUT_REGISTRY : "${registry:=$INPUT_USERNAME}" # an empty registry defaults to DockerHub, and a username is needed to detect its images : "${registry:?Either registry or username (for DockerHub) is needed to build from a compose file}" + images=() - mapfile -t images < <(_yq e ".services.[].image | select(. != null and test(\"^${registry}/\"))" "$merged_compose") + mapfile -t images < <(_yq e " + .services + | with_entries(select(.key | test(\"^${INPUT_SERVICES_REGEX:-.+}\$\"))) + | .[].image + | select(. != null and test(\"^${registry}/\")) + " "$merged_compose" + ) } _set_variables() {