deploy/ci/tasks/dev-releases/make-release.yml
---
platform: linux
inputs:
- name: stratos
- name: image-tag
- name: helm-repo
outputs:
- name: helm-chart
image_resource:
type: docker-image
source:
# Generated using scripts/Dockerfile.stratos-ci
repository: ghcr.io/cf-stratos/stratos-ci-concourse
run:
path: sh
args:
- -exc
- |
# Initialize Helm for client-side use
helm init --client-only
ROOT_DIR=$PWD
STRATOS=${ROOT_DIR}/stratos
source ${STRATOS}/deploy/ci/tasks/dev-releases/create-chart-helper.sh
source ${STRATOS}/deploy/ci/tasks/dev-releases/github-helper.sh
SOURCE_REPO=${SOURCE_REPO_ORG}/${SOURCE_REPO_REPO}
ARTIFACT_REPO=${GITHUB_ORG}/${GITHUB_REPO}
HELM_REPO=${ROOT_DIR}/helm-repo/${HELM_REPO_PATH}
GIT_TAG=$(cat image-tag/v2-alpha-tag)
VERSION=$(cat image-tag/v2-version)
RELEASE_VERSION=$(cat image-tag/v2-tag)
COMMIT=$(cat image-tag/v2-commit)
HELM_REPOSITORY=${GITHUB_ORG}/${GITHUB_REPO}
cd ${ROOT_DIR}/stratos
# Set git username and password so that we can push changes (Don't log)
set +x
setupGitConfig
set -x
# $VERSION is version number - e.g. 2.3.0
# Find the last tag created for this release tag
LAST_TAG=$(git tag --sort=creatordate | grep "$VERSION-" | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' | head -n 1)
echo "Last tag: $LAST_TAG"
# Get the commit hash for this tag
LAST_TAG_COMMIT=$(git rev-list --abbrev-commit -n 1 ${LAST_TAG})
if [ "$COMMIT" != "$LAST_TAG_COMMIT" ]; then
echo "Can not make release - no release candidate published"
echo "The commit for the \"stable\" tag does not match that of the tag \"$LAST_TAG\""
#exit 1
fi
echo "Making release"
# Check to see if the release tag already exists - if so, abort - we won't re-release
set +e
github-release info -t ${VERSION}
RETVAL=$?
set -e
if [ $RETVAL -eq 0 ]; then
echo "Release already exists... aborting"
exit 1
fi
# Delete Stable release on GitHub and recreate it again
# This ensures it has the correct date/time and appears in the correct place in the release history
# Check that the release exists
set +e
github-release info -t stable
RETVAL=$?
set -e
# Delete the stable release if it already exists
if [ $RETVAL -eq 0 ]; then
github-release delete -t stable
fi
# Create fresh stable release on GitHub
github-release release -t stable --name "Stable" --description "Stable Release"
# Need to create the Helm chart
# Download the Helm chart from the last RC/Beta/Alpha - we'll modify it
# Note: Use helper to ensure this works with a private GitHub repository
mkdir tmp
cd tmp
echo "Downloaing Helm Chart for Release Candidate"
downloadReleaseFile ${GITHUB_TOKEN} ${HELM_REPOSITORY} ${LAST_TAG} console-helm-chart-${LAST_TAG}-${LAST_TAG_COMMIT}.tgz
echo "Downloaded Helm Chart"
ls -al console-helm-chart-${LAST_TAG}-${LAST_TAG_COMMIT}.tgz
# Unpack the Helm Chart from the release candidate
tar -xvf console-helm-chart-${LAST_TAG}-${LAST_TAG_COMMIT}.tgz
echo "Unpacked Helm Chart for Release Candidate"
cat console/Chart.yaml
# List all of the files and folders in the Chart
ls -alR ./console
# Just need to change the version in the Chart.yaml file
sed -i -e 's/version: '"${LAST_TAG}"'/version: '"${VERSION}"'/g' console/Chart.yaml
cat console/Chart.yaml
# Package modified Helm chart
HELM_FILE=console-helm-chart-${VERSION}-${COMMIT}.tgz
helm package console
cp console-${VERSION}.tgz ${ROOT_DIR}/helm-chart/${HELM_FILE}
# Temp folder for helm-chart
mkdir -p ${ROOT_DIR}/helm-chart
# Add helm chart to the helm repository
cd ${ROOT_DIR}/helm-chart/
if [ -f ${HELM_REPO}/index.yaml ]; then
cp ${HELM_REPO}/index.yaml ${ROOT_DIR}/helm-chart/
MERGE_INDEX="--merge index.yaml"
fi
# Update Helm Repository
helm repo index ./ ${MERGE_INDEX} --url https://github.com/${GITHUB_ORG}/${GITHUB_REPO}/releases/download/${VERSION}/
cp index.yaml ${HELM_REPO}
cd ${HELM_REPO}
set +x
setupAndPushChange
set -x
# Tag the repo and create the release on GitHub
cd ${ROOT_DIR}/stratos
# Create tag for the release
git tag -f -a ${VERSION} -m "${VERSION}"
git push -f origin ${VERSION}
# Create a tag for the release and push the tag
# Check that the release exists first
set +e
github-release info -t ${VERSION}
RETVAL=$?
set -e
# Delete the release if it already exists
if [ $RETVAL -eq 0 ]; then
github-release delete -t ${VERSION}
fi
# Generate title for the release, based on the tag
# Sets RELEASE_TITLE and PRE_RELEASE_ARG
generateReleaseTitle ${VERSION}
echo "Release Title - ${RELEASE_TITLE}"
echo "Release Arg - ${PRE_RELEASE_ARG}"
# Create new release
github-release release -t ${VERSION} --name "${RELEASE_TITLE}" ${PRE_RELEASE_ARG} --description "${RELEASE_DESCRIPTION}"
# Upload Helm chart to release
github-release upload -t ${VERSION} --file ${ROOT_DIR}/helm-chart/${HELM_FILE} --name "${HELM_FILE}"
# Create a release if the source repo is not the same as the helm repo
if [ "${SOURCE_REPO}" != "${ARTIFACT_REPO}" ]; then
echo "Creating release in GitHub Repository: ${SOURCE_REPO}"
# Create new release in the source repository
github-release release --user ${SOURCE_REPO_ORG} --repo ${SOURCE_REPO_REPO} -t ${VERSION} --name "${RELEASE_TITLE}" ${PRE_RELEASE_ARG} --description "${RELEASE_DESCRIPTION}"
fi
# Create the PR for the Helm Chart changes if configured
if [ "${HELM_RELEASE_GITCLONE}" != "null" ]; then
echo "Creating PR for Helm Chart Changes for this release"
NEW_BRANCH="stratos-${VERSION}"
mkdir -p ${ROOT_DIR}/helm-pr
cd ${ROOT_DIR}/helm-pr
git clone ${HELM_RELEASE_GITCLONE} helm-git-repo
cd helm-git-repo
git checkout ${HELM_RELEASE_REPO_BRANCH}
git checkout -b ${NEW_BRANCH}
cd ${HELM_RELEASE_REPO_FOLDER}
cp ${ROOT_DIR}/helm-chart/${HELM_FILE} .
tar -xvf ${HELM_FILE}
# Patch the registry hostname and org if needed
if [ ! -z "${HELM_RELEASE_REGISTRY_HOST}" ]; then
echo "Patching Helm Chart Docker Registry and Organisation"
cat ./console/values.yaml
sed -i -e 's/hostname: '"${DOCKER_REGISTRY}"'/hostname: '"${HELM_RELEASE_REGISTRY_HOST}"'/g' ./console/values.yaml
sed -i -e 's/organization: '"${DOCKER_ORG}"'/organization: '"${HELM_RELEASE_REGISTRY_ORG}"'/g' ./console/values.yaml
echo "Updated Chart..."
cat ./console/values.yaml
fi
rm -rf ./${HELM_FILE}
git add -A
git status
git commit -m "Stratos Release ${VERSION} Helm Chart Changes"
git push -u origin ${NEW_BRANCH}
# Now create a PR for the changes
URL=https://api.github.com/repos/${HELM_RELEASE_REPO_NAME}/pulls
TITLE="Stratos Release ${VERSION}"
curl --fail -H "Content-Type: application/json" -H "Authorization: token ${GITHUB_TOKEN}" \
-d '{"title":"'"${TITLE}"'", "head": "'"${NEW_BRANCH}"'", "base": "'"${HELM_RELEASE_REPO_BRANCH}"'"}' ${URL}
fi
set +e
set +x
echo "=================================================================================================================="
echo ""
echo "RELEASE COMPLETE ================================================================================================="
echo ""
echo "Version : ${VERSION}"
echo "Base Version : ${LAST_TAG}"
echo ""
echo "Helm Chart filename : ${HELM_FILE}"
echo "Helm Chart URL : https://github.com/${HELM_REPOSITORY}/releases/download/${VERSION}/${HELM_FILE}"
echo ""
echo "=================================================================================================================="
# If we have a mirror URL for the Helm Chart then output that at the end
if [ ! -z "${HELM_MIRROR_BASE_URL}" ]; then
echo ""
echo "=================================================================================================================="
echo "Helm Chart will be mirrored to:"
echo "${HELM_MIRROR_BASE_URL}${HELM_FILE}"
echo "=================================================================================================================="
fi