Daniel Dayley
5868c928f9
All checks were successful
git.cronocide.net/container-copy/pipeline/head This commit looks good
152 lines
4.7 KiB
Bash
Executable File
152 lines
4.7 KiB
Bash
Executable File
#!/bin/bash
|
|
# Generic Build Script for Jenkins
|
|
# v1.0 Mar 2023 by Cronocide
|
|
|
|
# ___ ___ ___ _ ___ ___ ___ _ _ _____ ___
|
|
# | _ )/ _ \_ _| | | __| _ \ _ \ | /_\_ _| __|
|
|
# | _ \ (_) | || |__| _|| / _/ |__ / _ \| | | _|
|
|
# |___/\___/___|____|___|_|_\_| |____/_/ \_\_| |___|
|
|
|
|
# Get the OS type. Most specific distributions first.
|
|
export OS="$(uname -a)"
|
|
[[ "$OS" == *"iPhone"* || "$OS" == *"iPad"* ]] && export OS="iOS"
|
|
[[ "$OS" == *"ndroid"* ]] && export OS="Android"
|
|
[[ "$OS" == *"indows"* ]] && export OS="Windows"
|
|
[[ "$OS" == *"arwin"* ]] && export OS="macOS"
|
|
[[ "$OS" == *"BSD"* ]] && export OS="BSD"
|
|
[[ "$OS" == *"inux"* ]] && export OS="Linux"
|
|
|
|
# Verify a list of software or operating systems. Inverted returns for ease-of-use.
|
|
__missing_os() {
|
|
for i in $(echo "$@"); do
|
|
! __no_os "$i" && return 1
|
|
done
|
|
echo "This function is not available on $OS." && return 0
|
|
}
|
|
|
|
__no_os() {
|
|
[[ "$OS" == "$1" ]] && return 1
|
|
return 0
|
|
}
|
|
|
|
# Verify we have dependencies needed to execute successfully
|
|
__missing_reqs() {
|
|
for i in "$@"; do
|
|
[[ "$0" != "$i" ]] && __no_req "$i" && echo "$i is required to perform this function." && return 0
|
|
done
|
|
return 1
|
|
}
|
|
|
|
__no_req() {
|
|
[[ "$(type $1 2>/dev/null)" == '' ]] && return 0
|
|
return 1
|
|
}
|
|
|
|
# An abstraction for curl/wget. Accepts url and <optional output path>
|
|
__http_get() {
|
|
OUTPUT="$2"; [ -z "$OUTPUT" ] && export OUTPUT="-"
|
|
if [[ "$(type curl 2>/dev/null)" != '' ]]; then
|
|
curl -s "$1" -o "$OUTPUT"
|
|
[ "$OUTPUT" != "-" ] && (! [ -f "$OUTPUT" ] || [[ $(cat "$OUTPUT" | tr -d '\0' 2>/dev/null) == '' ]]) && return 1
|
|
return 0
|
|
else
|
|
if ! [[ "$(type wget 2>/dev/null)" != '' ]]; then
|
|
wget "$1" -O "$2"
|
|
[ "$OUTPUT" != "-" ] && (! [ -f "$2" ] || [[ $(cat "$2" | tr -d '\0' 2>/dev/null) == '' ]]) && return 1
|
|
return 0
|
|
fi
|
|
fi
|
|
echo "curl or wget is required to perform this function." && return 1
|
|
}
|
|
|
|
__missing_sed() {
|
|
__no_req "sed" && __no_req "gsed" && echo "sed or gsed is required to perform this function." && return 0
|
|
}
|
|
|
|
sed_i() {
|
|
__missing_sed && return 1;
|
|
if [[ "$OS" == "macOS" ]]; then
|
|
if [[ $(type gsed 2>/dev/null) != '' ]]; then
|
|
gsed -i "$@";
|
|
else
|
|
sed -i '' "$@";
|
|
fi;
|
|
else
|
|
sed -i "$@";
|
|
fi
|
|
}
|
|
|
|
# Echo errors to stderr
|
|
error() {
|
|
echo "$@" 1>&2
|
|
}
|
|
|
|
# ___ _ _ _ _ ___ _____ ___ ___ _ _ ___
|
|
# | __| | | | \| |/ __|_ _|_ _/ _ \| \| / __|
|
|
# | _|| |_| | .` | (__ | | | | (_) | .` \__ \
|
|
# |_| \___/|_|\_|\___| |_| |___\___/|_|\_|___/
|
|
|
|
get_regctl() {
|
|
REGCTL_URL=$(curl -s -L "${GITHUB_AUTH[@]}" "https://api.github.com/repos/regclient/regclient/releases/latest" | grep browser_download_url | grep linux | grep amd | grep regctl | cut -d '"' -f 4)
|
|
wget -q "$REGCTL_URL" -O regctl
|
|
chmod +x regctl
|
|
}
|
|
|
|
cicd_copy() {
|
|
# Download regctl
|
|
echo "Downloading regctl..."
|
|
get_regctl
|
|
# Log in to repo
|
|
echo "Logging into repo..."
|
|
./regctl registry login "$GIT_REPO_NAME" --user "$DOCKER_USERNAME" --pass "$DOCKER_PASSWORD" 2>&1
|
|
# Copy repos
|
|
echo "Copying repos..."
|
|
for i in "${REPOS_TO_COPY[@]}"; do
|
|
./regctl image copy $i 2>&1
|
|
done
|
|
echo "Done copying images"
|
|
}
|
|
|
|
|
|
|
|
# __ __ _ ___ _ _
|
|
# | \/ | /_\ |_ _| \| |
|
|
# | |\/| |/ _ \ | || .` |
|
|
# |_| |_/_/ \_\___|_|\_|
|
|
|
|
__missing_reqs "git sed" && exit 1
|
|
|
|
# Verify that an ACTION is supplied in the environment.
|
|
BUILD_PREFIX="cicd"
|
|
[ -z "$ACTION" ] && error "No ACTION supplied, no action taken." && exit 1
|
|
[[ "$ACTION" != "$BUILD_PREFIX"* ]] && error "Action $ACTION is not recognized as a valid action."
|
|
__no_req "$ACTION" && error "Action $ACTION is not recognized as a valid action." && exit 1
|
|
|
|
# Authenticate to github if available
|
|
export GITHUB_AUTH=(-H "Authorization: Bearer $GITHUB_TOKEN")
|
|
[[ -z "$GITHUB_TOKEN" ]] && unset GITHUB_AUTH
|
|
|
|
# Fill in variables if not supplied by CICD
|
|
[ -z "$USERN" ] && export USERN=cronocide
|
|
[ -z "$GIT_REPO_NAME" ] && export GIT_REPO_NAME=git.cronocide.net
|
|
|
|
# Define needed build strings
|
|
DIR=$(cd $(dirname $BASH_SOURCE[0]) && pwd)
|
|
PROJECT_NAME="$(git config --local remote.origin.url|sed -n 's#.*/\([^.]*\)\.git#\1#p')"
|
|
IMAGE_NAME="$GIT_REPO_NAME/$USERN/$PROJECT_NAME"
|
|
GIT_COMMIT=$(git rev-parse HEAD)
|
|
GIT_URL=$(git config --get remote.origin.url)
|
|
GIT_BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
|
|
COMMIT_TAG="${IMAGE_NAME}:${GIT_COMMIT}"
|
|
LATEST_TAG="${IMAGE_NAME}:latest"
|
|
|
|
REPOS_TO_COPY=("busybox:latest git.cronocide.net/cronocide/busybox:latest" \
|
|
"nginx:latest git.cronocide.net/cronocide/nginx:latest" \
|
|
"postgres:15 git.cronocide.net/cronocide/postgres:15" \
|
|
"democraticcsi/democratic-csi:latest git.cronocide.net/cronocide/democratic-csi:latest" \
|
|
"python:3.12-bullseye git.cronocide.net/cronocide/python:3.12-bullseye" \
|
|
"docker.io/registry:latest git.cronocide.net/cronocide/registry:latest")
|
|
|
|
# Run specified build task
|
|
"$ACTION"
|