Development and Production Environments with GitHub flow
Asked Answered
O

5

12

At work, we're now using GitHub, and with that GitHub flow. My understanding of GitHub flow is that there is a master branch and feature branches. Unlike git flow, there is no develop branch.

This works quite well on projects that we've done, and simplifies things.

However, for our products, we have a development and production environment. For the production environment, we use the master branch, whereas for the development environment we're not sure how to do it?

The only idea I can think of is:

  1. When a branch is merged with master, redeploy master using GitHub actions.
  2. When another branch is pushed, set up a GitHub action so that any other branch (other than master) is deployed to this environment.

Currently, for projects that require a development environment, we're essentially using git flow (features -> develop -> master).

Do you think my idea is sensible, and if not what would you recommend?

Edit:

Just to clarify, I'm asking the best way to implement development with GitHub Flow and not git flow.

Osteen answered 22/5, 2020 at 15:25 Comment(0)
H
12

In my experience, GitHub Flow with multiple environments works like this. Merging to master does not automatically deploy to production. Instead, merging to master creates a build artifact that is able to be promoted through environments using ChatOps tooling.

For example, pushing to master creates a build artifact named something like my-service-47cbd6c, which is a combination of the service name and the short commit hash. This is pushed to an artifact repository of some kind. The artifact can then be deployed to various environments using tooling such as ChatOps style slash commands to trigger the deloy. This tooling could also have checks to make sure test environments are not skipped, for example. Finally, the artifact is promoted to production.

So for your use case with GitHub Actions, what I would suggest is this:

  1. Pushing to master creates the build artifact and automatically deploys it to the development environment.
  2. Test in development
  3. Promote the artifact by deploying to production using a slash command. The action slash-command-dispatch would help you with this.
Hali answered 23/5, 2020 at 5:13 Comment(3)
thank you! Yes, this is what I was looking for, this outlook is interesting and will feed this back.Osteen
@Hali disclaimer - I've never worked with GitHub flow, mainly with Git Flow or something similar. But most of the sources about GitHub flow says deployment to production and testing should happen before merging to master. For example #39918343 or guides.github.com/introduction/flow. In your flow you start testing only after pushing to masterCarduaceous
@Carduaceous I only have experience in fairly large orgs where deploying after merge to master is the norm. Allowing deploy of feature branches to production sounds a bit dangerous to me, unless it's carefully managed. I would say, though, don't feel you have to follow "the rules." Experiment a bit and do what works for you and your team.Hali
L
4

You might also consider the notion of environments (as illustrated here)

Recently (Feb. 2021), you can:

##Limit which branches can deploy to an environment

You can now limit which branches can deploy to an environment using Environment protection rules.

When a job tries to deploy to an environment with Deployment branches configured Actions will check the value of github.ref against the configuration and if it does not match the job will fail and the run will stop.

The Deployment branches rule can be configured to allow:

  • All branches – Any branch in the repository can deploy
  • Protected branches – Only branches with protection rules
  • Selected branches – Branches matching a set of name patterns

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpLS21bC2jaRA/user-images.githubusercontent.com/185122/107719397-253c1e80-6ca6-11eb-9a5c-5d6fc7d4668e.gif?ssl=1

That means you can define a job to deploy in dev environment, and that job, as a condition, will only run if triggered from a commit pushed from a given branch (master in your case)

Lavonnelaw answered 17/2, 2021 at 19:38 Comment(0)
I
2

For anyone facing the same question or wanting to simplify their process away from gitflow, I'd recommend taking a look at this article. Whilst it doesn't talk about Github flow explicitly it does effectively provide one solution to the OP.

Purests may consider this to be not strictly Gitflow but to my mind it's a simple tweak that makes the deployment & CI/CD strategy more explicit in git. I prefer to have this approach rather than add some magic to the tooling which can make a process harder for devs to follow and understand.

I think the Gitflow intro is written fairly pragmatically as well:

Different teams may have different deployment strategies. For some, it may be best to deploy to a specially provisioned testing environment. For others, deploying directly to production may be the better choice...

The diagram in the article sums it up well:

enter image description here

So here we have Master == Gitflow main and the useful addition is the temporary release branch from which you can deploy to other environments such as development. What is worth considering is what you choose to call this temporary branch, in the above it's considered a release, in your process it may be a test branch, etc.

You can take or leave the squashing and tagging and the tooling will change between teams. Equally you may or may not care about actual version numbers.

This isn't a million miles away from VonC's answer, the difference is the process is more tightly defined and it's more towards having multiple developers merge into a single branch & apply fixes in order to get a new version ready for production. It may well be that you configure the deployment of this temporary branch via a naming convention as in his answer.

Indefinable answered 28/9, 2021 at 10:21 Comment(0)
M
2

The way I've implemented this flow is using PRs. I did it with Azure DevOps, but I'd say that the same can be achieved with GitHub Actions.

When you have a branch that you intent to test and eventually merge to master and release to production, you create a PR from that branch to master. The PR will trigger a pipeline, which will run your build, static analysis and tests. If that passes, the PR is deployed to a test environment where further automated and manual testing can happen. That PR can be reviewed and approved by other developers and, if you need to, by QA after manual testing. You can configure GitHub PR rules to enforce the approvals. Once approved, you can merge the PR to master.

What happens once in master is independent of the workflow above, but most likely a new pipeline will be triggered, which will build a release candidate and run the whole path to production (with or without manual intervention).

One of the tricks is how the PR pipeline decides which environment to deploy the PR too. I can think of three options:

  1. Create an environment on the fly which will be killed once the PR is merged or closed. This is the most advanced and flexible option. This would require the system to publish the environment location to the PR.

  2. Have a pool of environments and have the automation figure out which are free and automatically choose one. The environments could be stopped, so you find an environment which is stopped, start it up and deploy there. Once the PR is closed/merged, stop the environment again.You can publish the environment location to the PR.

  3. Add a label to the PR indicating the environment (ie. env-1, env-2, etc.). This is the simplest option, but it requires that developers look at the open PRs to see which environments are already in use in other PRs to avoid overwriting other people's code.

With all these options, once the PR is created, you can just push new commits to the branch and the environment will be updated.

You also need to decide what you want to do when a new commit is pushed to master. You most likely want to trigger a new PR build to update the environments with the latest master, but you can do this automatically or manually, depending on how busy your master is.

Manteltree answered 13/1, 2023 at 7:52 Comment(0)
D
-3

Nathan, adding a development branch is good idea, you can work on development changes in new branch and test them in dev environment and after getting signoff to move to production environment you can merge your changes in master branch.

Don't forget to perform regression testing on merged master branch to test both old features and new features are working fine before releasing your code for installation in production

Dubenko answered 22/5, 2020 at 16:17 Comment(1)
Hi Gabriel, appreciate the resposne. However a development branch isn't featured in GitHub Flow, just git flow. So my question is more about how to create a development environment using GitHub Flow (where there is no sepcific development branch). Hence > When another branch is pushed, set up a GitHub action so that any other branch (other than master) is deployed to this environment.Osteen

© 2022 - 2024 — McMap. All rights reserved.