Skip to content

Commit

Permalink
Merge pull request #20 from docksal/develop
Browse files Browse the repository at this point in the history
Release 1.3.0
  • Loading branch information
lmakarov authored Feb 12, 2022
2 parents 99c3644 + 7c66f62 commit cd6666d
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 232 deletions.
45 changes: 45 additions & 0 deletions .github/scripts/docker-tag-delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

# Deletes an image tag from Docker Hub
#
# Expects USER, PASSWORD
# Expects IMAGE:TAG as argument.
#
# Example: docker-tag-delete.sh docksal/cli:php7.3-build-01c92a2-amd64

# Credit:
# https://devopsheaven.com/docker/dockerhub/2018/04/09/delete-docker-image-tag-dockerhub.html

set -euo pipefail

# Get IMAGE and TAG from first argument
if [[ "${1}" == "" ]]; then
echo "Usage: ${0} image:tag"
exit 1
else
# Split image:tag
IFS=$':' read IMAGE TAG <<< ${1};
# Remove registry prefix from image if present
IMAGE=${IMAGE#"docker.io/"}
fi

login_data() {
cat <<EOF
{
"username": "${DOCKERHUB_USERNAME}",
"password": "${DOCKERHUB_PASSWORD}"
}
EOF
}

# Get auth token
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d "$(login_data)" "https://hub.docker.com/v2/users/login/" | jq -r .token)

# Delete tag
output=$(curl -sI "https://hub.docker.com/v2/repositories/${IMAGE}/tags/${TAG}/" \
-H "Authorization: JWT ${TOKEN}" \
-X DELETE
)

# Return and error if HTTP response code is not 204
echo "${output}" | grep "HTTP/1.1 204 NO CONTENT"
77 changes: 50 additions & 27 deletions .github/scripts/docker-tags.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
#!/usr/bin/env bash

# Generates docker images tags for the docker/build-push-action@v2 action depending on the branch/tag.
# Image tag format:
# develop => image:[version_prefix][version-]edge[-version_suffix]
# master => image:[version_prefix][version][-][version_suffix]
# semver tag => image:[version_prefix][version-]major.minor[-version_suffix]

declare -a registryArr
registryArr+=("docker.io") # Docker Hub
registryArr+=("ghcr.io") # GitHub Container Registry
# Declare expected variables
IMAGE=${IMAGE} # docksal/cli
VERSION_PREFIX=${VERSION_PREFIX} # php
VERSION=${VERSION} # 7.4
VERSION_SUFFIX=${VERSION_SUFFIX} # ide
REGISTRY="${REGISTRY}" # ghcr.io
GITHUB_REF=${GITHUB_REF} # refs/heads/develop, refs/heads/master, refs/tags/v1.0.0

# Join arguments with hyphen (-) as a delimiter
# Usage: join <arg1> [<argn>]
join() {
local IFS='-' # join delimiter
echo "$*"
}

# Prints resulting image tags and sets output variable
set_output() {
local -n inputArr=${1}

declare -a outputArr
for imageTag in ${inputArr[@]}; do
# Prepend registry to imageTag if provided
[[ "${REGISTRY}" != "" ]] && imageTag="${REGISTRY}/${imageTag}"
outputArr+=("${imageTag}")
done

# Print with new lines for output in build logs
(IFS=$'\n'; echo "${outputArr[*]}")
# Using newlines in output variables does not seem to work, so we'll use comas
(IFS=$','; echo "::set-output name=tags::${outputArr[*]}")
}

# Image tags
declare -a imageTagArr

# feature/* => sha-xxxxxxx
# Note: disabled
#if [[ "${GITHUB_REF}" =~ "refs/heads/feature/" ]]; then
# GIT_SHA7=$(echo ${GITHUB_SHA} | cut -c1-7) # Short SHA (7 characters)
# imageTagArr+=("${IMAGE}:sha-${GIT_SHA7}-${VERSION}")
# imageTagArr+=("ghcr.io/${IMAGE}:sha-${GIT_SHA7}-${VERSION}")
#fi
## On every build => build / build-sha7
## Latest build tag (used with cache-from)
#imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} ${VERSION_SUFFIX} build)")
## Specific build tag - SHA7 (first 7 characters of commit SHA)
#imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} ${VERSION_SUFFIX} build ${GITHUB_SHA:0:7})")

# develop => version-edge
if [[ "${GITHUB_REF}" == "refs/heads/develop" ]]; then
imageTagArr+=("${IMAGE}:${VERSION}-edge")
imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} edge ${VERSION_SUFFIX})")
fi

# master => version
if [[ "${GITHUB_REF}" == "refs/heads/master" ]]; then
imageTagArr+=("${IMAGE}:${VERSION}")
imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} ${VERSION_SUFFIX})")
fi

# tags/v1.0.0 => 1.0
Expand All @@ -32,20 +63,12 @@ if [[ "${GITHUB_REF}" =~ "refs/tags/" ]]; then
IFS='.' read -a release_arr <<< "${GITHUB_REF#refs/tags/}"
releaseMajor=${release_arr[0]#v*} # 2.7.0 => "2"
releaseMinor=${release_arr[1]} # "2.7.0" => "7"
imageTagArr+=("${IMAGE}:${VERSION}")
imageTagArr+=("${IMAGE}:${VERSION}-${releaseMajor}")
imageTagArr+=("${IMAGE}:${VERSION}-${releaseMajor}.${releaseMinor}")
imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} ${VERSION_SUFFIX})")
imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} ${releaseMajor} ${VERSION_SUFFIX})")
imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} ${releaseMajor}.${releaseMinor} ${VERSION_SUFFIX})")
fi

# Build an array of registry/image:tag values
declare -a repoImageTagArr
for registry in ${registryArr[@]}; do
for imageTag in ${imageTagArr[@]}; do
repoImageTagArr+=("${registry}/${imageTag}")
done
done

# Print with new lines for output in build logs
(IFS=$'\n'; echo "${repoImageTagArr[*]}")
# Using newlines in outputs variables does not seem to work, so we'll use comas
(IFS=$','; echo "::set-output name=tags::${repoImageTagArr[*]}")
# Note: imageTagArr is passed as variable name ("reference") and then expanded inside the called function
# See https://stackoverflow.com/questions/16461656/how-to-pass-array-as-an-argument-to-a-function-in-bash/26443029#26443029
# DockerHub tags
set_output imageTagArr
Loading

0 comments on commit cd6666d

Please sign in to comment.