Git - Pull (HTTPS)

Octopus.Script exported 2014-05-16 by bobjwalker belongs to ‘Git’ category.

Gets the latest source for a Git repository.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Clone URL

GitHttpsUrl

‘https://’ URL to the repository that will be cloned from.

Username

Username

Username to use when authenticating with the HTTPS server.

Password

Password

Password to use when authenticating with the HTTPS server. You should create a sensitive variable in your project variables, and then bind this value.

Branch name

GitHttpsBranchName = master

Name of the Git branch to clone

Script body

Steps based on this template will execute the following PowerShell script.

[System.Reflection.Assembly]::LoadWithPartialName("System.Web")
function Format-UriWithCredentials($url, $username, $password) {
    $uri = New-Object "System.Uri" $url
    
    $url = $uri.Scheme + "://"
    if (-not [string]::IsNullOrEmpty($username)) {
        $url = $url + [System.Web.HttpUtility]::UrlEncode($username)
        
        if (-not [string]::IsNullOrEmpty($password)) {
            $url = $url + ":" + [System.Web.HttpUtility]::UrlEncode($password)  
        }
        
        $url = $url + "@"    
    } elseif (-not [string]::IsNullOrEmpty($uri.UserInfo)) {
        $url = $uri.UserInfo + "@"
    }

    $url = $url + $uri.Host + $uri.PathAndQuery
    return $url
}

function Test-LastExit($cmd) {
    if ($LastExitCode -ne 0) {
        Write-Host "##octopus[stderr-error]"
        write-error "$cmd failed with exit code: $LastExitCode"
    }
}

$tempDirectoryPath = $OctopusParameters['Octopus.Tentacle.Agent.ApplicationDirectoryPath']
$tempDirectoryPath = join-path $tempDirectoryPath "GitPull" 
$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Environment.Name']
$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Project.Name']
$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Action.Name']

Write-Host "Repository will be cloned to: $tempDirectoryPath"

# Step 1: Ensure we have the latest version of the repository
mkdir $tempDirectoryPath -ErrorAction SilentlyContinue
cd $tempDirectoryPath

Write-Host "##octopus[stderr-progress]"

git init
Test-LastExit "git init"

$url = Format-UriWithCredentials -url $OctopusParameters['GitHttpsUrl'] -username $OctopusParameters['Username'] -password $OctopusParameters['Password']

$branch = $OctopusParameters['GitHttpsBranchName']

# We might have already run before, so we need to reset the origin
git remote remove origin
git remote add origin $url
Test-LastExit "git remote add origin"

Write-Host "Fetching remote repository"
git fetch origin
Test-LastExit "git fetch origin"

Write-Host "Check out branch $branch"
git reset --hard "origin/$branch"
Test-LastExit "git reset --hard"

Set-OctopusVariable -name "RepositoryDirectory" -value $tempDirectoryPath
Write-Verbose "Directory path '$tempDirectoryPath' available in 'RepositoryDirectory' output variable"
Write-Host "Repository successfully cloned to: $tempDirectoryPath"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "5c08170d-e919-4afe-9da3-7616c797d42b",
  "Name": "Git - Pull (HTTPS)",
  "Description": "Gets the latest source for a Git repository.",
  "Version": 8,
  "ExportedAt": "2014-05-16T00:23:33.282+00:00",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Parameters": [
    {
      "Name": "GitHttpsUrl",
      "Label": "Clone URL",
      "HelpText": "'https://' URL to the repository that will be cloned from.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Username",
      "Label": "Username",
      "HelpText": "Username to use when authenticating with the HTTPS server.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Password",
      "Label": "Password",
      "HelpText": "Password to use when authenticating with the HTTPS server. You should create a sensitive variable in your project variables, and then bind this value.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "GitHttpsBranchName",
      "Label": "Branch name",
      "HelpText": "Name of the Git branch to clone",
      "DefaultValue": "master",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "[System.Reflection.Assembly]::LoadWithPartialName(\"System.Web\")\nfunction Format-UriWithCredentials($url, $username, $password) {\n    $uri = New-Object \"System.Uri\" $url\n    \n    $url = $uri.Scheme + \"://\"\n    if (-not [string]::IsNullOrEmpty($username)) {\n        $url = $url + [System.Web.HttpUtility]::UrlEncode($username)\n        \n        if (-not [string]::IsNullOrEmpty($password)) {\n            $url = $url + \":\" + [System.Web.HttpUtility]::UrlEncode($password)  \n        }\n        \n        $url = $url + \"@\"    \n    } elseif (-not [string]::IsNullOrEmpty($uri.UserInfo)) {\n        $url = $uri.UserInfo + \"@\"\n    }\n\n    $url = $url + $uri.Host + $uri.PathAndQuery\n    return $url\n}\n\nfunction Test-LastExit($cmd) {\n    if ($LastExitCode -ne 0) {\n        Write-Host \"##octopus[stderr-error]\"\n        write-error \"$cmd failed with exit code: $LastExitCode\"\n    }\n}\n\n$tempDirectoryPath = $OctopusParameters['Octopus.Tentacle.Agent.ApplicationDirectoryPath']\n$tempDirectoryPath = join-path $tempDirectoryPath \"GitPull\" \n$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Environment.Name']\n$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Project.Name']\n$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Action.Name']\n\nWrite-Host \"Repository will be cloned to: $tempDirectoryPath\"\n\n# Step 1: Ensure we have the latest version of the repository\nmkdir $tempDirectoryPath -ErrorAction SilentlyContinue\ncd $tempDirectoryPath\n\nWrite-Host \"##octopus[stderr-progress]\"\n\ngit init\nTest-LastExit \"git init\"\n\n$url = Format-UriWithCredentials -url $OctopusParameters['GitHttpsUrl'] -username $OctopusParameters['Username'] -password $OctopusParameters['Password']\n\n$branch = $OctopusParameters['GitHttpsBranchName']\n\n# We might have already run before, so we need to reset the origin\ngit remote remove origin\ngit remote add origin $url\nTest-LastExit \"git remote add origin\"\n\nWrite-Host \"Fetching remote repository\"\ngit fetch origin\nTest-LastExit \"git fetch origin\"\n\nWrite-Host \"Check out branch $branch\"\ngit reset --hard \"origin/$branch\"\nTest-LastExit \"git reset --hard\"\n\nSet-OctopusVariable -name \"RepositoryDirectory\" -value $tempDirectoryPath\nWrite-Verbose \"Directory path '$tempDirectoryPath' available in 'RepositoryDirectory' output variable\"\nWrite-Host \"Repository successfully cloned to: $tempDirectoryPath\"",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Git",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/git-pull-https.json",
  "Website": "/step-templates/5c08170d-e919-4afe-9da3-7616c797d42b",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////9CQ09TJB/dbZ9k1a/vHy+ZGZ/MjM+HaA92l0+62z91tn/uTm+YSN9T9N/Lq/+p+mxWmzxwAAA/5JREFUeNrs3dlyqzAMBmAWs0Po+z9t007OaUJskCXZljL6r3pDqi94w6bTqrJYLBaLxWKxWCwWi8VisVgYsw736GfMrv7NpNvSPhi/lE6xo6mf4rrPcOiVHB1aJe8OnRKfQ6PE79AnCTm0ScIOXZIzhybJuUOP5MqhRXLt0CGBODRIYA4Fkltdf4YEekdMYhKTmKRw7S+bJHol98rdJ0h+6/4AyaNq9ZL/NSuXPFWsWvJSr2LJoVq1krdalUo8laqUeOs8SG5T3/dLI1oS+L5fSvr3877IlQTbjb+kvREqOWn//pJWJ1Jy2o/9JXU3gZKL8chfUiuvn7S4Br9JkwDmh9HbTYSNwpB5bvZeOYmSgObr1Xvpl6Q5HuRo/NcOglYrsPXT4r94lbPuAq4DA5BOzAoSup4lNK0sEvi63F/KLGRVD3cEht9RxvNJhCPQSWQ8acU46noQK4lz1E7qDnekI7DaKi+JdgiVIBzBFzJLSlCO+7TYC5MgHff8fYQECd7xB+nH8hKC4wlSF5dQHM+Q0hK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