Skip to content

Commit

Permalink
Merge pull request #2101 from video-dev/netlify
Browse files Browse the repository at this point in the history
configure netlify for PR's
  • Loading branch information
tjenkinson committed Jan 28, 2019
2 parents e7f9c9e + e2e71c7 commit dcb3db6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 59 deletions.
14 changes: 14 additions & 0 deletions netlify.toml
@@ -0,0 +1,14 @@
# https://www.netlify.com/docs/netlify-toml-reference/

# Settings in the [build] context are global and are applied to all contexts
# unless otherwise overridden by more specific contexts.
[build]
# Directory (relative to root of your repo) that contains the deploy-ready
# HTML files and assets generated by the build. If a base directory has
# been specified, include it in the publish directory path.
publish = "netlify"

# Deploy Preview context: all deploys resulting from a pull/merge request will
# inherit these settings.
[context.deploy-preview]
command = "TRAVIS_MODE=netlifyPr ./scripts/travis.sh"
17 changes: 17 additions & 0 deletions scripts/build-netlify.sh
@@ -0,0 +1,17 @@
#!/bin/bash
set -e

root="./netlify"

rm -rf "$root"
mkdir "$root"

echo "Building netlify..."

# redirect / to /demo
echo "/ /demo" > "$root/_redirects"
cp -r "./dist" "$root/dist"
cp -r "./demo" "$root/demo"
cp -r "./api-docs" "$root/api-docs"

echo "Built netlify."
15 changes: 1 addition & 14 deletions scripts/build-deploy-netlify.sh → scripts/deploy-netlify.sh
Expand Up @@ -10,27 +10,14 @@ idShort="$(echo "$id" | cut -c 1-8) ($version)"
latestSiteId="642d9ad4-f002-4104-9309-40ed9cd81a1f"
stableSiteId="deef7ecf-4c3e-4de0-b6bb-676b02e1c20e"

rm -rf "$root"
mkdir "$root"

echo "Building netlify for $id"

# redirect / to /demo
echo "/ /demo" > "$root/_redirects"
cp -r "./dist" "$root/dist"
cp -r "./demo" "$root/demo"
cp -r "./api-docs" "$root/api-docs"

echo "Built netlify."

deploy () {
siteId=$1
echo "Deploying netlify to '$siteId'."
./node_modules/.bin/netlify deploy -d "$root" -m "deploy for $id" -s "$siteId" --prod -a "$NETLIFY_ACCESS_TOKEN"
echo "Deployed netlify to '$siteId'."
}

echo "Creating site for current commit."
echo "Creating site for current commit ($id)."
uuid=$(uuidgen)
commitSiteName="hls-js-$uuid"
commitSiteId=$(curl --fail -d "{\"name\":\"$commitSiteName\"}" -H "Content-Type: application/json" -X POST "https://api.netlify.com/api/v1/sites?access_token=$NETLIFY_ACCESS_TOKEN" | jq -r '.site_id')
Expand Down
20 changes: 16 additions & 4 deletions scripts/set-package-version.js
Expand Up @@ -17,7 +17,7 @@ try {
}
// remove v
newVersion = tag.substring(1);
} else if (TRAVIS_MODE === 'releaseCanary') {
} else if (TRAVIS_MODE === 'releaseCanary' || TRAVIS_MODE === 'netlifyPr') {
// bump patch in version from latest git tag
let currentVersion = getLatestVersionTag();
if (!VALID_VERSION_REGEX.test(currentVersion)) {
Expand All @@ -34,7 +34,11 @@ try {
if (!matched) {
throw new Error('Error calculating version.');
}
newVersion += '-canary.' + getCommitNum();
if (TRAVIS_MODE === 'netlifyPr') {
newVersion += `-pr.${getCommitHash().substr(0, 8)}`;
} else {
newVersion += `-canary.${getCommitNum()}`;
}
} else {
throw new Error('Unsupported travis mode: ' + TRAVIS_MODE);
}
Expand All @@ -49,9 +53,17 @@ try {
process.exit(0);

function getCommitNum() {
return parseInt(require('child_process').execSync('git rev-list --count HEAD').toString(), 10);
return parseInt(exec('git rev-list --count HEAD'), 10);
}

function getCommitHash() {
return exec('git rev-parse HEAD');
}

function getLatestVersionTag() {
return require('child_process').execSync('git describe --abbrev=0 --match="v*"').toString().trim();
return exec('git describe --abbrev=0 --match="v*"');
}

function exec(cmd) {
return require('child_process').execSync(cmd).toString().trim();
}
90 changes: 49 additions & 41 deletions scripts/travis.sh
Expand Up @@ -11,54 +11,62 @@ if [ "${TRAVIS_MODE}" = "build" ]; then
# see https://github.com/video-dev/hls.js/pull/1642
node -e 'require("./" + require("./package.json").main)'
elif [ "${TRAVIS_MODE}" = "unitTests" ]; then
npm run test:unit
npm run test:unit
elif [ "${TRAVIS_MODE}" = "funcTests" ]; then
npm run build
n=0
maxRetries=1
until [ $n -ge ${maxRetries} ]
do
if [ $n -gt 0 ]; then
echo "Retrying... Attempt: $((n+1))"
delay=$((n*60))
echo "Waiting ${delay} seconds..."
sleep $delay
fi
npm run test:func && break
n=$[$n+1]
done
if [ ${n} = ${maxRetries} ]; then
exit 1
fi
elif [ "${TRAVIS_MODE}" = "release" ] || [ "${TRAVIS_MODE}" = "releaseCanary" ]; then
npm run build
n=0
maxRetries=1
until [ $n -ge ${maxRetries} ]
do
if [ $n -gt 0 ]; then
echo "Retrying... Attempt: $((n+1))"
delay=$((n*60))
echo "Waiting ${delay} seconds..."
sleep $delay
fi
npm run test:func && break
n=$[$n+1]
done
if [ ${n} = ${maxRetries} ]; then
exit 1
fi
elif [ "${TRAVIS_MODE}" = "release" ] || [ "${TRAVIS_MODE}" = "releaseCanary" ] || [ "${TRAVIS_MODE}" = "netlifyPr" ]; then
# update the version
# make sure everything is fetched https://github.com/travis-ci/travis-ci/issues/3412
git fetch --unshallow
if [[ $(git rev-parse --is-shallow-repository) = "true" ]]; then
# make sure everything is fetched https://github.com/travis-ci/travis-ci/issues/3412
git fetch --unshallow
fi
node ./scripts/set-package-version.js
npm run lint
npm run build
npm run test:unit
if [[ $(node ./scripts/check-already-published.js) = "not published" ]]; then
# write the token to config
# see https://docs.npmjs.com/private-modules/ci-server-config
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
if [ "${TRAVIS_MODE}" = "releaseCanary" ]; then
npm publish --tag canary
echo "Published canary."
curl https://purge.jsdelivr.net/npm/hls.js@canary
curl https://purge.jsdelivr.net/npm/hls.js@canary/dist/hls-demo.js
echo "Cleared jsdelivr cache."
elif [ "${TRAVIS_MODE}" = "release" ]; then
npm publish
curl https://purge.jsdelivr.net/npm/hls.js@latest
echo "Published."
if [ "${TRAVIS_MODE}" != "netlifyPr" ]; then
npm run test:unit
if [[ $(node ./scripts/check-already-published.js) = "not published" ]]; then
# write the token to config
# see https://docs.npmjs.com/private-modules/ci-server-config
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
if [ "${TRAVIS_MODE}" = "releaseCanary" ]; then
npm publish --tag canary
echo "Published canary."
curl https://purge.jsdelivr.net/npm/hls.js@canary
curl https://purge.jsdelivr.net/npm/hls.js@canary/dist/hls-demo.js
echo "Cleared jsdelivr cache."
elif [ "${TRAVIS_MODE}" = "release" ]; then
npm publish
curl https://purge.jsdelivr.net/npm/hls.js@latest
echo "Published."
fi
else
echo "Already published."
fi
else
echo "Already published."
fi
npm run docs
./scripts/build-deploy-netlify.sh

./scripts/build-netlify.sh
if [ "${TRAVIS_MODE}" != "netlifyPr" ]; then
./scripts/deploy-netlify.sh
fi
else
echo "Unknown travis mode: ${TRAVIS_MODE}" 1>&2
exit 1
echo "Unknown travis mode: ${TRAVIS_MODE}" 1>&2
exit 1
fi

0 comments on commit dcb3db6

Please sign in to comment.