Last active
December 6, 2016 13:31
-
-
Save FlyersWeb/e13840c52c5f0557b0711ce19ca0fb3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
shopt -s nocasematch | |
set -e | |
set -o pipefail | |
npm run test | |
FIX_PATTERN="*Fix*" | |
MASTER_BRANCH="master" | |
CI_SKIP="[ci skip]" | |
echo "Travis Pull Request : " ${TRAVIS_PULL_REQUEST} | |
if [[ ${TRAVIS_PULL_REQUEST} != 'false' ]]; then | |
echo "Detect pull request. Ignore release task (Done on PR Branch)" | |
exit 0 | |
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 [ -n "$UPDATE_DEP_MESSAGE" ]; then | |
RELEASE_MESSAGE="$UPDATE_DEP_MESSAGE" | |
else | |
RELEASE_MESSAGE="$(git log --first-parent --format=%B -n 1)" | |
fi | |
if [ -z "$RELEASE_TYPE" ]; then | |
if [[ ${RELEASE_MESSAGE} == ${FIX_PATTERN} ]]; then | |
RELEASE_TYPE="patch" | |
else | |
RELEASE_TYPE="minor" | |
fi | |
fi | |
RELEASE_VERSION=`semver ${NPM_PACKAGE_VERSION} -i ${RELEASE_TYPE}` | |
if [[ ${TRAVIS_BRANCH} != ${MASTER_BRANCH} ]]; then | |
SHORT_HASH="$(git rev-parse --short ${TRAVIS_COMMIT})" | |
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 "Message :" ${RELEASE_MESSAGE} | |
################################################################################################### | |
### Publish release | |
################################################################################################### | |
npm version ${RELEASE_VERSION} | |
if [[ ${TRAVIS_BRANCH} == ${MASTER_BRANCH} ]]; then | |
if [ -n "$UPDATE_DEP_NAME" ] && [ -n "$UPDATE_DEP_VERSION" ] && [ -n "$UPDATE_DEP_MESSAGE" ]; | |
then | |
echo "Should update ${UPDATE_DEP_NAME} to ${UPDATE_DEP_VERSION}" | |
sed -i "s|\(\"$UPDATE_DEP_NAME\"\s*:\s*\) \".*\"|\1 \"$UPDATE_DEP_VERSION\"|g" package.json | |
npm install --no-optional | |
npm run test | |
fi | |
git add package.json | |
git commit -m "${RELEASE_MESSAGE} - ${CI_SKIP}" | |
git pull --rebase | |
git push origin ${TRAVIS_BRANCH} | |
fi | |
npm publish | |
git tag ${RELEASE_TAG} -a -m "Version ${RELEASE_VERSION}" | |
git push origin ${RELEASE_TAG} | |
################################################################################################### | |
### Ask related dependencies build | |
################################################################################################### | |
GITHUB_RELATED_PROJECTS=($GITHUB_RELATED_NAMES) | |
echo "Related projects :" ${GITHUB_RELATED_PROJECTS} | |
if [[ ${TRAVIS_BRANCH} == ${MASTER_BRANCH} ]]; then | |
for GITHUB_REPO_NAME in "${GITHUB_RELATED_PROJECTS[@]}" | |
do | |
echo "Update related dependency :" ${GITHUB_REPO_NAME} | |
body="{ | |
\"request\": { | |
\"message\":\"Update related dependency\", | |
\"branch\":\"${TRAVIS_BRANCH}\", | |
\"config\": { | |
\"env\": { | |
\"matrix\": [ | |
\"RELEASE_TYPE=${RELEASE_TYPE} UPDATE_DEP_VERSION=${RELEASE_VERSION} UPDATE_DEP_NAME=${NPM_PACKAGE_NAME} UPDATE_DEP_MESSAGE=${RELEASE_MESSAGE}\" | |
] | |
} | |
} | |
}}"; | |
curl -s -X POST \ | |
-H "Content-Type: application/json" \ | |
-H "Accept: application/json" \ | |
-H "Travis-API-Version: 3" \ | |
-H "Authorization: token ${TRAVIS_TOKEN}" \ | |
-d "$body" \ | |
https://api.travis-ci.com/repo/${GITHUB_ORGANIZATION_NAME}%2F${GITHUB_REPO_NAME}/requests | |
done | |
else | |
echo "Do not update related dependencies if current branch is not default" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/FlyersWeb/e13840c52c5f0557b0711ce19ca0fb3f#file-npm_release-sh-L9
I think FIX makes more "sense" than Fix