Skip to content

Commit

Permalink
Merge pull request #244 from docksal/develop
Browse files Browse the repository at this point in the history
Release v3.0.0
  • Loading branch information
lmakarov authored Jul 26, 2021
2 parents 813fcbb + d05bac0 commit dd70242
Show file tree
Hide file tree
Showing 63 changed files with 2,436 additions and 1,008 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"
84 changes: 57 additions & 27 deletions .github/scripts/docker-tags.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,74 @@
#!/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 IMAGE_TAGS
# 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

# feature/* => sha-xxxxxxx
# Note: disabled
#if [[ "${GITHUB_REF}" =~ "refs/heads/feature/" ]]; then
# GIT_SHA7=$(echo ${GITHUB_SHA} | cut -c1-7) # Short SHA (7 characters)
# IMAGE_TAGS+=("${REPO}:sha-${GIT_SHA7}-php${VERSION}")
# IMAGE_TAGS+=("ghcr.io/${REPO}:sha-${GIT_SHA7}-php${VERSION}")
#fi
# Join arguments with hyphen (-) as a delimiter
# Usage: join <arg1> [<argn>]
join() {
local IFS='-' # join delimiter
echo "$*"
}

# develop => edge
# 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

## 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
IMAGE_TAGS+=("${REPO}:edge-php${VERSION}")
IMAGE_TAGS+=("ghcr.io/${REPO}:edge-php${VERSION}")
imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} edge ${VERSION_SUFFIX})")
fi

# master => stable
# master => version
if [[ "${GITHUB_REF}" == "refs/heads/master" ]]; then
IMAGE_TAGS+=("${REPO}:stable-php${VERSION}")
IMAGE_TAGS+=("ghcr.io/${REPO}:stable-php${VERSION}")
imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} ${VERSION_SUFFIX})")
fi

# tags/v1.0.0 => 1.0
if [[ "${GITHUB_REF}" =~ "refs/tags/" ]]; then
# Extract version parts from release tag
IFS='.' read -a ver_arr <<< "${GITHUB_REF#refs/tags/}"
VERSION_MAJOR=${ver_arr[0]#v*} # 2.7.0 => "2"
VERSION_MINOR=${ver_arr[1]} # "2.7.0" => "7"
IMAGE_TAGS+=("${REPO}:stable-php${VERSION}")
IMAGE_TAGS+=("${REPO}:${VERSION_MAJOR}-php${VERSION}")
IMAGE_TAGS+=("${REPO}:${VERSION_MAJOR}.${VERSION_MINOR}-php${VERSION}")
IMAGE_TAGS+=("ghcr.io/${REPO}:stable-php${VERSION}")
IMAGE_TAGS+=("ghcr.io/${REPO}:${VERSION_MAJOR}-php${VERSION}")
IMAGE_TAGS+=("ghcr.io/${REPO}:${VERSION_MAJOR}.${VERSION_MINOR}-php${VERSION}")
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}:$(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

# Output a comma concatenated list of image tags
IMAGE_TAGS_STR=$(IFS=,; echo "${IMAGE_TAGS[*]}")
echo "${IMAGE_TAGS_STR}"
echo "::set-output name=tags::${IMAGE_TAGS_STR}"
# 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 dd70242

Please sign in to comment.