deploy.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. set -e
  3. TARGET_DIR='gh-pages'
  4. TARGET_BRANCH='master'
  5. TARGET_REPO_URL="https://$GITHUB_AUTH_TOKEN@github.com/chartjs/chartjs.github.io.git"
  6. VERSION_REGEX='[[:digit:]]+.[[:digit:]]+.[[:digit:]]+(-.*)?'
  7. # Make sure that this script is executed only for the release and master branches
  8. if [ "$TRAVIS_BRANCH" == "release" ]; then
  9. # Travis executes this script from the repository root, so at the same level than package.json
  10. VERSION=$(node -p -e "require('./package.json').version")
  11. elif [ "$TRAVIS_BRANCH" == "master" ]; then
  12. VERSION="master"
  13. else
  14. echo "Skipping deploy because this is not the master or release branch"
  15. exit 0
  16. fi
  17. function update_latest {
  18. local out_path=$1
  19. local latest=($(ls -v $out_path | egrep '^('$VERSION_REGEX')$' | tail -1))
  20. if [ "$latest" == "" ]; then latest='master'; fi
  21. rm -f $out_path/latest
  22. ln -s $latest $out_path/latest
  23. }
  24. function deploy_files {
  25. local in_files=$1
  26. local out_path=$2
  27. rm -rf $out_path/$VERSION
  28. mkdir -p $out_path/$VERSION
  29. cp -r $in_files $out_path/$VERSION
  30. update_latest $out_path
  31. }
  32. # Clone the repository and checkout the gh-pages branch
  33. git clone $TARGET_REPO_URL $TARGET_DIR
  34. cd $TARGET_DIR
  35. git checkout $TARGET_BRANCH
  36. # Copy dist files
  37. deploy_files '../dist/*.js' './dist'
  38. # Copy generated documentation
  39. deploy_files '../dist/docs/*' './docs'
  40. # Copy samples ...
  41. deploy_files '../samples/*' './samples'
  42. # ... and relocate samples Chart/js scripts
  43. for f in $(find ./samples/$VERSION -name '*.html'); do
  44. sed -i -E "s/((\.\.\/)+dist\/)/..\/\1$VERSION\//" $f
  45. done
  46. git add -A
  47. git remote add auth-origin $TARGET_REPO_URL
  48. git config --global user.email "$GITHUB_AUTH_EMAIL"
  49. git config --global user.name "Chart.js"
  50. git commit -m "Deploy $VERSION from $TRAVIS_REPO_SLUG" -m "Commit: $TRAVIS_COMMIT"
  51. git push -q auth-origin $TARGET_BRANCH
  52. git remote rm auth-origin
  53. # Cleanup
  54. cd ..
  55. rm -rf $TARGET_DIR