./local_build_push_notify.sh [options] IMAGE_NAME[:TAG]
The token to pass into the authorization header. Will override the
HUMANITEC_TOKEN environment variable if supplied.
The organization in Humanitec that the token belongs to.
IMAGE_NAME is required and must consist only of lowercase letters and numbers
and -. It may not start or end with -.
If TAG is not provided, it will be set to the current commit SHA1.
By default, the token will be read from the HUMANITEC_TOKEN environment
./local_build_push_notify.sh --org my-org my-image:0.3.2-rc5
tr -d '\n' | sed 's/^[ \t\v\f]*{.*"'"${1}"'"[ \t\v\f]*:[ \t\v\f]*"\([^"]*\)"[ \t\v\f]*[,}].*$/\1/'
if [ "$method" = "POST" ] || [ "$method" = "PUT" ] || [ "$method" = "PATCH" ]
auth_header="Authorization: Bearer $HUMANITEC_TOKEN"
if command -v curl &> /dev/null
-H "Content-Type: application/json" \
elif command -v wget &> /dev/null
--header="$auth_header" \
--header="Content-Type: application/json" \
--header="$auth_header" \
echo "System does not have the commands wget or curl installed." >&2
api_prefix="https://api.humanitec.io"
export HUMANITEC_TOKEN="$2"
export HUMANITEC_ORG="$2"
if ! echo "$1" | grep -E '^[a-z0-9][a-z0-9-]+[a-z0-9]:[a-zA-Z0-9_.-]+$|^[a-z0-9][a-z0-9-]+[a-z0-9]#x27; > /dev/null
image_name=${image_with_tag%:*}
image_tag=${image_with_tag#*:}
if [ "$image_tag" = "$image_with_tag" ]
if [ -z "$HUMANITEC_TOKEN" ]
echo "No token specified as option or via HUMANITEC_TOKEN environment variable." >&2
if [ -z "$HUMANITEC_ORG" ]
echo "No Organization specified as option or via HUMANITEC_ORG environment variable." >&2
if [ -z "$image_with_tag" ]
echo "No IMAGE_NAME:TAG provided." >&2
echo "Retrieving registry credentials"
registry_json="$(fetch_url GET "${api_prefix}/orgs/${HUMANITEC_ORG}/registries/humanitec/creds")"
echo "Unable to retrieve credentials for humanitec registry." >&2
username="$(echo "$registry_json" | key_from_json_obj "username")"
password="$(echo "$registry_json" | key_from_json_obj "password")"
server="$(echo "$registry_json" | key_from_json_obj "registry")"
branch="$(git rev-parse --abbrev-ref HEAD)"
commit="$(git rev-parse HEAD)"
echo "Logging into docker registry"
echo "${password}" | docker login -u "${username}" --password-stdin "${server}"
echo "Unable to log into humanitec registry." >&2
echo "Performing docker build"
local_tag="${HUMANITEC_ORG}/${image_name}:${image_tag}"
if ! docker build -t "$local_tag" .
echo "Docker build failed." >&2
image_id="$(docker images -q "$local_tag")"
remote_tag="${server}/$local_tag"
if ! docker tag "$local_tag" "$remote_tag"
echo "Error pushing to remote registry: Cannot retag locally." >&2
echo "Pushing image to registry: $remote_tag"
if ! docker push "$remote_tag"
echo "Error pushing to remote registry: Push failed." >&2
echo "Notifying Humanitec"
payload="{\"commit\":\"${commit}\",\"branch\":\"${branch}\",\"tags\":"[]",\"image\":\"${remote_tag}\"}"
if ! fetch_url POST "$payload" "${api_prefix}/orgs/${HUMANITEC_ORG}/images/${image_name}/builds"
echo "Unable to notify Humanitec." >&2