Home >Technology peripherals >It Industry >How to Use Git Branches & Buddy to Organize Project Code
This article is created in collaboration with Buddy. Thank you for supporting the partners who made SitePoint possible.
This article will guide you on how to set up a continuous integration/deployment pipeline for branch workflows. We will use the Buddy CI/CD service to set up these pipelines. We will use a basic JavaScript project where we will set up several development branches. I'll show you how to automate tests on each type of branch. I'll also introduce the concept of branch workflow and show some examples you can take in your project.
To learn this tutorial, you only need basic Node.js skills. You also need to be familiar with Git. Here are some articles that can help you:
In order to set up our pipeline, we will need to write some tests using Jest. If you are not familiar with Jest, you don't need to learn it - the focus of this article is to learn how to set up a pipeline that will automatically select new branches and build them for you. Before we get started, we should look at the various branching strategies we can use.
Zero branch strategy is just a saying that "you are not using any branch strategy". It is also known as the basic workflow. You only have one master branch where you can commit and build your version directly. This strategy is convenient and good if the project meets the following conditions:
This type of projects includes tutorials, demonstrations, prototypes, introductory project templates, and personal projects. However, this method has several disadvantages:
All of these problems can be solved by adopting branching strategies. This should provide you with:
Please note that you are free to choose from many types of branch workflows. You can also create custom branch workflows that suit you. Let's start with the simplest branching strategy.
In this policy, you will set up a long-term branch called develop that runs in parallel with the main branch. All work is first committed to the develop branch. This is a safe place where you can introduce code that can break your project. You need a test strategy to ensure that errors are not introduced into the main branch when the changes are merged.
The advantages of this workflow are:
The disadvantages of this workflow are:
Let's look at another workflow that can alleviate these challenges.
In this workflow, you set up a new feature branch every time you want to develop a new feature. If there is a problem, you can always apply hot fixes on the main branch. Developers need to extract the latest fix from the main branch before they can merge their feature branches into the main branch.
To keep track of the features and bug fixes currently under development, you need to make a naming convention for the branch. Here are some format suggestions you can find on the internet:
The advantages of this strategy are:
The disadvantages of this strategy are:
Let's look at the next strategy and see how we can mitigate this problem.
If you can combine the "development" and "features" branch workflows, you will get a solution that can eliminate each other's shortcomings. Vincent Driessen wrote a blog post that describes an advanced git branching model that helps large teams collaborate efficiently on complex projects and minimize version control issues.
Gitflow is a customizable model that allows you to choose the features that best suit your project and team. If you use Gitflow, you can take Daniel Kummer's git extension for git. These tools allow developers to perform advanced repository operations based on Vincent's model. I won't go into this in depth, but here are some things you need to know.
Pros:
Disadvantages:
Now let's see how to use the Buddy CI service to automate tasks on our branch.
We first need to set up a simple project and use it to set up our pipeline. We will create a pipeline that automatically extracts changes and runs tests. First, create a new GitHub repository. Name it buddy-demo.
Next, download the following entry project and push it to your repository:
<code>$ git clone git@github.com:brandiqa/react-parcel-starter.git buddy-demo $ git remote rm origin # 将`username`替换为您自己的用户名 $ git remote add origin git@github.com:username/buddy-demo.git $ git config master.remote origin $ git config master.merge refs/heads/master $ git push -u origin master </code>
This project is a simple React project built using Parcel. You can run the following command to make sure it is running:
<code>$ npm install $ npm start </code>
If you are using Visual Studio Code, press F5 to launch the browser. Otherwise, open the browser page and navigate to localhost:1234.
As you can see, nothing special. Before we deploy it to Buddy CI, we need to write a test. We will use the Jest test framework for this:
<code>$ git clone git@github.com:brandiqa/react-parcel-starter.git buddy-demo $ git remote rm origin # 将`username`替换为您自己的用户名 $ git remote add origin git@github.com:username/buddy-demo.git $ git config master.remote origin $ git config master.merge refs/heads/master $ git push -u origin master </code>
Update the package.json script section to run jest when the npm test command is executed.
<code>$ npm install $ npm start </code>
Let's update a little bit srcApp.jsx:
<code>$ npm install -D jest </code>
Next, let's write a passable test. Create the file App.test.js and insert this code:
<code> "scripts": { //... "test": "jest" }, </code>
Execute npm test command to confirm that our test has passed.
Submit your changes and push them to your GitHub repository. Next, we will set up our CI pipeline on Buddy. If you are not familiar with the platform, just sign up for a free account using your GitHub account. Please note that Buddy supports many remote repository services other than GitHub:
No matter which service provider you choose, Buddy lists repositories for which you can set up automation. In this example, we will select the buddy-demo project. Click the "Add New Pipeline" button and fill in the following details on the next page:
In our main branch pipeline, we will set up actions for:
In the next page, you will see different ways to define the action. Select Node.js, and on the next page, make sure the following command is specified:
<code><div>> <h1>></h1>React Parcel Starter Kit> <p>></p>This page is on master branch!> </div>> </code>
You can rename the operation name to Run Test in the Actions tab. What I want to point out is that if your test requires database services, you can set one through the Services tab:
Most popular databases are already supported. Just select the database type and provide connection details and credentials. When finished, click the Add This button. On the next page, click the plus button at the bottom to add the Bundle Resource action. Select Node.js again and enter the following command on the next page:
<code>$ git clone git@github.com:brandiqa/react-parcel-starter.git buddy-demo $ git remote rm origin # 将`username`替换为您自己的用户名 $ git remote add origin git@github.com:username/buddy-demo.git $ git config master.remote origin $ git config master.merge refs/heads/master $ git push -u origin master </code>
Rename the action to Bundle Resources in the Actions tab. Click Add this when you are finished. Click the plus sign again to add the Deploy to Production action. Buddy native support for deploying projects to different types of hosting vendors:
If you have an account in any of these services, feel free to use any deployment options. If you don't have one, select a provider that allows you to set up a free account to deploy your application. In my case, I already have a shared web hosting plan account that I can use. Typically, you will have your main website www.domainname.com to host a live production version of your project.
You need to have a separate staging site (usually hidden from the public) that is deployed from your development or integration branch pipeline. A staging site can be just a subdomain and search engines should not index it. The staging site will allow developers, project managers, and testers to confirm that new features are working properly before pushing to the live production site.
To deploy your application to a shared or dedicated web hosting server (using CPanel), just use the FTP method. Buddy also provides an sFTP method that encrypts your project resource package when uploaded to the server. Here is an example of how I set up my:
You need to set up a new FTP account using your CPanel. Make sure that the home directory of your new FTP user account points directly to the www or subdomain folder. Otherwise, you may not be able to access the correct managed directory via FTP. After setting up all three operations in the pipeline, you can:
After completion, the complete pipeline looks like this:
Suppose you are using a Gitflow workflow or something similar, you may need to set up another pipeline for:
The development branch pipeline is almost the same as the main branch pipeline. However, you need to provide a different configuration for the deployment in order to deploy the code to the staging site. Functional and hot repair branch pipes only need to be configured at least for testing operations. You may want to limit the number of tests you can run in the feature branch pipeline. You can easily do this in Jest by simply adding this to the test command: jest --coverage --changedSince=master. This will only test new code that has not been pushed to the main branch.
Since there will be multiple features and hot fix branches, you may want to know how to set up a pipeline for this situation. Very simple - just use the wildcard option:
To confirm that your develop/feature*/hotfix* pipeline is working, just create a branch on your computer. In this example, let's create a random feature branch:
<code>$ git clone git@github.com:brandiqa/react-parcel-starter.git buddy-demo $ git remote rm origin # 将`username`替换为您自己的用户名 $ git remote add origin git@github.com:username/buddy-demo.git $ git config master.remote origin $ git config master.merge refs/heads/master $ git push -u origin master </code>
Then create a new test in App.test.js:
<code>$ npm install $ npm start </code>
Next, commit the changes and push the branch to your GitHub repository:
<code>$ npm install -D jest </code>
If you quickly switch to your Buddy account dashboard, you should see your pipeline pick up your new branch and run the action you defined. This is how we set up pipelines for any branch policy workflow that the project has adopted.
The last thing to note is that if you plan to have long-term branches, it is best to set them up in the shared repository first. This way, when you start creating a new pipeline, you can simply use the Select Branch option to select your long-term branch.
We have now completed this tutorial. As a challenge, continue to set up pipelines for hot repairs and development. Create some branches and write some failed tests to see what happens. You can also continue to research more about Git branching strategies. You can even install git-flow and use the tool to customize your own branch workflow. Then, set up your Buddy pipeline to support your custom git branch workflow.
Git branch is a critical part of any software development process. They allow developers to handle different features or bug fixes simultaneously without affecting the main code base. This means developers can experiment with new ideas in a secure environment without risking breaking existing code. If the new feature or bug fix is successful, you can merge it back to the main code base. This makes the development process more efficient and reduces the risk of introducing errors into production code.
Creating a new branch in Git is simple. You can use the git branch command followed by the name of the new branch. For example, git branch new-feature will create a new branch called "new-feature". After creating a branch, you can switch to that branch using the git checkout command as follows: git checkout new-feature.
Merging changes from one branch to another is done in Git using the git merge command. First, you need to switch to the branch you want to merge the changes to. This can be done using the git checkout command. Once you are on the correct branch, you can use git merge
Git branch conflict occurs when two or more developers make changes to the same part of the code base in different branches and then try to merge those changes. Git doesn't know what changes to keep and what changes to discard, resulting in conflicts. To resolve conflicts, you need to manually edit the conflicting files to decide which changes to keep. After the conflict is resolved, you can use git add to add the resolved files to the staging area and then use git commit to commit the changes.
Deleting branches in Git is done using the git branch -d command followed by the name of the branch. For example, git branch -d old-feature will delete the branch named "old-feature". However, if the branch has changes that have not been merged, Git will not allow you to delete the branch. If you are sure you want to delete branches and lose these changes, you can use the -D option instead, as shown below: git branch -D old-feature.
You can use the git branch command (without any parameters) to view all branches in the Git repository. This lists all branches in the repository, the current branch is highlighted and marked with an asterisk.
The local branch in Git is a branch that only exists on the local machine, while the remote branch is a branch that exists on the remote repository. When cloning the repository, Git creates local branches for all remote branches. You can handle these local branches and then push changes to the remote branch when ready.
Renaming a Git branch is done using the git branch -m command followed by the old branch name and the new branch name. For example, git branch -m old-name new-name will rename the branch "old-name" to "new-name". If you are currently on the branch you want to rename, you can omit the old branch name as follows: git branch -m new-name.
You can use the git revert command followed by a commit hash to restore changes in the Git branch. This creates a new commit, undoing changes made in the specified commit. For example, git revert a867b4af will create a new commit, undoing changes made in a commit that hashed to "a867b4af".
You can use the git log command to view the commit history of the Git branch. This will display a list of all commits made on the current branch in reverse chronological order. If you want to view the commit history for different branches, you can specify the branch name as follows: git log branch-name.
The above is the detailed content of How to Use Git Branches & Buddy to Organize Project Code. For more information, please follow other related articles on the PHP Chinese website!