Azure Pipelines has the very good integration with GitHub. It provides 10 free parallel jobs and unlimited minutes for open source projects. It is a very good choice for open source projects to have the CI/CD capability.
As I have configured the site to use hexo’s git deployment, an Azure build pipeline is enough to carry out the deployment. And in the build pipeline, I need the following three tasks.
- Install hexo-cli.
- Install all npm packages used by the site with
- Configure git credential and run
The first 2 steps are quite straight forward. The tricky part is how to use script to configure git credentials.
git credential store only accept inputs from
stdin. I did some search and tests, and ended up with a bash pipe to feed in the credentials. The following is a sample for credential store.
git config --global user.email "<git email address>" git config --global user.name "<git user name>" git config --global credential.helper 'store --file ~/.my-credentials' printf "protocol=https\nhost=github.com\nusername=<username>\npassword=%s\n\n" "$GHP" | git credential-store --file ~/.my-credentials store ./node_modules/.bin/hexo clean && ./node_modules/.bin/hexo deploy
As credential store will save the credential to a file on disk, it is not a good practice for a shared build agent. I ended up to replace the credential store with credential cache, and exit the cache when deployment is done.
The following is the yaml file of the build definition. $GHP is an environment variable storing the personal token of GitHub. The actual value is saved in Azure DevOps encrypted.
resources: - repo: self queue: name: Hosted Ubuntu 1604 demands: npm steps: - script: | echo "install hexo-cli" npm install hexo-cli displayName: 'Install hexo-cli' - task: Npm@1 displayName: 'npm install' inputs: workingDir: / verbose: false - script: | git config --global user.email "<git email address>" git config --global user.name "<git user name>" git config --global credential.helper cache printf "protocol=https\nhost=github.com\nusername=<username>\npassword=%s\n\n" "$GHP" | git credential-cache store ./node_modules/.bin/hexo clean && ./node_modules/.bin/hexo deploy git credential-cache exit displayName: 'hexo deploy' env: GHP: $(password)
Now when I update the site, I just need to commit the changes and push it to GitHub. Azure Pipeline will take care of the build and deployment automatically.