dotfiles/home/.bin/gs-create-repo

215 lines
5.9 KiB
Bash
Executable File

#!/usr/bin/env bash
source .utils
TEMPLATE="giantswarm/template-app"
VISIBILITY="public"
CODEOWNER_TEAM="team-tinkerers"
print_usage() {
orange "gs-create-repo - a new Giant Swarm repo"
echo " "
underline "Usage:"
echo "gs-create-repo (flags) [repo-name]"
echo " "
echo " "
underline "Options:"
echo "-h, --help show this help text"
echo "-t, --template the template repo to base the new repo on (default: ${TEMPLATE})"
echo " --team the team to be set as codeowner of the repo (default: ${CODEOWNER_TEAM})"
echo " --visibility the visibility of the repo (default: ${VISIBILITY}"
}
POS_ARGS=()
while test $# -gt 0; do
case "$1" in
-t|--template)
shift
TEMPLATE=$1
shift
;;
-p|--private)
shift
VISIBILITY="private"
;;
--visibility)
shift
VISIBILITY=$1
shift
;;
--team)
shift
CODEOWNER_TEAM=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
*)
POS_ARGS+=${1}
shift
;;
esac
done
case $TEMPLATE in
*/*)
shift
;;
*)
TEMPLATE="giantswarm/${TEMPLATE}"
;;
esac
REPOSITORY_NAME=${POS_ARGS[0]}
#############################################
echo "✨ Creating new repo $(italic ${VISIBILITY}) $(orange ${REPOSITORY_NAME}) using base template $(blue ${TEMPLATE}) assigned to $(underline ${CODEOWNER_TEAM})"
printf "Continue? (y/n): "
read CONFIRM
if [[ "${CONFIRM}" != "y" ]]; then
exit 1
fi
gh repo create --${VISIBILITY} --template ${TEMPLATE} giantswarm/${REPOSITORY_NAME}
echo "Waiting breifly for cluster to be created from template..."
sleep 5
DST="${HOME}/Code/giantswarm/${REPOSITORY_NAME}"
echo "Cloning repo to ${DST}"
mkdir -p ${DST}
git clone git@github.com:giantswarm/${REPOSITORY_NAME}.git ${DST}
cd ${DST}
if [ -d helm/APP-NAME ]; then
mv 'helm/{APP-NAME}' helm/${REPOSITORY_NAME}
fi
devctl replace -i '{APP-NAME}' ${REPOSITORY_NAME} --ignore .git ./.** ./**
# Clean up some stuff
sed -i 's|\[Read me after cloning this template (GS staff only)\](https://intranet.giantswarm.io/docs/dev-and-releng/app-developer-processes/adding_app_to_appcatalog/)||g' README.md
sed -i 's|- {APP HELM REPOSITORY}||g' README.md
sed -i '$!N; /^\(.*\)\n\1$/!P; D' README.md
sed -i 's/- .*//' CHANGELOG.md
sed -i '$!N; /^\(.*\)\n\1$/!P; D' CHANGELOG.md
echo ""
blue "Run Kubebuilder init? (y/n): "
read CONFIRM
if [ "${CONFIRM}" = "y" ]; then
mv helm .helm
kubebuilder init --domain giantswarm.io --repo github.com/giantswarm/${REPOSITORY_NAME} --plugins=go/v4-alpha
mv .helm helm
mv Makefile Makefile.kubebuilder.mk
go mod tidy
fi
devctl gen workflows --flavour app --flavour generic --check-secrets
devctl gen makefile --flavour app --flavour generic --language go
touch Makefile.custom.mk
echo ""
blue "Update Circle-CI job? (y/n): "
read CONFIRM
if [ "${CONFIRM}" = "y" ]; then
cat << EOF > .circleci/config.yml
version: 2.1
orbs:
architect: giantswarm/architect@4.24.0
workflows:
test-and-push:
jobs:
- architect/go-build:
context: architect
name: go-build
binary: ${REPOSITORY_NAME}
resource_class: xlarge
filters:
tags:
only: /^v.*/
- architect/push-to-docker:
context: architect
name: push-${REPOSITORY_NAME}-to-quay
image: "quay.io/giantswarm/${REPOSITORY_NAME}"
username_envar: "QUAY_USERNAME"
password_envar: "QUAY_PASSWORD"
requires:
- go-build
filters:
# Trigger the job also on git tag.
tags:
only: /^v.*/
- architect/push-to-docker:
context: "architect"
name: push-${REPOSITORY_NAME}-to-docker
image: "docker.io/giantswarm/${REPOSITORY_NAME}"
username_envar: "DOCKER_USERNAME"
password_envar: "DOCKER_PASSWORD"
requires:
- go-build
# Needed to trigger job also on git tag.
filters:
tags:
only: /^v.*/
# Ensure that for every commit
# there is an app version in the test catalog.
- architect/push-to-app-catalog:
context: architect
name: push-to-app-catalog
app_catalog: "control-plane-catalog"
app_catalog_test: "control-plane-test-catalog"
chart: "${REPOSITORY_NAME}"
requires:
- push-${REPOSITORY_NAME}-to-quay
- push-${REPOSITORY_NAME}-to-docker
filters:
# Trigger the job also on git tag.
tags:
only: /^v.*/
- architect/push-to-app-collection:
context: architect
name: push-to-gcp-app-collection
app_name: "${REPOSITORY_NAME}"
app_collection_repo: "gcp-app-collection"
requires:
- push-to-app-catalog
filters:
branches:
ignore: /.*/
tags:
only: /^v.*/
EOF
fi
git add -A
git commit -m "Initial repo scaffold and setup"
git push
devctl repo setup giantswarm/${REPOSITORY_NAME} \
--allow-automerge=true --allow-mergecommit=false --allow-rebasemerge=false \
--allow-squashmerge=true --allow-updatebranch=true --delete-branch-on-merge=true \
--enable-issues=true --enable-projects=false --enable-wiki=false
echo ""
echo "🎉 New repo $(orange ${REPOSITORY_NAME}) created! - https://github.com/giantswarm/${REPOSITORY_NAME}"
echo ""
echo "⚡️ Adding reference to $(orange ${REPOSITORY_NAME}) in giantswarm/github"
cd "${HOME}/Code/giantswarm/github"
git checkout main
git pull
yq -i '. += {"name": "'${REPOSITORY_NAME}'", "gen": {"flavour": "app,generic", "language": "go"}, "replace": {"architect-orb": true, "renovate": true}} | sort_by(.name)' repositories/${CODEOWNER_TEAM}.yaml
git add repositories/${CODEOWNER_TEAM}.yaml
git commit -m "Added ${REPOSITORY_NAME} to ${CODEOWNER_TEAM} repos"
git push
cd -