1

I have a powershell script that downloads files from an SFTP and then it tries to commit them to git, however I am having issue in that last step.

try
{
    # Load WinSCP .NET assembly
   Add-Type -Path  "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "myhostname"
        UserName = "myusername"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 myfingerprint"
        #GiveUpSecurityAndAcceptAnySshHostKey = "true" 
    }

    $session = New-Object WinSCP.Session

    try
    {
        New-Item -ItemType directory -Path "C:\tempfolder"

        # Connect
        $session.Open($sessionOptions)

        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

        $transferResult =
            $session.GetFiles("/BE_Test3rdPartyUpload2VSTS/Project1/upload/*", "c:\tempfolder\*", $False, $transferOptions)

        # Throw on any error
        $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Download of $($transfer.FileName) succeeded"
        }

        git config --global user.email "myusername"
        git config --global user.name "myname"

        git pull --rebase origin master
        git push origin master


        git add .
        git commit -m "Add existing file"
        git push origin "master"
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch [Exception]
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Error I have is this:

enter image description here

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
  • Your `HEAD` is detached. Where are you cloning or otherwise initializing the repository you're trying to work with? You're downloading files to `C:\tempfolder`, but I don't see that you're doing anything at all with the files you put there, unless `C:\tempfolder` is the Git repository you're working with. – Daniel Mann May 23 '18 at 04:02

1 Answers1

2

To push changes to master branch, you should change the powershell script with below aspects:

  1. Deselect Fail on Standard Error option for PowerShell task

    Since most git commands shows non-standard output, you need to deselect Fail on Standard Error option for PowerShell task, else PowerShell will be failed.

    enter image description here

  2. Provider credential in the git repo URL

    You need to provider credential when pushing changes to remote repo. You can use PAT or alternate credential in the git repo URL. The format of the git repo URL as below:

    https://Personal%20Access%20Token:{PAT}@{account}.visualstudio.com/{project}/_git/{repo}
    https://{AlternateUserName}:{AlternatePassword}@{account}.visualstudio.com/{project}/_git/{repo}
    

    So you should push with the command:

    git push https://Personal%20Access%20Token:{PAT}@{account}.visualstudio.com/{project}/_git/{repo} master
    

    Or

    git push https://{AlternateUserName}:{AlternatePassword}@{account}.visualstudio.com/{project}/_git/{repo} master
    
  3. Checkout to the build branch

    In the build source directory, git repo is in HEAD detached by default, so you need to switch to the build branch by:

    git checkout $(Build.SourceBranchName)
    
  4. Remove unnecessary commands

    Below two commands in your script seems make no changes both for the local master branch and remote master branch:

    git pull --rebase origin master
    git push origin master
    

So you can remove them in your script.

And below is the example for the changed powershell scripts:

try
{
    # Load WinSCP .NET assembly
   Add-Type -Path  "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "myhostname"
        UserName = "myusername"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 myfingerprint"
        #GiveUpSecurityAndAcceptAnySshHostKey = "true" 
    }

    $session = New-Object WinSCP.Session

    try
    {
        New-Item -ItemType directory -Path "C:\tempfolder"

        # Connect
        $session.Open($sessionOptions)

        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

        $transferResult =
            $session.GetFiles("/BE_Test3rdPartyUpload2VSTS/Project1/upload/*", "c:\tempfolder\*", $False, $transferOptions)

        # Throw on any error
        $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Download of $($transfer.FileName) succeeded"
        }

        git config --global user.email "myusername"
        git config --global user.name "myname"

        git checkout $(Build.SourceBranchName)

        git add .
        git commit -m "Add existing file"
        git push https://Personal%20Access%20Token:tp3iai4yextum26xa6k6qbflhjrvpt4jcaak8fkhbhlpis7zkndq@marinaliu.visualstudio.com/Git2/_git/product1 master 
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch [Exception]
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}
Marina Liu
  • 36,876
  • 5
  • 61
  • 74