Skip to content

Commit

Permalink
Update hook with master and branch tags
Browse files Browse the repository at this point in the history
  • Loading branch information
blai-seek committed Feb 25, 2019
1 parent b268268 commit d27a432
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
60 changes: 35 additions & 25 deletions hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
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}"

0 comments on commit d27a432

Please sign in to comment.