Skip to content

Instantly share code, notes, and snippets.

@sandfox
Created August 22, 2018 08:18
Show Gist options
  • Save sandfox/99dcf3b2bdd4b30a00a0b53b11280ac7 to your computer and use it in GitHub Desktop.
Save sandfox/99dcf3b2bdd4b30a00a0b53b11280ac7 to your computer and use it in GitHub Desktop.
publish to artifactory from travis
#! /usr/bin/env sh
# The hackiest of hacks
# travis NPM deploy tool can only accept config via .travis.yml.
# travis.yml only supports inline encrypted env vars up 128 cars in length.
# env vars are supported in stuff thats gets fed to bash,
# travis NPM deploy tool read directly from travis.yml
# artifactory tokens are 900+ chars
# Depends on ARTIFACTORY_TOKEN env var being set
# If you run this locally it might nuke your ~/.npmrc - so maybe don't do that?
NPMRC_FILE="$HOME/.npmrc"
DEFAULT_NPM_REGISTRY="registry.npmjs.org/"
if [ -z "$ARTIFACTORY_TOKEN" ]; then
echo 'ARTIFACTORY_TOKEN must contain an auth token'
exit 1
fi
if [ -e package.json ]; then
# This breaks horribly if publishConfig or publishConfig.registry is missing
PACKAGE_REGISTRY=$(node -e "r = require('url').parse(require('./package.json').publishConfig.registry); process.stdout.write(r.host+r.pathname)")
fi
REGISTRY="${PACKAGE_REGISTRY:-$DEFAULT_NPM_REGISTRY}"
NPMRC_FILE_CONTENT="//$REGISTRY:_authToken=${ARTIFACTORY_TOKEN}"
# Protection against people running this on their own laptops
if [ -e "$NPMRC_FILE" ] && [ "$CI" != "true" ]; then
echo "$NPMRC_FILE already exists and not in CI enviroment - not overwriting"
SKIP_NPMRC_REMOVAL=1
else
echo "$NPMRC_FILE_CONTENT" > "$NPMRC_FILE"
fi
# pass args through
npm publish "$@"
if [ -z "$SKIP_NPMRC_REMOVAL" ]; then
# Don't leave credentials lying around if we wrote them
echo "removing $NPMRC_FILE"
rm "$NPMRC_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment