diff --git a/README.md b/README.md index 5b756e2..6170a9e 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ A [Buildkite plugin](https://buildkite.com/docs/agent/v3/plugins) to build, tag, cache and push entire docker images to ECR. +Allows for additional tags if required for master(default-tags) and branch(branch-tags) builds + # Example ## Basic Usage @@ -19,8 +21,10 @@ steps: seek-oss/docker-ecr-publish#v1.0.2: dockerfile: Dockerfile ecr-name: insert-ecr-name - additional-tags: - - branch-1.1.0 + default-tags: + - master-1.1.0 + branch-tags: + - branch-1.1.0 ``` # License diff --git a/hooks/pre-command b/hooks/pre-command index 3040293..8376013 100755 --- a/hooks/pre-command +++ b/hooks/pre-command @@ -2,6 +2,20 @@ set -euo pipefail +compute_tag() { + local docker_file="$1" + local sums=($(sha1sum "${docker_file}")) + echo "${sums}" | sha1sum | cut -c-7 +} + +get_ecr_url() { + local repository_name="$1" + aws ecr describe-repositories \ + --repository-names "${repository_name}" \ + --output text \ + --query 'repositories[0].repositoryUri' +} + plugin_read_list_into_result() { local prefix="$1" local parameter="${prefix}_0" @@ -22,37 +36,33 @@ plugin_read_list_into_result() { [[ ${#result[@]} -gt 0 ]] || return 1 } -get_ecr_url() { - local repository_name="$1" - aws ecr describe-repositories \ - --repository-names "${repository_name}" \ - --output text \ - --query 'repositories[0].repositoryUri' -} - -compute_tag() { - local docker_file="$1" - local sums=($(sha1sum "${docker_file}")) - echo "${sums}" | sha1sum | cut -c-7 +push_additional_tags() { + local tags_parameter="$1" + local current_image="$2" + + if plugin_read_list_into_result ${tags_parameter} ; then + for tag in "${result[@]}" ; do + docker tag ${current_image} "${image_name}:$tag" + docker push "${image_name}:$tag" + done + fi } $(aws ecr get-login --no-include-email) docker_file="${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_DOCKERFILE:-Dockerfile}" image_name="$(get_ecr_url ${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_ECR_NAME})" -image_tag="$(compute_tag ${docker_file})" +image_tag="$(compute_tag ${docker_file})" if ! docker pull "${image_name}:${image_tag}"; then echo "Image not cached, building" - docker build . --file "${docker_file}" -t "${image_name}" || exit 1 - if [ "$BUILDKITE_BRANCH" == "$BUILDKITE_PIPELINE_DEFAULT_BRANCH" ]; then - docker push "${image_name}:latest" - fi + docker build . --file "${docker_file}" -t "${image_name}:${image_tag}"|| exit 1 +fi || echo "Not found" - # Parse plugin command if provided - if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_ADDITIONAL_TAGS ; then - for tag in "${result[@]}" ; do - docker tag "${image_name}" "${image_name}:$tag" - docker push "${image_name}:$tag" - done - fi -fi || echo "Not found" \ No newline at end of file +if [ "$BUILDKITE_BRANCH" == "$BUILDKITE_PIPELINE_DEFAULT_BRANCH" ]; then + push_additional_tags BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_DEFAULT_TAGS "${image_name}" + docker push "${image_name}:latest" +else + push_additional_tags BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_BRANCH_TAGS "${image_name}:${image_tag}" +fi + +docker push "${image_name}:${image_tag}" \ No newline at end of file