从 GitHub Actions 中当前运行的工作流程中获取最新成功运行的工作流程的提交哈希值?

sky*_*frk 2 git github-actions

我正在 GitHub 存储库中编写 LaTeX 文档,我想使用git-latexdiffHEAD编译一个 pdf 文件,该文件显示上次成功工作流程运行的提交哈希之间的差异:

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

因此,我需要一种方法来从当前工作流程运行中访问最新成功工作流程运行的提交哈希。

我在文档中没有找到任何内容,但也许有解决方法?

sky*_*frk 5

实际上,我自己通过使用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);
}
Run Code Online (Sandbox Code Playgroud)