Skip to content

Instantly share code, notes, and snippets.

@stjohnjohnson
Last active March 13, 2018 16:48
Show Gist options
  • Save stjohnjohnson/3d2388b2a7ba658cdcdaffa8cd874e50 to your computer and use it in GitHub Desktop.
Save stjohnjohnson/3d2388b2a7ba658cdcdaffa8cd874e50 to your computer and use it in GitHub Desktop.
List of scripts to help automate publishing to NPM and Docker
#!/bin/bash -e
echo Uploading coverage to Coveralls
export CI_PULL_REQUEST=${SD_PULL_REQUEST}
export COVERALLS_SERVICE_NAME=screwdriver
cat ./artifacts/coverage/lcov.info | ./node_modules/.bin/coveralls
#!/bin/bash -e
# DOCKER_TAG = Tag to check for
# DOCKER_REPO = Org/Repo name
if [ -z "$DOCKER_TAG" ] || [ -z "$DOCKER_REPO" ]; then
echo Unable to docker wait, missing environment variables
exit 2
fi
function check_status {
TOKEN=`wget -q -O - "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$DOCKER_REPO:pull" | python -c "import sys, json; print(json.load(sys.stdin))['token']"`
wget -S --spider -q --header="Authorization: Bearer $TOKEN" https://index.docker.io/v2/$DOCKER_REPO/manifests/$DOCKER_TAG > /dev/null 2>&1
}
echo Looking for image $DOCKER_REPO:$DOCKER_TAG
MINUTES=0
until check_status
do
if [ $MINUTES -gt 20 ] ; then
echo "Timed out after 20 minutes"
exit 1
fi
echo "Not available yet ($MINUTES minutes elapsed)"
sleep 60
((MINUTES+=1))
done
echo Image found
#!/bin/bash -e
# DOCKER_TRIGGER = API Key for triggering Docker Hub
# DOCKER_REPO = Org/Repo name
if [ -z "$DOCKER_TRIGGER" ] || [ -z "$DOCKER_REPO" ]; then
echo Unable to docker trigger, missing environment variables
exit 2
fi
GIT_VERSION=/tmp/gitversion
if [ ! -f "$GIT_VERSION" ] ; then
echo Downloading gitversion
wget -q -O - https://github.com/screwdriver-cd/gitversion/releases/latest \
| egrep -o '/screwdriver-cd/gitversion/releases/download/v[0-9.]*/gitversion' \
| wget --base=https://github.com/ -i - -O /tmp/gitversion
chmod +x $GIT_VERSION
fi
echo "Triggering Docker builds (master and tag)"
git fetch origin --tags
GIT_TAG=`$GIT_VERSION --prefix v show`
DOCKER_URL=https://registry.hub.docker.com/u/${DOCKER_REPO}/trigger/${DOCKER_TRIGGER}/
# Latest
curl -H "Content-Type: application/json" --data '{"docker_tag": "master"}' -X POST $DOCKER_URL
# Recent Tag
curl -H "Content-Type: application/json" --data "{\"source_type\": \"Tag\", \"source_name\": \"${GIT_TAG}\"}" -X POST $DOCKER_URL
#!/bin/bash -e
GIT_VERSION=/tmp/gitversion
if [ ! -f "$GIT_VERSION" ] ; then
echo Downloading gitversion
wget -q -O - https://github.com/screwdriver-cd/gitversion/releases/latest \
| egrep -o '/screwdriver-cd/gitversion/releases/download/v[0-9.]*/gitversion' \
| wget --base=https://github.com/ -i - -O /tmp/gitversion
chmod +x $GIT_VERSION
fi
echo Finding version
$GIT_VERSION --prefix v show | tee VERSION
#!/bin/bash -e
# GITHUB_TOKEN = Token for updating your git repo
# RELEASE_FILE = File to release
if [ -z "$GITHUB_TOKEN" ] || [ -z "$RELEASE_FILE" ]; then
echo Unable to release, missing environment variables
exit 2
fi
GITHUB_RELEASE=/tmp/bin/linux/amd64/github-release
if [ ! -f "$GITHUB_RELEASE" ] ; then
echo Downloading github-release
wget -q -O - https://github.com/aktau/github-release/releases/latest \
| egrep -o '/aktau/github-release/releases/download/v[0-9.]*/linux-amd64-github-release.tar.bz2' \
| wget --base=https://github.com/ -i - -O /tmp/linux-amd64-github-release.tar.bz2
tar -xvf /tmp/linux-amd64-github-release.tar.bz2 -C /tmp
chmod +x $GITHUB_RELEASE
fi
GIT_VERSION=/tmp/gitversion
if [ ! -f "$GIT_VERSION" ] ; then
echo Downloading gitversion
wget -q -O - https://github.com/screwdriver-cd/gitversion/releases/latest \
| egrep -o '/screwdriver-cd/gitversion/releases/download/v[0-9.]*/gitversion' \
| wget --base=https://github.com/ -i - -O /tmp/gitversion
chmod +x $GIT_VERSION
fi
GIT_ORG=`git remote -v | grep fetch | sed 's/ (fetch)//' | cut -d'/' -f4`
GIT_REPO=`git remote -v | grep fetch | sed 's/ (fetch)//' | cut -d'/' -f5`
if [ -f VERSION ] ; then
GIT_TAG=$(<VERSION)
else
GIT_TAG=`$GIT_VERSION --prefix v show`
fi
echo "Creating release $GIT_TAG for $GIT_ORG / $GIT_REPO"
$GITHUB_RELEASE release --user $GIT_ORG --repo $GIT_REPO --tag $GIT_TAG --name $GIT_TAG
$GITHUB_RELEASE upload --user $GIT_ORG --repo $GIT_REPO --tag $GIT_TAG --name $RELEASE_FILE --file $RELEASE_FILE
#!/bin/bash -e
# GIT_KEY = SSH Deployment key
if [ -z "$GIT_KEY" ]; then
echo Unable to git ssh, missing environment variables
exit 2
fi
GITHUB_FINGERPRINT=16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
echo Addding github.com to known_hosts
mkdir -p /root/.ssh
touch /root/.ssh/known_hosts
ssh-keyscan -H github.com >> /root/.ssh/known_hosts
chmod 600 /root/.ssh/known_hosts
echo Validating good known_hosts
ssh-keygen -l -f ~/.ssh/known_hosts | grep $GITHUB_FINGERPRINT
echo Starting ssh-agent
eval "$(ssh-agent -s)"
echo Loading github key
echo $GIT_KEY | sed -E 's/([^ ]{40,64}) /*\1*/g' | tr "*" "\n" | sed '/^$/d' > /tmp/git_key
chmod 600 /tmp/git_key
ssh-keygen -y -f /tmp/git_key > /tmp/git_key.pub
ssh-keygen -l -f /tmp/git_key.pub
ssh-add /tmp/git_key
rm /tmp/git_key
echo Setting up secrets
GIT_PATH=`git remote -v | grep fetch | sed 's/ (fetch)//' | cut -d'/' -f4-5`
git remote set-url --push origin [email protected]:$GIT_PATH
git remote -v
echo Setting global username/email
git config --global user.email "[email protected]"
git config --global user.name "sd-buildbot"
#!/bin/bash -e
DIR="$(cd "$(dirname "$0")" && pwd)"
echo Setting up git SSH
source $DIR/git-ssh.sh
GIT_VERSION=/tmp/gitversion
if [ ! -f "$GIT_VERSION" ] ; then
echo Downloading gitversion
wget -q -O - https://github.com/screwdriver-cd/gitversion/releases/latest \
| egrep -o '/screwdriver-cd/gitversion/releases/download/v[0-9.]*/gitversion' \
| wget --base=https://github.com/ -i - -O /tmp/gitversion
chmod +x $GIT_VERSION
fi
echo Bumping version
$GIT_VERSION --prefix v bump auto | tee VERSION
echo Pushing the new tag to GitHub
git push origin --tags -q
#!/bin/bash -e
# K8S_HOST = Kubernetes Hostname
# K8S_TOKEN = Kubernetes Service Account Token
# K8S_DEPLOYMENT = Kubernetes Deployment Name
# K8S_CONTAINER = Container name to update in deployment
# K8S_IMAGE = Docker image name (without the version)
# K8S_TAG = Docker tag to install
# K8S_ENV_KEY = Environment key to update (optional)
# K8S_ENV_VALUE = Environment value to update (optional)
if [ -z "$K8S_TOKEN" ] || [ -z "$K8S_TAG" ] || [ -z "$K8S_DEPLOYMENT" ] || [ -z "$K8S_CONTAINER" ] || [ -z "$K8S_IMAGE" ] || [ -z "$K8S_HOST" ]; then
echo Unable to kubernetes trigger, missing environment variables
exit 2
fi
if [ ! -z "$K8S_ENV_KEY" ] && [ ! -z "$K8S_ENV_VALUE" ]; then
echo Overriding an Environment key ${K8S_ENV_KEY} with ${K8S_ENV_VALUE}
ENVIRO_MOD=",\"env\":[{\"name\":\"${K8S_ENV_KEY}\",\"value\":\"${K8S_ENV_VALUE}\"}]"
fi
echo "Triggering Kubernetes deployment"
URL=https://${K8S_HOST}/apis/extensions/v1beta1/namespaces/default/deployments/${K8S_DEPLOYMENT}
BODY="{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"${K8S_CONTAINER}\",\"image\":\"${K8S_IMAGE}:${K8S_TAG}\"${ENVIRO_MOD}}]}}}}"
curl -k -i \
-XPATCH \
-H "Accept: application/json, */*" \
-H "Authorization: Bearer ${K8S_TOKEN}" \
-H "Content-Type: application/strategic-merge-patch+json" \
-d $BODY \
$URL > /tmp/k8s_out
grep "200 OK" /tmp/k8s_out || (echo "Failed deployment" && cat /tmp/k8s_out && exit 1)
#!/bin/bash -e
# NPM_FILTER = Filter to reduce NPM dependencies to
if [ -z "$NPM_FILTER" ] ; then
echo Unable to npm duplicate check, missing environment variables
exit 2
fi
echo Searching for filtered packages
npm ls | grep $NPM_FILTER | tee /tmp/npm.filter
echo Reducing to duplicate packages
cat /tmp/npm.filter | sed -E -e 's/[^a-zA-Z_-]//g' | sort | uniq -iD > /tmp/npm.dups
if [ -s /tmp/npm.dups ] ; then
echo Duplicate packages found, failing build
exit 1
else
echo No duplicate packages found
fi
#!/bin/bash -e
# GIT_KEY = SSH Deployment key
# NPM_TOKEN = NPM token for publishing the module
if [ -z "$GIT_KEY" ] || [ -z "$NPM_TOKEN" ]; then
echo Unable to publish, missing environment variables
exit 2
fi
DIR="$(cd "$(dirname "$0")" && pwd)"
echo Setting up git SSH
source $DIR/git-ssh.sh
echo Setting up npm secrets
npm config set access public > /dev/null 2>&1
npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN > /dev/null 2>&1
npm whoami
echo Bumping the version
./node_modules/.bin/npm-auto-version
echo Publish the package
npm publish
echo Push the new tag to GitHub
git push origin --tags -q
@FenrirUnbound
Copy link

Update docker-wait.sh at Line 10 with

TOKEN=`wget -q -O - "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$DOCKER_REPO:pull" | python -c "import sys, json; print(json.load(sys.stdin))['token']"`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment