Create Git Log using Gitlab CI

2.8k Views Asked by At

We need to generate commit history file automatically when user commits code into git repository.

It can be done using Jenkins, Gitlab Webhooks and Jenkins Git Changelog plugin. Also, it can be created using below git command.

$ git log --pretty=format:'At %ci, %cN committed %h : %s' --decorate --graph >log.log

But, is there anyway we can generate commit history file using Gitlab CI/CD operations. The file can be saved in git repositry or local storage.

Sample Commit History File

* At 2018-11-16 18:02:21, kRiZ committed 1714a95 : Commit 4
* At 2018-11-15 16:06:06, kRiZ committed bab5c0c : Commit 3
* At 2018-11-14 18:05:09, kRiZ committed b3c9d86 : Commit 2
* At 2018-11-14 06:47:34, kRiZ committed 8e6ee30 : Add README.md
2

There are 2 best solutions below

1
On BEST ANSWER

I'm sure there are multiple ways of doing this in GitLab. Here's one:

  1. Create a .gitlab-ci.yaml file at the root of your repository. You can do this locally or using GitLab's web UI.
  2. Paste this snippet into your .gitlab-ci.yaml file:

    changelog:
      image: docker:git
      script:
        - git log --pretty=format:'At %ci, %cN committed %h - %s' --decorate --graph >log.log
      artifacts:
        paths: [log.log]
    
  3. Either commit and push locally or commit on GitLab's Web UI. The changelog job will be triggered.

  4. After the job has finished successfully, your log.log file will be available as an artifact of the changelog job

Essentially, with this snippet you are setting up GitLab's CI/CD system to:

  • Use the Docker executor with a Docker image with git preinstalled
  • Define a changelog job that will run your git command
  • Define a log.log artifact that will be generated as part of the job and stored, so that you can download it afterwards.

I'd also recommend checking out the GitLab CI/CD quickstart

0
On

The library used by the Jenkins plugin also has a command line tool that can be used in anywhere:

npx git-changelog-command-line -std -tec "
# Changelog

Changelog for {{ownerName}} {{repoName}}.

{{#tags}}
## {{name}}
 {{#issues}}
  {{#hasIssue}}
   {{#hasLink}}
### {{name}} [{{issue}}]({{link}}) {{title}} {{#hasIssueType}} *{{issueType}}* {{/hasIssueType}} {{#hasLabels}} {{#labels}} *{{.}}* {{/labels}} {{/hasLabels}}
   {{/hasLink}}
   {{^hasLink}}
### {{name}} {{issue}} {{title}} {{#hasIssueType}} *{{issueType}}* {{/hasIssueType}} {{#hasLabels}} {{#labels}} *{{.}}* {{/labels}} {{/hasLabels}}
   {{/hasLink}}
  {{/hasIssue}}
  {{^hasIssue}}
### {{name}}
  {{/hasIssue}}

  {{#commits}}
**{{{messageTitle}}}**

{{#messageBodyItems}}
 * {{.}} 
{{/messageBodyItems}}

[{{hash}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}} *{{commitTime}}*

  {{/commits}}

 {{/issues}}
{{/tags}}
"