|
- #!/usr/bin/env bash
- #
- # Generate stuffs for GitLab Release
- #
- # Author: donkey <anjingyu_ws@foxmail.com>
-
- # Assume the terminal is a MODERN terminal
- # support colorful output
- normal="\e[0m"
- black="\e[0;30m"
- red="\e[0;31m"
- green="\e[0;32m"
- yellow="\e[0;33m"
- blue="\e[0;34m"
- magenta="\e[0;35m"
- cyan="\e[0;36m"
- white="\e[0;37m"
-
- readonly CUR_DIR=$(cd `dirname $0`; pwd)
- readonly PROJ_DIR=`dirname $CUR_DIR`
- readonly DOCKER_DIR="$PROJ_DIR/docker"
- readonly PROJ_URL="https://gitlab.navinfo.com/api/v4/projects/3958/releases"
- readonly NEXUS_BASE="http://172.25.40.12:8088"
- readonly NEXUS_REPO="${NEXUS_BASE}/repository/pypi-hosted/"
- readonly NEXUS_REPO_URL_PREFIX="${NEXUS_BASE}/repository/pypi-hosted/packages/adtools"
-
- function help()
- {
- echo "Usage: `basename $0` [-h,--help] [-t,--tag TAG]"
- echo
- echo "Options:"
- echo -e " ${green}-h${normal},${green}--help${normal} Show help and exit."
- echo -e " ${green}-t${normal},${green}--tag${yellow} TAG${normal} Specify the tag to release, default: version in __init__.py"
- echo
- echo "Examples:"
- echo " ./`basename $0` -t 0.5.16"
- echo
- }
-
- function main()
- {
- local _TAG=""
- local _RELEASENOTE=""
-
- while [ $# -ne 0 ]; do
- case "$1" in
- -t|--tag)
- shift
- _TAG="$1"
- ;;
- -h|--help)
- help
- exit 0
- ;;
- *)
- ;;
- esac
- shift
- done
-
- if [ -z "${MY_GITLAB_TOKEN}" ]; then
- echo -e "[E] ${red}Please make sure you have a valid token stored in${normal}: ${green}MY_GITLAB_TOKEN${normal}"
- exit 2
- fi
-
- if [ -z "${_TAG}" ]; then
- while read line; do
- # Skip the empty lines and commented lines
- if [ -n "$line" ] && ! [[ "$line" = \#* ]]; then
- _TAG=$(sed "s/.*__version__\s*=\s*\"\(.*\)\".*/\1/" <<< "$line")
- if [ -n "${_TAG}" ]; then
- break
- fi
- fi
- done < "${PROJ_DIR}/adtools/__init__.py"
- fi
-
- local _VER_DIFF="false"
- # Check version number in Dockerfiles
- for dockerfile in `ls $DOCKER_DIR/Dockerfile*`; do
- # Get the tag in current Dockerfile
- _DFTAG=$(sed -n "s/.*ADTOOLS_VERSION\s*=\s*\"\(.*\)\".*/\1/p" $dockerfile)
- if [ "${_DFTAG}" != "${_TAG}" ]; then
- echo -e "[E] ${yellow}The version number ${red}${_DFTAG}${yellow} in ${cyan}docker/`basename $dockerfile`${yellow} is mismatched, expectted: ${green}${_TAG}${normal}"
- _VER_DIFF="true"
- fi
- done
-
- if [ "$_VER_DIFF" = "true" ]; then
- echo -e "[E] Please run the script ${green}tools/update_docker_file.sh${normal} before this script."
- exit -1
- fi
-
- # Check this tag
- local _MATCH_TAG=$(git tag -l "$_TAG")
- if [ -n "${_MATCH_TAG}" ]; then
- echo -e "[W] ${yellow}The tag ${green}${_TAG}${yellow} has been existent!${normal}"
- else
- pushd "${PROJ_DIR}" 1>/dev/null 2>&1
- # Create tag
- git tag ${_TAG}
- git push --tags
- popd 1>/dev/null 2>&1
- fi
-
- # Make python package
- pushd "${PROJ_DIR}" 1>/dev/null 2>&1
- if [ -d "$PROJ_DIR/dist" ]; then
- echo -e "[I] ${cyan}Removing the old dist${normal}"
- rm -rf "$PROJ_DIR/dist"
- fi
-
- # Check package manager
- if command -v "pyproject-build" 1>/dev/null 2>&1; then
- echo -e "[I] ${cyan}Build command: pyproject-build build --wheel${normal}"
- pyproject-build --wheel
- else
- echo -e "[I] ${cyan}Build command: python3 setup.py bdist_wheel${normal}"
- python3 setup.py bdist_wheel
- fi
-
- local _WHLPKG=$(ls "$PROJ_DIR/dist/")
-
- if [ -n "$NEXUS_PY_USERNAME" ] && [ -n "$NEXUS_PY_PASSWORD" ] && [ -n "$NEXUS_REPO" ]; then
- echo -e "[I] ${cyan}Uploading to ${yellow}${NEXUS_REPO}${normal}"
- twine upload -u $NEXUS_PY_USERNAME -p $NEXUS_PY_PASSWORD --repository-url ${NEXUS_REPO} ${PROJ_DIR}/dist/${_WHLPKG}
- fi
-
- echo -e "[I] ${cyan}Cleaning up${normal}"
-
- # Cleanup
- rm -rf build adtools.egg-info
- popd 1>/dev/null 2>&1
-
- local _NEXUS_REPO_URL="${NEXUS_REPO_URL_PREFIX}/${_TAG}/${_WHLPKG}"
-
- echo -e "[I] ${cyan}Releasing to GitLab${normal}"
- local _DTS=`date +"%Y-%m-%dT%H:%M:%S%z"`
-
- if [ -f "$CUR_DIR/RELEASENOTE.md" ]; then
- _RELEASENOTE=`sed -E ':a;N;$!ba;s/\r{0,1}\n/\\\\n/g' "$CUR_DIR/RELEASENOTE.md"`
- else
- echo -e "[W] ${yellow}Release note is missing!${normal}"
- fi
-
- curl -s --header 'Content-Type: application/json' \
- --header "PRIVATE-TOKEN: ${MY_GITLAB_TOKEN}" \
- --data "{ \"name\": \"${_TAG}\", \"tag_name\": \"${_TAG}\", \"description\": \"${_RELEASENOTE}\", \"assets\": { \"links\": [{ \"name\": \"wheel package\", \"url\": \"${_NEXUS_REPO_URL}\" }] }, \"released_at\": \"${_DTS}\" }" \
- "${PROJ_URL}" | jq
- }
-
- main "$@"
|