1

What's the easiest way to auto-update a git submodule, without having to manually pull it and commit/push the owning repo update?

I'm using Azure DevOps with yaml. I'm trying to find the best way that when a submodule is committed, the owning repo gets published with the update. Even if I trigger a republish of the owning repo (manually, or automatically), how can I instruct the owning repo to re-pull the submodule?

Would one path be some yaml instruction OR git config to "always pull latest"? That doesn't seem to exist as a submodule seems to only show a specific commit to pull.

I'm hoping I won't have to write an Azure Function to (1) watch the submodule, (2) pull the owner, (3) pull the submodule, (4) commit/push the owner - which already automatically publishes the update. ... but if that's the simplest way, how does one best automate git management?

Jim
  • 804
  • 6
  • 17
  • Does this answer your question? [Is there a way to make git pull automatically update submodules?](https://stackoverflow.com/questions/4611512/is-there-a-way-to-make-git-pull-automatically-update-submodules) – phd Apr 27 '20 at 09:39
  • https://stackoverflow.com/search?q=%5Bgit-submodules%5D+automatic+update – phd Apr 27 '20 at 09:39
  • Thanks - but those references don't fully answer it. Commands are provided to keep a submodule up to date (git submodule update --remote --rebase), but, I'm trying to achieve this automatically from a submod checkin through a master auto-deploy - which could be kicked off by the master simply being re-pushed. But, what could trigger that (with the cmds pulling in submods)? So this comes back to auto-update (not a manual update) and push. – Jim Apr 28 '20 at 20:53

2 Answers2

1

To automatically achieve this, You can create a pipeline for the submodule repo. And set a pipeline trigger in YAML pipeline of the owning repo, which will be triggered on the completion of the submodule's pipeline.

In the YAML pipeline of the owning repo add a script task to run the command git submodule update --remote --rebase. Please check out below detailed steps.

1, Create a pipeline(recommended Classic UI pipeline) with an empty agent job(no tasks), which makes the pipeline always complete with success. And enable continuous integration, so that the commits to the submodule repo will always trigger this pipeline.

enter image description here

2, Add pipeline resources trigger for the YAML pipeline of the main repo. see document Pipeline resource for more information. see below example:

resources:
  pipelines:
  - pipeline: submoduleTriggerPipeline
    source: TestConnection
    trigger: true

3, Add a script task to run git command in the yaml pipeline of the owning repo. See below script example:

steps:
- powershell: |
    cd $(system.defaultworkingdirectory)
    #get the latest submodule source on the agent.
    git submodule update --remote --rebase

    #optionally run below git commands to update the owning repo with the submodule's changes
    git config --global user.email "user@email.com"
    git config --global user.name "name"
    git add .
    git commit -m "commit the submodule's changes to owning repo" 
    git push https://$(System.Accesstoken)@dev.azure.com/azureOrgName/ProjName/_git/submodule HEAD:$(Build.SourceBranchName) -q

So that when commit is made to submodule repo, submodule pipeline(created in the first step) will be triggered and complete successfully, then the pipeline of owning repo will be triggered on the completion of the submodule's pipeline. And then above script task will be executed to pull the latest submodule code and push back to owning repo with the submodule's changes.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
0

In extension to Levi's answer,

In YAML, you must explicitly map System.AccessToken into the pipeline using a variable. You can do this at the step or task level: YAML

steps:
  - bash: echo This script could use $SYSTEM_ACCESSTOKEN
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  - powershell: Write-Host "This is a script that could use $env:SYSTEM_ACCESSTOKEN"
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
TeamDman
  • 649
  • 6
  • 26