Git - Push (HTTPS)

Octopus.Script exported 2014-10-24 by bobjwalker belongs to ‘Git’ category.

Deploy a package using Git to a HTTPS server. Performs a clone, overwrites the repository with the files from your package, then pushes. Great for deploying to AppHarbor and Windows Azure websites.

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 and pushed to.

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.

Package step name

GitHttpsPackageStepName

Name of the previously-deployed package step that contains the files that you want to push.

Branch name

GitHttpsBranchName = master

Name of the Git branch to clone/push

Script body

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

[System.Reflection.Assembly]::LoadWithPartialName("System.Web")

# A collection of functions that can be used by script steps to determine where packages installed
# by previous steps are located on the filesystem.
 
function Find-InstallLocations {
    $result = @()
    $OctopusParameters.Keys | foreach {
        if ($_.EndsWith('].Output.Package.InstallationDirectoryPath')) {
            $result += $OctopusParameters[$_]
        }
    }
    return $result
}
 
function Find-InstallLocation($stepName) {
    $result = $OctopusParameters.Keys | where {
        $_.Equals("Octopus.Action[$stepName].Output.Package.InstallationDirectoryPath",  [System.StringComparison]::OrdinalIgnoreCase)
    } | select -first 1
 
    if ($result) {
        return $OctopusParameters[$result]
    }
 
    throw "No install location found for step: $stepName"
}
 
function Find-SingleInstallLocation {
    $all = @(Find-InstallLocations)
    if ($all.Length -eq 1) {
        return $all[0]
    }
    if ($all.Length -eq 0) {
        throw "No package steps found"
    }
    throw "Multiple package steps have run; please specify a single step"
}

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 "GitPush" 
$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Environment.Name']
$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Project.Name']
$tempDirectoryPath = join-path $tempDirectoryPath $OctopusParameters['Octopus.Action.Name']

$stepName = $OctopusParameters['GitHttpsPackageStepName']

$stepPath = ""
if (-not [string]::IsNullOrEmpty($stepName)) {
    Write-Host "Finding path to package step: $stepName"
    $stepPath = Find-InstallLocation $stepName
} else {
    $stepPath = Find-SingleInstallLocation
}
Write-Host "Package was installed to: $stepPath"

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"

# Step 2: Overwrite the contents
write-host "Synchronizing package contents with local git repository using Robocopy"
& robocopy $stepPath $tempDirectoryPath /MIR /xd ".git"
if ($lastexitcode -ge 5) {
    write-error "Unable to copy files from the package to the local cloned Git repository. See the Robocopy errors above for details."
}

# Step 3: Push the results
$deploymentName = $OctopusParameters['Octopus.Deployment.Name']
$releaseName = $OctopusParameters['Octopus.Release.Number']
$projName = $OctopusParameters['Octopus.Project.Name']

git add . -A
Test-LastExit "git add"

git diff-index --quiet HEAD
if ($lastexitcode -ne 0) {
    git commit -m "$projName release $releaseName - $deploymentName"
    Test-LastExit "git commit"
}

git push origin $branch
Test-LastExit "git push"

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": "85cc56f9-26cb-4b0e-8320-854e88a09a25",
  "Name": "Git - Push (HTTPS)",
  "Description": "Deploy a package using Git to a HTTPS server. Performs a clone, overwrites the repository with the files from your package, then pushes. Great for deploying to AppHarbor and Windows Azure websites.",
  "Version": 2,
  "ExportedAt": "2014-10-24T14:59:45.295+00:00",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Parameters": [
    {
      "Name": "GitHttpsUrl",
      "Label": "Clone URL",
      "HelpText": "`https://` URL to the repository that will be cloned from and pushed to.",
      "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": "GitHttpsPackageStepName",
      "Label": "Package step name",
      "HelpText": "Name of the previously-deployed package step that contains the files that you want to push.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "GitHttpsBranchName",
      "Label": "Branch name",
      "HelpText": "Name of the Git branch to clone/push",
      "DefaultValue": "master",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "[System.Reflection.Assembly]::LoadWithPartialName(\"System.Web\")\n\n# A collection of functions that can be used by script steps to determine where packages installed\n# by previous steps are located on the filesystem.\n \nfunction Find-InstallLocations {\n    $result = @()\n    $OctopusParameters.Keys | foreach {\n        if ($_.EndsWith('].Output.Package.InstallationDirectoryPath')) {\n            $result += $OctopusParameters[$_]\n        }\n    }\n    return $result\n}\n \nfunction Find-InstallLocation($stepName) {\n    $result = $OctopusParameters.Keys | where {\n        $_.Equals(\"Octopus.Action[$stepName].Output.Package.InstallationDirectoryPath\",  [System.StringComparison]::OrdinalIgnoreCase)\n    } | select -first 1\n \n    if ($result) {\n        return $OctopusParameters[$result]\n    }\n \n    throw \"No install location found for step: $stepName\"\n}\n \nfunction Find-SingleInstallLocation {\n    $all = @(Find-InstallLocations)\n    if ($all.Length -eq 1) {\n        return $all[0]\n    }\n    if ($all.Length -eq 0) {\n        throw \"No package steps found\"\n    }\n    throw \"Multiple package steps have run; please specify a single step\"\n}\n\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 \"GitPush\" \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\n$stepName = $OctopusParameters['GitHttpsPackageStepName']\n\n$stepPath = \"\"\nif (-not [string]::IsNullOrEmpty($stepName)) {\n    Write-Host \"Finding path to package step: $stepName\"\n    $stepPath = Find-InstallLocation $stepName\n} else {\n    $stepPath = Find-SingleInstallLocation\n}\nWrite-Host \"Package was installed to: $stepPath\"\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\"\n\n# Step 2: Overwrite the contents\nwrite-host \"Synchronizing package contents with local git repository using Robocopy\"\n& robocopy $stepPath $tempDirectoryPath /MIR /xd \".git\"\nif ($lastexitcode -ge 5) {\n    write-error \"Unable to copy files from the package to the local cloned Git repository. See the Robocopy errors above for details.\"\n}\n\n# Step 3: Push the results\n$deploymentName = $OctopusParameters['Octopus.Deployment.Name']\n$releaseName = $OctopusParameters['Octopus.Release.Number']\n$projName = $OctopusParameters['Octopus.Project.Name']\n\ngit add . -A\nTest-LastExit \"git add\"\n\ngit diff-index --quiet HEAD\nif ($lastexitcode -ne 0) {\n    git commit -m \"$projName release $releaseName - $deploymentName\"\n    Test-LastExit \"git commit\"\n}\n\ngit push origin $branch\nTest-LastExit \"git push\"\n",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Git",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/git-push-https.json",
  "Website": "/step-templates/85cc56f9-26cb-4b0e-8320-854e88a09a25",
  "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+Q0hKS4wVSVkJzvEJKSoiOA6SchOo4QkpJyI43SBkJ3fEOOUjcNg/D3rukEgaHBxLYgxzTSTgcPkhgVTykOj9hcXghAUmX5vyEx+GHhCRJ7omrE0JCrevyASfeMddJIQHJ5Wb9UOqGBCH1nueWrHVqiL+538CfB8yQGhLY5B6521ZySGCHpVcH8e9KVPunQGb2YSs1ZEI2rTUWMiWG3HC/1lWlhq3gd92iFilz/NQ+JYaMmC7iEGutzqWF+Fr71Vs3TVsVk4QhS/SrKjgHk+RkPBojGxbWwSM5G1jHTA4WyekMsTz1k25K5+CQXEx142OmbrcmpYNBcj1nL/dcPlZTHXQJfPGR1kGWsEA4HFQJB4THQZQwQLgcNAkdwucgScgQTgdFQoXwOggSIoTbgZfQIPwOpGRcn450ZDgwkmaPedrI5YiXvNUxy3BES4bYJ8BcjkjJRtvjT+mIkjQdaZMprSNGsuGODTI5IiT+TdpNigMuoWxg5nBAJaG3fuU4gJIFv8ufywGT4O9IPgdMgu0jOR0gSYs7yMnrgEg21PF9bgdAgprZ8zsAkj7+hpRwACRtbA8p47iWxD6PlHJES76kOiD95K/Hr4tcB2TsGvcfyzpPtWQH29lvcQeXpLyDRyLBwSGR4aBLpDioEjkOmkSSgyKR5cBLpDmwEnkOnESiA3V+ItLBcH6iVCLXESeR7IiRyHbAJdIdUIl8B0yiwQGR6HAg9ruUSvQ4ziWaHGcSXY6wRJsjJNHn8Es0OnwSnY53iVbHUaLX8fo3IU6x457hQXFzpT4//yB4rSwWi8VisVgsFovFYrFYLBbGfAswAOAaON62iVbvAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Friday, October 24, 2014