【问题标题】:Create Git Log using Gitlab CI使用 Gitlab CI 创建 Git 日志
【发布时间】:2019-05-24 11:33:51
【问题描述】:

当用户将代码提交到 git 仓库时,我们需要自动生成提交历史文件。

可以使用 Jenkins、Gitlab Webhooks 和 Jenkins Git Changelog 插件来完成。此外,它可以使用下面的 git 命令创建。

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

但是,我们是否可以使用 Gitlab CI/CD 操作生成提交历史文件。该文件可以保存在 git 存储库或本地存储中。

提交历史文件示例

* 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

【问题讨论】:

    标签: gitlab gitlab-ci git-commit git-log git-webhooks


    【解决方案1】:

    我确信在 GitLab 中有多种方法可以做到这一点。这是一个:

    1. 在存储库的根目录下创建一个.gitlab-ci.yaml 文件。您可以在本地执行此操作,也可以使用 GitLab 的 Web UI。
    2. 将此 sn-p 粘贴到您的 .gitlab-ci.yaml 文件中:

      changelog:
        image: docker:git
        script:
          - git log --pretty=format:'At %ci, %cN committed %h - %s' --decorate --graph >log.log
        artifacts:
          paths: [log.log]
      
    3. 要么在本地提交并推送,要么在 GitLab 的 Web UI 上提交。 changelog 作业将被触发。

    4. 作业成功完成后,您的log.log 文件将作为changelog 作业的工件提供

    基本上,通过这个 sn-p,您可以将 GitLab 的 CI/CD 系统设置为:

    • Docker executor 与预安装了git 的Docker 映像一起使用
    • 定义一个 changelog job 来运行你的 git 命令
    • 定义一个log.logartifact,它将作为作业的一部分生成并存储,以便以后下载。

    我还建议您查看GitLab CI/CD quickstart

    【讨论】:

    • 但是,我们有没有办法更新 gitlab 存储库中的日志文件?或者除了将日志文件保存在服务器或工件中之外的其他方式?
    【解决方案2】:

    Jenkins plugin 使用的library 还有一个command line tool 可以在任何地方使用:

    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}}
    "
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2021-12-27
      • 2020-01-08
      • 2019-05-01
      • 2021-03-21
      • 2018-08-30
      • 1970-01-01
      相关资源
      最近更新 更多