git - 如何使用 Github Actions 查看最新提交?

标签 git github continuous-integration git-checkout github-actions

我创建了一个新的 Rust 项目并决定,我将给出 Github Actions尝试对每个 pull 请求运行自动化构建和测试:

name: Rust
on: [pull_request]

我花了一段时间才注意到,默认情况下,Github Actions 根本不 checkout 我的代码,并且 GITHUB_WORKSPACE 是空的。因此,我尝试手动克隆存储库。做这样的事情:

REPO=/tmp/path/to/repository
git clone https://github.com/myself/mycode.git $REPO

但这只是检查默认分支上的内容。因此,我调查了 $GITHUB_SHA ,结果发现这是我的存储库未知的东西。对于空的 $GITHUB_REF 也是如此。

此时我对自己在做什么一无所知。我最初的假设是,一个被字面配置为在 [pull request] 上运行的作业应该具有完全相同的代码,但它无法 checkout 并准备它。

我还调查了提供的 Checkout Actions :

This action checks out your repository to $GITHUB_WORKSPACE, so that your workflow can access the contents of your repository.

By default, this is equivalent to running git fetch and git checkout $GITHUB_SHA, so that you'll always have your repo contents at the version that triggered the workflow. See here to learn what $GITHUB_SHA is for different kinds of events.

但正如我之前所说,$GITHUB_WORKSPACE 完全是空的,git fetch 只会告诉您没有 git 存储库。

这是这样一个example failure :

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
fatal: reference is not a tree: d76745134ae45905e4a0ab8d27c92f1e2544bdc1
##[error]Process completed with exit code 128.

如果我的存储库未知,$GITHUB_SHA 是什么?我是否完全误解了 Github Actions?如何使用 Github Actions(即 pull 请求)检查最新提交?

这是chronology of my failures .

最佳答案

您应该使用official action处理 checkout 。 github-actions 的做事方式需要一些时间来适应,因为它是一个项目管理套件,不仅仅满足 CI/CD 需求。

所以,有些事情肯定会有点奇怪或麻烦,最重要的是因为所有这些的文档还不是很成熟 - 但是嘿,这是一个测试版是有原因的。

该 Action 的基本用法是:

  • 找到您想要 checkout 当前提交的steps部分
  • 在编写依赖于代码的步骤之前添加操作,通常在最顶部
  • 决定您想要使用哪个版本操作,教程中通常会有@master,但命名当前最新版本会更安全一些 - 在此案例@v1
  • 享受您的工作流程
jobs:
  build:
    runs-on: ubuntu-latest
    steps:

      # may or may not have a name, it's quite self-descriptive
      - uses: actions/checkout@v1

      # run steps that rely on the code in the commit that was pushed
      - name: test code
        steps: ...
      - name: build package
        steps: ...

关于git - 如何使用 Github Actions 查看最新提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58025217/

相关文章:

git - 如何强制从远程服务器 check out 最新版本?

git - Intellij 15 + Github - 无法克隆存储库,出现 "Repository Test has Failed"错误

nginx - Github webhook 的 Jenkins 自动构建不起作用

git - 我如何在 Jenkins 中克隆一个工作?

svn - 使用持续集成构建标签

git 在 zsh TAB 完成中显示旧的已删除分支

git - 如何知道一个提交是否从另一个分支与 gitlab api merge ?

git - git 补丁的上下文

github - 将链接放置在 Markdown 代码块内

docker - 构建器的 Gitlab CI 拉取访问被拒绝,存储库不存在或可能需要 'docker login'