Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostBuild: Compile MOF PipeLine - Fatal: A branch named 'main' already exists #10

Open
OlyMills opened this issue Jan 10, 2025 · 4 comments

Comments

@OlyMills
Copy link

OlyMills commented Jan 10, 2025

Good afternoon,
We have implemented the solution and works well, apart from the issue below:

Scenario:

  • Make Change and commit

  • Piplines run and changes Built and deployed

  • Make another change and commit

  • Pipelines run however on Post Build during MOF Build Pipeline we recieve an error:

git : Switched to a new branch 'main'

At C:\agent\_work\2\s\CICD\Scripts\PostBuild.ps1:62 char:1
+ git checkout --track origin/$($env:BUILD_SOURCEBRANCHNAME) *> $null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Switched to a new branch 'main':String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

image

This is cleared up by purging the work folder on the agent.

Any help would be wonderful :)

Thanks in advance
Oly

@martincaddick
Copy link

@OlyMills,
I suspect I'm just masking the problem but I have modified my PostBuild.ps1 to this.

--- snip ---
Write-Log -Object "Switching to path: $($env:BUILD_SOURCEBRANCHNAME)"
Write-Log -Object "Running remote update"
git remote update #*> $null
Write-Log -Object "Running git fetch"
git fetch # *> $null
# git checkout --track origin/$($env:BUILD_SOURCEBRANCHNAME) -q *> $null
# git switch ($env:BUILD_SOURCEBRANCHNAME) # --track origin/($env:BUILD_SOURCEBRANCHNAME) -q *> $null

    # Check if the branch already exists
    Write-Log -Object "Checking if the branch already exists"
    if (git show-ref --verify refs/heads/$env:BUILD_SOURCEBRANCHNAME) {
        # If the branch exists, switch to it
        Write-Log -Object "Finally switching to the branch - running git checkout"
        git checkout $env:BUILD_SOURCEBRANCHNAME
        Write-Log -Object "Running git pull"
        git pull
    } else {
        # If the branch does not exist, create it and set up tracking
        Write-Log -Object "The branch doesn't already exist - runninggit checkout --track origin"
        git checkout --track origin/$env:BUILD_SOURCEBRANCHNAME
    }

Write-Log -Object ' '
Write-Log -Object '---------------------------------------------------------'
Write-Log -Object ' Retrieving last commit information'
--- snip ---

@OlyMills
Copy link
Author

Hey @martincaddick,
Thankyou very much, works a treat. I must admit I was on the same thought track, although my attempt didnt work :)

Hopefully this helps others :)

@ykuijs
Copy link
Owner

ykuijs commented Jan 21, 2025

Included these commands in the PostBuild script. The new version will be pushed out shortly.

Closing this issue now. Please reopen if more help is needed

@ykuijs ykuijs closed this as completed Jan 21, 2025
@martincaddick
Copy link

@ykuijs Don't have permissions to reopen.

I suddenly realised I have these additional lines in my postbuild.ps1.

I used a different method of determining $yamlfiles and am only using git add with *.yaml (not *.yml)

    $yamlfiles = $status | where-object { $_ -match '\.yaml$' -or $_ -match '\.yml$' }
    Write-Log -Object $yamlfiles
    # if (($status | ForEach-Object { $_ -like '*.yaml' -or $_ -like '*.yml' }) -contains $true)
    if ($yamlfiles)
    {
        Write-Log -Object "Committing $($yamlfiles.Count) file changes to the repository"
        git config --global user.email "$email"
        git config --global user.name "$name"

        git add *.yaml # *.yml
        git commit -m 'Updated pipeline yaml files [skip ci]'
        git push #origin HEAD:main
    }

@ykuijs ykuijs reopened this Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants