简体   繁体   English

从 GitHub 操作中运行的当前工作流中获取最新成功运行的工作流的提交 hash?

[英]Get the commit hash of the latest successful workflow run from within the current workflow run in GitHub Actions?

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:我正在 GitHub 存储库中编写一个 LaTeX 文档,我想使用git-latexdiff编译一个 pdf 文件,该文件显示HEAD与最后一次成功运行的工作流的提交 hash 之间的差异:

git latexdiff "$LAST_SUCCESSFUL_COMMIT_HASH" HEAD --no-view -o diff.pdf

So I need a way to access the commit hash of the latest successful workflow run from within the current workflow run.因此,我需要一种方法来访问当前工作流运行中最新成功运行的工作流的提交 hash。

I didn't find anything in the docs but maybe there is a workaround?我在文档中没有找到任何内容,但也许有解决方法?

I actually solved the problem myself by writing a small GitHub Action using the GitHub Actions API :我实际上通过使用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);
}

This is in Powershell这是在 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: 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

So I need a way to access the commit hash of the latest successful workflow run from within the current workflow run.因此,我需要一种从当前工作流运行中访问最新成功工作流运行的提交 hash 的方法。

I didn't find anything in the docs but maybe there is a workaround?我在文档中没有找到任何内容,但也许有解决方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Github 操作:在 workflow_run 上读取分支更改 - Github actions: read branch changes on workflow_run 如何在每次提交推送时在 GitHub 工作流中运行 commitlint - How to run commitlint in GitHub workflow on every commit of a push 获取当前分支上带有最新标签的提交哈希 - Get hash of commit with latest tag on current branch 如何使用 github 操作从 Github 中的存储库 A 的工作流作业触发存储库 B(下游作业)中的工作流作业 - How to trigger a workflow job in repository B(downstream job) from workflow job of repository A in Github using github actions 如何使用个人访问令牌在 github 操作工作流 (B) 中从不同的存储库 (B) 提交和推送到私有存储库 (A) - How to commit and push to a private repo(A), from a different repo(B), in github actions workflow (B) , using personal access token 从输入验证提交 hash (workflow_dispatch) - Validate commit hash from the input (workflow_dispatch) 如何从远程获取最新的提交哈希? - How to get latest commit hash from remote? 简单的 GitHub 工作流程开始使用 - Simple GitHub workflow to get started with 从当前提交中获取最新的git标记 - Get latest git tag from the current commit 如何使用 Github 操作检查最新提交? - How to checkout the latest commit with Github Actions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM