Sometimes, there are limitations which prevent your deployment flow from working properly. One of these limitations we were facing sometime, was the build and release process.
Usually, we store our code on a GitHub repository, then setup a webhook on it. So each time we push a new commit into the release branch, GitHub will trigger a webhook call to a script our server, which pulls the latest commit, builds the application, and releases the build.
It was a nice deployment flow, everything including pulling, testing, building, releasing,... worked automatically. However, after months, we've noticed a few issues with that:
- When our server receives a webhook call, it has to test and build the projects. As the project continues growing bigger, the build process becomes slower. Everytime it builds, our server's resources (CPU, RAM, ...) spikes up, affects performances of other running applications.
- It's hard to rollback to different deployments.
After discovering GitHub workflows, we've found a way to improve our deployment process, with many GitHub Actions. And today we'd like to share it with you: We've created a GitHub worklow to build and deploy our application into a Git branch.
With this worklow, we automate all the testing and building steps, then store the ready-to-run build into another branch. Our server just needs to pull the latest build from the deployment branch, and release it. And sometime, if we want to switch between versions, simply switch between commits. We can also trace the build files change back to the commit which made it.
Also, thanks to the actions/cache@v2 action, we could also reduce the building time by caching the dependencies. It allows us to re-use them for future builds
Bonus: You can also use it to deploy to Github Pages, by selecting the destination branch as the Github Pages's branch.
Use GitHub Actions and Workflows to build and deploy your applications to a branch. So you can just pull this branch to deploy on the production server, without building.
Deploy application to GIT branch
This project follows the DEV.to #ActionsHackathon21 hackathon.
Use GitHub Actions and Workflows to build and deploy your applications to a branch. So you can just pull this branch to deploy on the production server, without building. You can also use it to deploy to Github Pages, by selecting the destination branch as the Github Pages's branch.
Check the complete workflow here (build-and-deploy-to-branch.yml)
- actions/checkout@v2 To checkout the source code from the repository
- actions/cache@v2 To cache the dependencies, allow us to re use them for future builds
(Also actions/setup-node@v2 for setup nodejs, although it's not required)
- You can config the deployment branch postfix, with
DEPLOY_BRANCH_POSTFIXvariable. So the code on "main" branch will be built and pushed into
- You can also configure the branches which you want to run this workflow, with
In this repository, I use…
- README: https://github.com/ActionsHackathon21/deploy-to-git-branch/blob/main/README.md
- Workflow file: https://github.com/ActionsHackathon21/deploy-to-git-branch/blob/main/.github/workflows/build-and-deploy-to-branch.yml
- License: https://github.com/ActionsHackathon21/deploy-to-git-branch/blob/main/COPYING
In the repository, there is a sample NextJS project, however you can change a bit on the workflow file to match your project.
You can config the deployment branch postfix, with the
DEPLOY_BRANCH_POSTFIXvariable. Example: the code on
mainbranch will be built and pushed into
You can also configure the branches which you want to run this workflow, with branches key.