Science and technology

How I take advantage of the Git for-each-ref command for DevOps

For most of immediately’s builders, utilizing Git is akin to respiratory, in you can’t dwell with out it. Along with model management, Git’s use has even expanded in recent times into the realm of GitOps, or managing and versioning configurations by way of Git. What lots of customers do not realize or take into consideration is that Git tracks not solely file modifications for every commit but additionally lots of meta-data round commits and branches. Your DevOps can leverage this information or automate IT operations utilizing software program improvement greatest practices, resembling with CI/CD.

In my case, I take advantage of an automatic course of (DevOps) whereby a brand new department is created each time I promote a picture right into a downstream CI/CD surroundings (namespace) in Kubernetes (here’s a shameless plug of my Opensource.com article describing the method). This permits me to switch the deployment descriptors for a selected deployment in a downstream CI/CD surroundings unbiased of the opposite environments and permits me to model these modifications (GitOps).

I’ll talk about a typical situation the place a breaking bug is found in QA, and nobody is certain which construct launched the bug. I can not and do not need to depend on picture meta-data to seek out the department in Git that holds the right deployment descriptors for a number of causes, particularly contemplating I may have to go looking one native repository or a number of distant photographs. So how can I simply leverage the data in a Git repository to seek out what I’m in search of?

Use the for-each-ref command

This situation is the place the for-each-ref command is of some actual use. It permits me to go looking all my Git repository’s branches filtered by the naming conference I take advantage of (an excellent motive to implement naming conventions when creating branches) and returns essentially the most just lately modified branches in descending type order. For instance:

$ git clone git@github.com:elcicd/Test-CICD1.git
$ cd Test-CICD1
$ git for-each-ref --format='%(refname:quick) (%(committerdate))'
                   --sort='-committerdate'
                   'refs/remotes/**/deployment-qa-*'
origin/deployment-qa-c6e94a5 (Wed May 12 19:40:46 2021 -0500)
origin/deployment-qa-b70b438 (Fri Apr 23 15:42:30 2021 -0500)
origin/deployment-qa-347fc1d (Thu Apr 15 17:11:25 2021 -0500)
origin/deployment-qa-c1df9dd (Wed Apr 7 11:10:32 2021 -0500)
origin/deployment-qa-260f8f1 (Tue Apr 6 15:50:05 2021 -0500)

The instructions above clone a repository I typically use to check Kubernetes deployments. I then use git for-each-ref to go looking the branches by the date of the final commit, prohibit the search to the branches that match the deployment department naming conference for the QA surroundings, and return the latest 5. These roughly (i.e., not essentially, however shut sufficient) correspond to the final 5 variations of the part/microservice I need to redeploy.

deployment-qa-* is predicated on the naming conference:

<descriptive-prefix>-<deployment-env>-<dev-branch-commit-hash>

The info returned can be utilized by builders or QA personnel when working the CI/CD redeployment pipeline to resolve what model to roll again/ahead to in a Kubernetes namespace and thus ultimately return to a identified good state. This course of narrows down when and what launched the breaking bug within the contrived situation.

While the naming conference and situation above are specific to wants and automatic CI/CD processes, there are different, extra typically helpful methods to make use of for-each-ref. Many organizations have department naming conventions much like the next:

<model>-<feature-or-bug>-<feature-or-bug-id>

The ID worth refers back to the ID describing the characteristic or bug in a mission administration system like Rally or Jira; e.g.:

v1.23-feature-12345

This ID permits customers to simply and shortly get some added visibility into the better improvement historical past of the repository and mission (utilizing refs/remotes/**/v.123-feature-*), relying on the event course of and department naming conference insurance policies. The course of works on tags, too, so itemizing out the newest pre-prod, prod, or different particular variations may very well be achieved virtually as simply (not all tags are pulled by default).

Wrap up

These are solely specific and slim examples of utilizing the for-each-ref. From authors to commit messages, the official documentation supplies perception into many particulars that may be searched, filtered, and reported.

Most Popular

To Top