Skip to content

Instantly share code, notes, and snippets.

@hjumeau
Last active July 25, 2018 12:42
Show Gist options
  • Save hjumeau/824979b7ddb2fcd243005126f063605e to your computer and use it in GitHub Desktop.
Save hjumeau/824979b7ddb2fcd243005126f063605e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
shopt -s nocasematch
set -e
set -o pipefail
FIX_PATTERN="*FIX*"
MASTER_BRANCH="master"
B2C_REPOSITORY="ClubMediterranee/cm-b2c"
CI_SKIP="[ci skip]"
FORCE_PUBLISH="[ci publish]"
COMMIT_MESSAGE="$(git log --format=%B --no-merges -n 1)"
if [ "${TRAVIS_REPO_SLUG}" == "${B2C_REPOSITORY}" ] && [ "${TRAVIS_BRANCH}" != "${MASTER_BRANCH}" ]; then
if [[ ${COMMIT_MESSAGE} =~ "${FORCE_PUBLISH}" ]]; then
echo "Publish required ! Continuing."
else
## Run at least test for other branches
npm run test
echo "No NPM publish for cm-b2c branches. Exiting."
exit 0
fi
fi
###################################################################################################
### Configure workspace
###################################################################################################
if [ -n "$GIT_USER_EMAIL" ] && [ -n "$GIT_USER_NAME" ];
then
echo "Configure git with username" ${GIT_USER_NAME} "and email" ${GIT_USER_EMAIL}
git config --global user.name ${GIT_USER_NAME}
git config --global user.email ${GIT_USER_EMAIL}
else
echo ERROR:"Ignore git configuration (Empty GIT_USER_EMAIL or/and GIT_USER_NAME variables)"
exit 1
fi
npm config set loglevel warn
npm config set git-tag-version false
git checkout ${TRAVIS_BRANCH}
NPM_PACKAGE_NAME=$(node -p -e "require('./package.json').name")
NPM_PACKAGE_VERSION=`npm show ${NPM_PACKAGE_NAME} version`
echo "-----------------------------------"
echo "--- Package info"
echo "-----------------------------------"
echo "Name : " ${NPM_PACKAGE_NAME}
echo "Version : " ${NPM_PACKAGE_VERSION}
###################################################################################################
### Prepare release infos
###################################################################################################
if [[ ${RELEASE_MESSAGE} == ${FIX_PATTERN} ]]; then
RELEASE_TYPE="patch"
else
RELEASE_TYPE="minor"
fi
RELEASE_NPM_TAG="latest"
RELEASE_MESSAGE="$(git log --first-parent --format=%B -n 1)"
RELEASE_VERSION=`semver ${NPM_PACKAGE_VERSION} -i ${RELEASE_TYPE}`
if [[ ${TRAVIS_BRANCH} != ${MASTER_BRANCH} ]]; then
SHORT_HASH="$(git rev-parse --short ${TRAVIS_COMMIT})"
RELEASE_NPM_TAG="rc"
RELEASE_VERSION=${RELEASE_VERSION}-${SHORT_HASH}
fi
RELEASE_TAG="version-${RELEASE_VERSION}"
RELEASE_MESSAGE=$(echo ${RELEASE_MESSAGE} | sed ':a;N;$!ba;s/\n/ /g')
echo "-----------------------------------"
echo "--- Release info"
echo "-----------------------------------"
echo "Type :" ${RELEASE_TYPE}
echo "Version :" ${RELEASE_VERSION}
echo "Tag :" ${RELEASE_TAG}
echo "Npm Tag :" ${RELEASE_NPM_TAG}
echo "Message :" ${RELEASE_MESSAGE}
###################################################################################################
### Publish release
###################################################################################################
## npm version => bump version, create tag, commit… but don't push.
npm version ${RELEASE_VERSION} -m "${RELEASE_MESSAGE} - ${CI_SKIP}"
## update package.json version only on master build.
if [[ ${TRAVIS_BRANCH} == ${MASTER_BRANCH} ]]; then
git push origin ${TRAVIS_BRANCH}
fi
npm run test
echo "Publishing on NPM"
npm publish --tag ${RELEASE_NPM_TAG}
echo "Pushing on Github"
git push origin ${RELEASE_VERSION}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment