DEV Community

Martin Pham
Martin Pham

Posted on • Edited on

Automatic deployment to GIT branch with GitHub workflow

Deploy application to GIT branch workflow

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.

Our workflow result

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.

Submission Category: DIY Deployments

Yaml File or Link to Code

GitHub logo ActionsHackathon21 / deploy-to-git-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.

Screenshot

Check the complete workflow here (build-and-deploy-to-branch.yml)

Actions used

(Also actions/setup-node@v2 for setup nodejs, although it's not required)

Configurations

  • You can config the deployment branch postfix, with DEPLOY_BRANCH_POSTFIX variable. So the code on "main" branch will be built and pushed into main-<DEPLOY_BRANCH_POSTFIX> branch
  • You can also configure the branches which you want to run this workflow, with branches key.

Flows

In this repository, I use…

In the repository, there is a sample NextJS project, however you can change a bit on the workflow file to match your project.

Configuration

  • You can config the deployment branch postfix, with the DEPLOY_BRANCH_POSTFIX variable. Example: the code on main branch will be built and pushed into main-<DEPLOY_BRANCH_POSTFIX> branch

  • You can also configure the branches which you want to run this workflow, with branches key.

Top comments (0)