Skip to content

Instantly share code, notes, and snippets.

@svor
Created March 21, 2023 10:47
Show Gist options
  • Save svor/5987377060f92ef4d2f0bfe9ee61d8ce to your computer and use it in GitHub Desktop.
Save svor/5987377060f92ef4d2f0bfe9ee61d8ce to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Copyright (c) 2018-2023 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# defaults
# try to compute branches from currently checked out branch; else fall back to hard coded value
# ./update_images_in_che_samples.sh -t latest --branchfrom main -gh sv-udi-latest
TARGET_BRANCH=sv-udi-latest
pkgs_devel_branch=${TARGET_BRANCH}
pduser=devspaces-build
samplesRepo=che-samples
SOURCE_BRANCH=main # normally, use this script to create tags, not branches
CLEAN="false" # if set true, delete existing folders and do fresh checkouts
if [[ $# -lt 4 ]]; then
echo "
To create tags (and push updated CSV content into operator-bundle repo):
$0 -v CSV_VERSION -t DS_VERSION -gh DS_GH_BRANCH -ghtoken GITHUB_TOKEN -pd PKGS_DEVEL_BRANCH -pduser kerberos_user
Example:
$0 -v 3.y.0 -t 3.y -gh ${TARGET_BRANCH} -ghtoken \$GITHUB_TOKEN -pd ${pkgs_devel_branch} -pduser $pduser
To create or update existing branches and update related PNC build-configs:
$0 -t DS_VERSION --branchfrom SOURCE_GH_BRANCH -gh TARGET_GH_BRANCH -ghtoken GITHUB_TOKEN
Example:
$0 -t DS_VERSION --branchfrom devspaces-3-rhel-8 -gh ${TARGET_BRANCH} -ghtoken \$GITHUB_TOKEN
"
exit 1
fi
# commandline args
while [[ "$#" -gt 0 ]]; do
case $1 in
'--branchfrom') SOURCE_BRANCH="$2"; shift 1;; # this flag will create branches instead of using branches to create tags
'-v') CSV_VERSION="$2"; shift 1;; # 3.y.0
'-t') DS_VERSION="$2"; shift 1;; # 3.y # used to get released bundle container's CSV contents
'-gh') TARGET_BRANCH="$2"; shift 1;;
'-ghtoken') GITHUB_TOKEN="$2"; shift 1;;
'-pd') pkgs_devel_branch="$2"; shift 1;;
'-pduser') pduser="$2"; shift 1;;
'--clean') CLEAN="true"; shift 0;; # if set true, delete existing folders and do fresh checkouts
esac
shift 1
done
if [[ ${CLEAN} == "true" ]]; then
rm -fr /tmp/tmp-checkouts || true
fi
mkdir -p /tmp/tmp-checkouts
cd /tmp/tmp-checkouts
set -e
# for the sample projects ONLY, commit changes to the devfile so it contains the correct image and tag
updateSampleDevfileReferences () {
devfile=devfile.yaml
if [[ $DS_VERSION ]]; then
DS_TAG="$DS_VERSION"
else
DS_TAG="${TARGET_BRANCH//-rhel-8}"; DS_TAG="${DS_TAG//devspaces-}"
fi
DS_TAG=ubi8-latest
# echo "[DEBUG] update $devfile with DS_TAG = $DS_TAG"
sed -i -E "s|image:.*universal-developer-image:.*|image: quay.io/devfile/universal-developer-image:ubi8-latest|" "$devfile"
sed -r -i $devfile -e "s#quay.io/devspaces/#registry.redhat.io/devspaces/#g"
git commit -s -m "chore(devfile) use ubi-latest tag for UDI" "$devfile" || echo ""
}
# create branch or tag
pushBranchAndOrTagGH () {
d="$1"
org="$2"
echo; echo "== $d =="
# if source_branch defined and target branch doesn't exist yet, check out the source branch
if [[ ${SOURCE_BRANCH} ]] && [[ $(git ls-remote --heads "https://github.com/${org}/${d}" "${TARGET_BRANCH}") == "" ]]; then
clone_branch=${SOURCE_BRANCH}
else # if source branch not set (tagging operation) or target branch already exists
clone_branch=${TARGET_BRANCH}
fi
if [[ ! -d "/tmp/tmp-checkouts/projects_${d}" ]]; then
git clone -q --depth 1 -b "${clone_branch}" "https://github.com/${org}/${d}" "projects_${d}"
pushd "/tmp/tmp-checkouts/projects_${d}" >/dev/null || exit 1
export GITHUB_TOKEN="${GITHUB_TOKEN}"
# git config user.email "[email protected]"
# git config user.name "Red Hat Devstudio Release Bot"
# git config --global push.default matching
# git config --global hub.protocol https
git remote set-url origin "https://github.com/${org}/${d}"
git checkout --track "origin/${clone_branch}" -q || true
git pull -q
popd >/dev/null || exit 1
fi
pushd "/tmp/tmp-checkouts/projects_${d}" >/dev/null || exit 1
if [[ ${SOURCE_BRANCH} ]]; then
# create a branch or use existing
git branch "${TARGET_BRANCH}" || true
git checkout "${TARGET_BRANCH}" || true
git pull origin "${TARGET_BRANCH}" || true
# for the devspaces sample repos, update devfiles to point to the correct tag/branch
if [[ $org == "${samplesRepo}" ]]; then
updateSampleDevfileReferences
fi
git pull origin "${TARGET_BRANCH}" || true
git push origin "${TARGET_BRANCH}" || true
fi
popd >/dev/null || exit 1
}
####### sample projects: branching and tagging
# cat devspaces-devfileregistry/devfiles/*/meta.* | grep v2 | sort
sampleprojects="\
web-nodejs-sample \
fuse-rest-http-booster \
bash \
cpp-hello-world \
aspnetcore-realworld-example-app \
golang-example \
lombok-project-sample \
java-guestbook \
java-spring-petclinic \
nodejs-angular \
nodejs-mongodb-sample \
nodejs-react-redux \
react-web-app \
php-symfony \
python-hello-world \
django-realworld-example-app \
quarkus-quickstarts \
helloworld-rust \
scala-sbt \
"
# create branches for devspaces samples, located under https://github.com/${samplesRepo}/
for s in $sampleprojects; do
pushBranchAndOrTagGH "$s" ${samplesRepo}
done
# cleanup
rm -fr /tmp/tmp-checkouts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment