Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to use Github Actions #25

Merged
merged 18 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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 -i "HTTP/1.1 204 No Content"
74 changes: 74 additions & 0 deletions .github/scripts/docker-tags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +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 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 tags="${outputArr[*]}" | tee -a ${GITHUB_OUTPUT})
}

# 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
imageTagArr+=("${IMAGE}:$(join ${VERSION_PREFIX}${VERSION} edge ${VERSION_SUFFIX})")
fi

# master => version
if [[ "${GITHUB_REF}" == "refs/heads/master" ]]; then
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 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

# 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