Automatic deployment to GIT branch with GitHub workflow
Tags: actionshackathon21, javascript, docker, github
Published: 2021-11-09 13:55:26

### 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](https://github.com/) 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](https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions), we've found a way to improve our deployment process, with many [GitHub Actions](https://docs.github.com/en/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jja2lctzzoj89qj5j31v.png)

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 https://github.com/ActionsHackathon21/deploy-to-git-branch %}

- README: [https://github.com/ActionsHackathon21/deploy-to-git-branch/blob/main/README.md](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](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](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.

### 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.