我正在构建一个将要发布到 Netlify 的项目的构建操作。在这个操作中,我可以传递一个部署消息。在该部署消息中,我想传递触发构建的提交的提交消息。我查看了文档,但没有找到是否可能实现此功能。谢谢。
我正在构建一个将要发布到 Netlify 的项目的构建操作。在这个操作中,我可以传递一个部署消息。在该部署消息中,我想传递触发构建的提交的提交消息。我查看了文档,但没有找到是否可能实现此功能。谢谢。
您可以使用以下命令获取具体的提交信息:
github.event.head_commit.message
如果你使用bash,可以使用git log
命令获取提交消息。
git log -1 --pretty=format:"%s"
更新: 关于文档,如果只有一个提交,则负载和提交消息的调用都可以使用commits
数组。在GitHub操作中,可以使用以下行获取消息:
github.event.commits[0].message
这两者之间只有细微差别:
${{ github.event.commits[0].message }}
当 Github 推送事件包含多个提交时,commit[0]
将包含最老的提交。我在合并后看到过这种情况。${{ github.event.head_commit.message }}
另一方面,head_commit
包含了最年轻的提交。
commit-message
可以在以下键中找到:
${{ github.event.commits[0].message }}
${{ github.event.head_commit.message }}
事件中还有很多其他信息可用。 例如,下面的工作流将为您提供所有这些信息:
# .github/workflows/logger.yaml
name: Event Loggger
on: push
jobs:
log-github-event-goodies:
name: "LOG Everything on GitHub Event"
runs-on: ubuntu-latest
steps:
- name: Logging
run: |
echo "${{toJSON(github.event)}}"
echo '${{toJSON(github.event)}}'
。 - hurrahfox如果您尝试从不同的工作流访问,例如:
on:
workflow_run:
workflows: Spec App
branches: master
types: completed // Only runs after spec is completed...
你需要使用:
${{ github.event.workflow_run.head_commit.message }}
我认为我找到了一个相当不错的拉取请求解决方案,并将介绍我是如何找到它的。一旦您知道提交消息,我还将发布您可以执行的操作。知识就是力量!
我的提交消息示例是:the message #@content @login
首先,正如其他人提到的那样,您可以尝试从事件环境变量中获取提交消息。要检查,请尝试:
steps:
- name: Logging
run: |
echo "${{toJSON(github.event)}}"
${{ github.event.head_commit.message }}
然而,如果您无法从事件变量中获取提交消息,则最好的选择是简单的bash。
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set tags env variables.
run: |
# Get your last commit message, not the merge commit.
text=$(git log -1 --no-merges --pretty=%B)
需要注意获取深度,因为GitHub默认只检出头提交,这将是合并提交而不是您想要解析的提交消息。
我还将发布我的最终解决方案,并在其中添加注释,以便在解析提交消息、查找标志并保存“标签”以供稍后在测试命令中运行时进行参考。
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set tags env variables.
run: |
# Set "checking" variable to false by default.
echo "has_tags=false" >> $GITHUB_ENV
# Get your last commit message, not the merge commit.
text=$(git log -1 --no-merges --pretty=%B)
# Read the commit message into an array split by "#".
readarray -d "#" -t commit_message <<< "$text"
# Sanity check.
echo "the tags are: ${commit_message[1]}"
# Add tags and overwrite "checking" variable to true if there are tags.
if [[ "${commit_message[1]}" == *"@"* ]]; then
echo "has_tags=true" >> $GITHUB_ENV
echo "spec_tags=${commit_message[1]}" >> $GITHUB_ENV
fi
然后,稍后如果提交信息具有标志,则可以在步骤中使用变量,如果它们不存在则执行其他操作。
- name: Run tagged tests.
if : ${{ env.has_tags == 'true' }}
run: yarn cy:run --env grepTags="${{ env.spec_tags }}"
- name: Run smoke tests.
if : ${{ env.has_tags == 'false' }}
run: yarn cy:run --env grepTags="@smoke"
我知道这个回答比问题要求的更完整,但我想在你找到提交消息后,你可能想做一些事情,比如有条件地运行不同的工作流步骤。
如果有人因为需要快速解决方案而来到这里,${{ github.event.head_commit.message }}
仍然可以正常使用。供您参考。
这里提供了一个方法,可以在pull_request
事件中获取拉取请求的最新提交消息:
---
name: 'How to get commit message'
on:
pull_request:
types:
- edited
- opened
- synchronize
jobs:
get_message:
name: 'Get commit message'
runs-on: ubuntu-latest
permissions:
# this permission should be set to be able use secrets.GITHUB_TOKEN
id-token: write
# not sure if this permission is necessary, just didn't try to remove it when testing
contents: read
# this permission should be set to be able get commits data by curl request
pull-requests: read
steps:
- name: Get commits
env:
# github URL to list commits
COMMITS_URL: ${{ github.event.pull_request.commits_url }}
run: |
if [ "${COMMITS_URL}x" != "x" ]; then
# get commits list and pick up last one by jq
# caution: only 100 commits will be taken by curl
# if your PR has more than 100 commits
# you have to set page query parameter
# and request last page commits
# API URL details: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-commits-on-a-pull-request
LAST_MSG=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "${COMMITS_URL}?per_page=100" | jq -r .[-1].commit.message)
# set environment variable
echo "LAST_MSG=${LAST_MSG}" >> "${GITHUB_ENV}"
else
echo 'LAST_MSG=' >> "${GITHUB_ENV}"
fi
当 获取提交
步骤完成后,可以从 LAST_MSG
环境变量中获取提交信息
${{ github.event.head_commit.message }}
)非常好,可以获取完整的消息。get-commit-title:
runs-on: ubuntu-latest
outputs:
text: ${{ steps.get_head_commit_title.outputs.title }}
steps:
- name: Get repo
uses: actions/checkout@v3
with:
ref: ${{ github.sha }}
- id: get_head_commit_title
run: echo "title=$(git log --format=%B -n 1 HEAD | head -n 1)" >> $GITHUB_OUTPUT
通过 ${{ needs.get-commit-title.outputs.text }}
,你可以在工作中重新使用其输出。
注意:你可以使用更简单的命令 git show -s --format=%s
替代 git log ... | head
,但是如果你的提交不符合标准的提交语法(标题和注释之间有空行),那么标题和注释将会合并在一行上。
github.event.head_commit.message
。看起来他们已从事件上下文дёе€ 除了该属性。 - Abhinaba Chakraborty${{ github.event.head_commit.message }}
,它仍然包括提交消息,包括正文。 - astrochungithub.event.head_commit
对象。虽然push
事件有该对象:https://docs.github.com/cn/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#push,但是pull_request
事件没有该对象:https://docs.github.com/cn/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request。 - Andrei Andreev