Auto-Deployer with OneDrive Makes Hexo Easier to Use

Ever since I built my blog, I was looking for a practical method to make it easier for me to deploy articles. I coded and introduced a simple tool to watch the local directory and auto-deploy articles. Useful but not every convenient because it is not easy to upload files by using sftp. So today, I’m going to introduce a better tool that can work with OneDrive to simplify the progress of deploying articles extremely. It can either watch local directory or watch remote OneDrive directory and auto-deploy. All you need to do is upload your articles to OneDrive through the web portal or move them to your local OneDrive directory.

Download and Install

Repositories: Github

You can clone or download this repository to your desired directory.

git clone https://github.com/SpereShelde/hexo-auto-deployer.git

Or

wget -O hexo-auto-deployer.zip 'https://github.com/SpereShelde/hexo-auto-deployer/archive/master.zip'

unzip hexo-auto-deployer.zip

Config file

You need to edit the config.js file under this program’s root directory before running.

For method, two options can be chosen for now.

  • local is to watch local directory, which is almost the same with the tool introduced last time
  • oneDrive is to watch the remote OneDrive directory, which is more convenient

OneDrive configuration

If you choose oneDrive, please read notes below as reference

To operate OneDrive, A Azure App is needed.

The default values of redirect_uri, client_id and client_secret come from my App. You are free and safe to use, but I recommend you to build your own.

OneDrive - App Registrations (optional)

Please login to Azure with your Microsoft account: https://portal.azure.com. Search and visit the App registration page to create a new registration. Fill the name input with whatever you want and select whatever type because they won’t affect anything for personal use. And for Redirect URI, I recommend you to fill it with http://localhost/. And please fill it in the redirect_uri line in config.js file.

After registration, you can find the Application (client) ID on the Overview page. Please fill it in the client_id line in the config.js file.

Then, visit the Certificates & secrets page to create a secret as shown in the image below.

Note: the value shows only once, please copy and paste it in the client_secret line in the config.js file.

Finally please add API permissions as shown below.

OneDrive - code

There is a code value to fill in the config.js file, which is used to login to your OneDrive account. So you have to grant it manually. Please visit the URL below, log in, grant, and finally come to a blank page. DO NOT close that page and please copy the value after code= in the URL of that page. Then please paste it in the config.js file.

*NOTE: *Users of business version OneDrive may get a really long URL in this step, and that contains a &session_state=xxxxxx. Please only copy and paste the code value which is after code= and before &session_state=xxxxxx.

URL mentioned above:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=c7596fb8-beee-4b96-ac86-5277779f4820&response_type=code&scope=offline_access+files.readwrite+files.read+files.read.all+files.readwrite.all&redirect_uri=http://localhost/onedrive-login

*NOTE: *This page comes from my application. If you created your own application, please edit the link like below (remember to remove the {}):

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={Application (client) ID}&response_type=code&scope=offline_access+files.readwrite+files.read+files.read.all+files.readwrite.all&redirect_uri={Redirect URI}

OneDrive - blogs

This records the blogs to watch. You can add more than one set of configuration if your deployed multiple blogs.

  • blogPath is the root directory of your blog
  • localPostsPath is the directory of the posts of your blog
  • remotePostsPath is the remote directory in OneDrive storing your posts
  • cycle is monitoring interval in seconds.

Local configuration

Please refer information below if you choose to use Local method.

Local - blogs

This records the blogs to watch. You can add more than one set of configuration if your deployed multiple blogs.

  • blogPath is the root directory of your blog
  • localPostsPath is the directory of the posts of your blog

Monitoring interval is not needed because this tool can re-deploy the blog right after changes detected.

Run

You can run Hexo-Auto-Deployer after completing the configuration file by using npm start or node hexoAutoDeploy.js

Again, screen is recommended: screen -R auto

Principal

Not much to discuss. You are free to read the source code.

Principal of Local method

Please read my last article for more information.

Principal of OneDrive method

  • Login to get access token by using API of OneDrive.
  • Use cyclic tasks to refresh the login state and access token.
  • Use cyclic tasks to query for the states of files in the given directory.
    • Download new files
    • Download newly edited files (by comparing the modification time)
    • Delete the local file if that file is deleted on OneDrive.
    • If any of these three events occur, re-deploy the blog.

Comments