[英]Get the commit hash of the latest successful workflow run from within the current workflow run in GitHub Actions?
我正在 GitHub 存储库中编写一个 LaTeX 文档,我想使用git-latexdiff编译一个 pdf 文件,该文件显示HEAD
与最后一次成功运行的工作流的提交 hash 之间的差异:
git latexdiff "$LAST_SUCCESSFUL_COMMIT_HASH" HEAD --no-view -o diff.pdf
因此,我需要一种方法来访问当前工作流运行中最新成功运行的工作流的提交 hash。
我在文档中没有找到任何内容,但也许有解决方法?
我实际上通过使用GitHub Actions API编写一个小的 GitHub Action 自己解决了这个问题:
const core = require('@actions/core');
const github = require('@actions/github');
try {
const octokit = github.getOctokit(core.getInput('github_token'));
octokit.actions.listWorkflowRuns({
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
workflow_id: core.getInput('workflow_id'),
status: "success",
branch: core.getInput('branch'),
event: "push"
}).then( res => {
const headCommits = res.data.workflow_runs.map(run => {return run.head_commit});
const sortedHeadCommits = headCommits.sort( (a, b) => {
const dateA = new Date(a.timestamp);
const dateB = new Date(b.timestamp);
if (dateA < dateB) return -1;
if (dateA > dateB) return 1;
return 0;
});
const lastSuccessCommitHash = sortedHeadCommits[sortedHeadCommits.length -1].id;
core.setOutput("commit_hash", lastSuccessCommitHash)
})
} catch (error) {
core.setFailed(error.message);
}
这是在 Powershell
$url = "https://api.github.com/repos/OWNER/REPO/actions/runs"
$token = "********"
$response = invoke-restMethod $url -headers @{Authorization = "bearer $token"}
$response.workflow_runs | ForEach-Object {
if($_.conclusion -eq "success"){
$last_successful_commit = $_.head_sha
break
}
}
Write-host "Last successful commit is $last_successful_commit"
I'm writing a LaTeX document in a GitHub repository and I want to use git-latexdiff to compile a pdf file which shows the differences between HEAD
and the commit hash of the last successful workflow run:
git latexdiff "$LAST_SUCCESSFUL_COMMIT_HASH" HEAD --no-view -o diff.pdf
因此,我需要一种从当前工作流运行中访问最新成功工作流运行的提交 hash 的方法。
我在文档中没有找到任何内容,但也许有解决方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.