How to avoid git failures when follow-on Workflows are used?

Our project has the following workflow setup:

  1. Library workflow
  2. iOS Sample workflow
  3. macOS Sample workflow

A git push will trigger the Library workflow, which chains the iOS Sample and macOS Sample workflows as a follow-on.

The reason we’ve split it up is so that each workflow represents independant concerns, and could be run separately.

However, when the workflows are chained, they are not run in isolation, and the git step will fail on subsequent workflows for pull requests (but not when building off master)

The error is:

Git clone repository
=> git "remote" "-v"
=> git "reset" "--hard" "HEAD"
=> git "clean" "-xdf"
=> git "submodule" "foreach" "git" "reset" "--hard" "HEAD"
=> git "submodule" "foreach" "git" "clean" "-xdf"
=> git "init"
=> git "fetch" "origin" "pull/87/merge:pull/87"
0 attempt failed:
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
fatal: Refusing to fetch into current branch refs/heads/pull/87 of non-bare repository

Retrying...
=> git "fetch" "origin" "pull/87/merge:pull/87"
1 attempt failed:
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
fatal: Refusing to fetch into current branch refs/heads/pull/87 of non-bare repository

Retrying...
=> git "fetch" "origin" "pull/87/merge:pull/87"
2 attempt failed:
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
fatal: Refusing to fetch into current branch refs/heads/pull/87 of non-bare repository

Failed, error: Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
fatal: Refusing to fetch into current branch refs/heads/pull/87 of non-bare repository

Deleting the git step in the follow-on workflows resolves the issue (it’s an unnecessary step when run in that sequence), but then they can’t be run independently.

What would you recommend? Can the git workflow be updated to handle this gracefully? Is there a way we should reset the working directory before each workflow, or should we move all git setup steps to “parent” workflows designed to be run independently, and chain to workflows that are only used as follow-on workflows?

1 Like

Hi @wdenniss,

Thanks for asking this here!

In general: You should only use a single Git Clone step, to clone the source code repository. If you do additional git clones you should use a Script step, or else the Git Clone step might get parameters for the clone for an unrelated repository.

In your case it’s a simple solution if it works otherwise / when you don’t do more than one Git Clone: have a single “setup” workflow, and reference / chain it with your other workflows.

A simple example for what you want to do:

workflows:
  setup:
    steps:
    - git-clone: {}

  build-library:
    before_run:
    - setup
    steps:
    - ...

  build-ios:
    before_run:
    - setup
    steps:
    - ...

This way when you run e.g. build-ios it’ll first run the setup workflow, including the Git Clone step, then it’ll proceed with the build-ios specific steps.

The point is that you can reference other workflows with before_run and after_run in the workflow’s definition. You can also have multiple before and after runs, and a workflow you run through before run can also have before and after run definitions, unless there’s a circular dependency between workflows. Note: This is also available on the Workflow Editor UI, but it’s easier to demonstrate in YML format.

You can find more information about Workflow Chaining on our DevCenter.

If you need more info or any help just let us know!