簡體   English   中英

檢索 Gitlab CICD 中兩個管道執行之間的所有提交消息

[英]Retrieve all commit messages between two pipeline executions in Gitlab CICD

我有一個 Gitlab CICD 管道,用於構建和部署 Java 應用程序。

當管道運行時,我需要在管道的最后一次執行和當前執行之間獲取所有新的 Git 提交消息。

例如:

先前的管道執行最后一次提交是:

commit 0f22ff72089axxx
   My old commit

從那時起,又有兩個提交:

commit 234ff72089axxx
   My new commit 1

commit 5454422089axxx
   My new commit 2

當管道再次執行時,我需要得到

My new commit 1
My new commit 2

如何才能做到這一點?

您可以使用 hitman 的 API 來獲取有關特定管道的信息

檢查運行時檢查管道 state 時是否應該使用shabefore_sha

您可以使用管道 API來獲取為您的分支運行的管道列表:

GET /projects/:id/pipelines?ref=:branch&scope=finished
  • :id是您在Settings > General中找到的項目 ID。
  • :branch是您的分支名稱,可以從 CI 中預定義的CI_COMMIT_REF_NAME變量中檢索。
  • scope=finished僅排除待處理或正在運行的管道。

這將返回如下所示的對象列表:

[
    {
        "id": 4717,
        "iid": 1563,
        "project_id": 87,
        "sha": "6d019ba714992aca9ee07c24664f04e4d05aac44",
        "ref": "my-branch",
        "status": "success",
        "source": "push",
        "created_at": "2022-09-05T23:24:40.613-07:00",
        "updated_at": "2022-09-05T23:26:31.249-07:00",
        "web_url": "REDACTED"
    },
    ...
]

默認情況下按管道 ID 排序,因此第一項應該是最近完成的管道。 您需要從該項目中獲取sha以在下一步中使用。

然后您可以使用git log來獲取一系列提交的提交消息:

git log --pretty=format:%s --no-merges <previous-sha>..<current-sha>
  • --pretty=format:%s導致git log僅顯示第一行提交消息。
  • --no-merges排除了合並提交,這對我來說似乎是合理的,但您可以刪除。
  • <previous-sha>是我們從 API 響應中檢索到的sha
  • <current-sha>是 CI 中預定義的CI_COMMIT_SHA變量的值。

這將 output 與此類似,提交從最新到最舊排序:

$ git log --pretty=format:%s --no-merges <previous-sha>..<current-sha>
Commit message of commit currently being built
The previous commit message
...

您可以將--reverse標志添加到git log中,以顛倒從最舊到最新提交的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM