【问题标题】:Get the commit hash of the latest successful workflow run from within the current workflow run in GitHub Actions?从 GitHub Actions 中运行的当前工作流中获取最新成功工作流运行的提交哈希?
【发布时间】:2020-06-14 19:20:40
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: git github-actions


    【解决方案1】:

    实际上我自己通过使用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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2020-08-20
      • 2022-01-13
      • 2021-01-29
      • 2020-03-20
      • 2020-04-02
      • 2023-01-05
      相关资源
      最近更新 更多