Branch Deployments With IssueOps and GitHub Actions | by Grant Birkinbine | May, 2022

Take your deployment practices to the celebrities with IssueOps and GitHub Actions

IssueOps + Department + Deploy

The most typical approach builders deploy their adjustments to manufacturing is the merge → deploy mannequin. Nonetheless, there’s a considerably higher technique to ship our code to manufacturing. Reasonably than smash the merge button, cross our fingers, and maintain onto our butts… we are able to hit merge with confidence that our change works precisely as we count on it to!

Introducing… the department deploy mannequin!

In case you have already heard of the department deploy mannequin and are accustomed to it, you’ll be able to skip forward to get to the half the place we implement it with GitHub Actions

To actually perceive the department deploy mannequin, lets first check out a standard deploy → merge mannequin. It goes like this:

  1. Create a department
  2. Add commits to your department
  3. Open a pull request
  4. Collect suggestions + peer opinions
  5. Merge your department
  6. A deployment begins from the primary / grasp department

Now lets check out the department deploy mannequin:

  1. Create a department
  2. Add commits to your department
  3. Open a pull request
  4. Collect suggestions + peer opinions
  5. Deploy your change
  6. Validate
  7. Merge your department
Merge Deploy Mannequin
Department Deploy Mannequin

As you’ll be able to see, the merge deploy mannequin is inherently riskier as a result of the primary department is rarely actually a steady department. If a deployment fails, or we have to roll again, we comply with all the course of once more to roll again our adjustments. Nonetheless, within the department deploy mannequin, the primary department is at all times in a “good” state and we are able to deploy it at any time to revert the deployment from a department deploy. Within the department deploy mannequin, we solely merge our adjustments into primary the department as soon as it has been efficiently deployed and validated.

Notice: That is typically known as the GitHub Flow

Key Ideas of the department deploy mannequin:

  • The primary department is at all times thought of to be a steady and deployable department
  • All adjustments are deployed to manufacturing earlier than they’re merged to the primary department
  • To roll again a department deployment, you deploy the primary department

Okay, so by now you’re hopefully bought on the department deploy methodology. However how can we implement it? Introducing… IssueOps!

The easiest way to outline IssueOps is to match it to one thing comparable, ChatOps. You might be accustomed to the idea ChatOps already however in case you aren’t here’s a fast definition beneath:

ChatOps is the method of interacting with a chat bot to execute instructions immediately in a chat platform. For instance, with ChatOps you would possibly do one thing like .ping to examine the standing of an internet site

IssueOps adopts the identical mindset however by way of a distinct medium. Reasonably than utilizing a chat service (Discord, Slack, and so on.) to invoke the instructions we use feedback on a GitHub Situation or Pull Request. GitHub Actions is the runtime that executes our desired logic when an IssueOps command is invoked.

How does it work?

This part will go into element about how this Motion works and hopefully encourage you on methods you’ll be able to leverage it in your individual tasks.

The total supply code and additional documentation might be discovered on GitHub

Create this file underneath .github/workflows/branch-deploy.yml in your GitHub repository

Let’s stroll by way of a GitHub Motion workflow utilizing this Action line by line:

Phase #1

You will need to notice that the workflow we need to run IssueOps on is issue_comment and created.

This implies we is not going to run underneath every other contexts for this workflow. You may edit this as you would like nevertheless it does change how this mannequin finally works.

For instance, issue_comment workflows solely use recordsdata discovered on primary to run. When you do one thing like on: pull_request you possibly can open your self as much as points as a consumer might alter a file in a PR and exfil your secrets and techniques for instance.

Solely utilizing issue_comment is the steered workflow sort

Phase #2

These are the minimal permissions you’ll want to run this Motion

Phase #3

Units up your demo job, makes use of an ubuntu runner, and checks out your repo – Just a few normal setup for a normal Motion

Phase #4

Notice: You will need to set an id: for this job so we are able to reference its outputs in subsequent steps — You may see a full checklist of inputs and outputs the Motion takes here

The core of this Motion takes place right here. This block of code will set off the department deploy motion to run. It can do the next:

  1. Verify the remark which invoked the workflow for the set off: phrase (.deploy) outlined right here
  2. If the set off phrase is discovered, it would proceed with a deployment
  3. It can begin by reacting to your message to let you recognize it’s working
  4. The Motion will put up a remark with a hyperlink to the working Actions workflow so that you can comply with its progress
  5. Deployment shall be began and connected to your pull request — You’ll get a pleasant little yellow rocket that tells you deployment is in progress
  6. Outputs shall be exported by this job for later reference in different jobs as nicely
Phase #5

As seen above, we now have two steps. One for a noop deploy, and one for an everyday deploy. For instance, the noop deploy might set off a terraform plan and the common deploy may very well be a terraform apply. These steps are conditionally gated by two variables:

  • steps.branch-deploy.outputs.proceed == 'true' – The proceed variable is simply set to true when deployment ought to proceed
  • steps.branch-deploy.outputs.noop == 'true' – The noop variable is simply set to true when a noop deployment needs to be run

Instance: You remark .deploy noop on a pull request. A noop deployment is detected so this motion outputs the noop variable to true. You even have the proper permissions to execute the IssueOps command so the motion additionally outputs the proceed variable to true. This may permit the “faux noop deploy” step seen above to run and the “faux common deploy” step shall be skipped

That’s it!

When you want to study extra about organising this Motion and all of the configuration choices obtainable, you’ll be able to view the Motion on the GitHub Market: link

Notice: It’s also possible to discover all code and a full workflow instance with the hyperlink referenced above

The instance beneath demonstrates utilizing the branch-deploy Motion on a pull request

Department Deploy Demo

In case you are trying to improve your DevOps expertise, have higher reliability in your deployments, or ship adjustments quicker, then branch-deployments are for you!

Hopefully you now have a greater understanding of why the branch-deploy mannequin is a good possibility for delivery your code to manufacturing.

Through the use of GitHub + Actions + IssueOps you’ll be able to leverage the department deploy mannequin in any repo!

Supply code: GitHub

More Posts