build-and-publish-nightly.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env node
  2. /**
  3. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  4. * For licensing, see LICENSE.md.
  5. */
  6. /* eslint-env node */
  7. 'use strict';
  8. /*
  9. This script is to be used on CI to automatically update https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/.
  10. */
  11. // Build the documentation only when master branch is updated.
  12. if ( process.env.TRAVIS_BRANCH !== 'master' ) {
  13. process.exit();
  14. }
  15. // Build the documentation only when a cron task triggered the CI.
  16. if ( process.env.TRAVIS_EVENT_TYPE !== 'cron' ) {
  17. process.exit();
  18. }
  19. const path = require( 'path' );
  20. const ROOT_PATH = process.cwd();
  21. const { tools } = require( '@ckeditor/ckeditor5-dev-utils' );
  22. const mainRepoUrl = 'https://github.com/CKEditor5/ckeditor5.github.io';
  23. // The assumption here is that the script is called from ckeditor5/.
  24. const projectVersion = require( path.join( process.cwd(), 'package.json' ) ).version;
  25. // Clone the CKEditor 5 page.
  26. console.log( 'Cloning ckeditor5.github.io repository...' );
  27. exec( `git clone ${ mainRepoUrl }.git` );
  28. // Build the documentation.
  29. console.log( 'Building documentation...' );
  30. exec( 'yarn run docs --production' );
  31. console.log( 'Copying files...' );
  32. // Remove existing documentation.
  33. exec( `rm -rf ckeditor5.github.io/docs/nightly/ckeditor5/${ projectVersion }` );
  34. exec( 'rm -rf ckeditor5.github.io/docs/nightly/ckeditor5/latest' );
  35. // Copy built documentation to the new destination.
  36. exec( 'cp -R build/docs/* ckeditor5.github.io/docs/nightly/' );
  37. // Umberto makes a symlink between the latest version (`projectVersion`) and "latest/" directory.
  38. // The symlink must be deleted in order to copy a documentation for `projectVersion` as `latest`.
  39. exec( 'rm -rf ckeditor5.github.io/docs/nightly/ckeditor5/latest' );
  40. // Copy the versioned documentation to latest/.
  41. exec( `cp -R ckeditor5.github.io/docs/nightly/ckeditor5/${ projectVersion } ckeditor5.github.io/docs/nightly/ckeditor5/latest` );
  42. // Change work directory in order to make a commit in CKEditor 5 page's repository.
  43. process.chdir( path.join( ROOT_PATH, 'ckeditor5.github.io' ) );
  44. exec( `echo "https://${ process.env.GITHUB_TOKEN }:@github.com" > .git/credentials 2> /dev/null` );
  45. exec( 'git config credential.helper "store --file=.git/credentials"' );
  46. // Commit the documentation.
  47. if ( exec( 'git diff --name-only docs/' ).trim().length ) {
  48. exec( 'git add docs/' );
  49. exec( 'git commit -m "Documentation build."' );
  50. exec( 'git push origin master --quiet' );
  51. const lastCommit = exec( 'git log -1 --format="%h"' );
  52. console.log( `Successfully published the documentation under ${ mainRepoUrl }/commit/${ lastCommit }` );
  53. } else {
  54. console.log( 'Nothing to commit. Documentation is up to date.' );
  55. }
  56. // Every 10th day of the month, we would like to clean the history of the documentation repository.
  57. if ( new Date().getDate() === 10 ) {
  58. // Copy a commit which will be a new root in the repository.
  59. const commit = exec( 'git log --oneline --reverse -5 --format="%h" | head -n 1' ).trim();
  60. // Checkout to the status of the git repo at commit. Create a temporary branch.
  61. exec( `git checkout --orphan temp ${ commit }` );
  62. // Create a new commit that is to be the new root commit.
  63. exec( 'git commit -m "Documentation build."' );
  64. // Rebase the part of history from <commit> to master on the temporary branch.
  65. exec( `git rebase --onto temp ${ commit } master` );
  66. // Remove the temporary branch.
  67. exec( 'git branch -D temp' );
  68. // Pray.
  69. exec( 'git push -f' );
  70. }
  71. // Change work directory to the previous value.
  72. process.chdir( ROOT_PATH );
  73. function exec( command ) {
  74. try {
  75. return tools.shExec( command, { verbosity: 'error' } );
  76. }
  77. catch ( error ) {
  78. console.error( error );
  79. process.exit( 1 );
  80. }
  81. }