<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Octopus &#8211; blog.boro2g .co.uk</title>
	<atom:link href="https://blog.boro2g.co.uk/category/octopus/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.boro2g.co.uk</link>
	<description>Some ideas about coding, dev and all things online.</description>
	<lastBuildDate>Thu, 11 Feb 2016 11:55:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>
	<item>
		<title>Octopus deploy &#8211; Script Steps sourced from a package</title>
		<link>https://blog.boro2g.co.uk/octopus-deploy-script-steps-sourced-from-a-package/</link>
					<comments>https://blog.boro2g.co.uk/octopus-deploy-script-steps-sourced-from-a-package/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Thu, 11 Feb 2016 11:49:41 +0000</pubDate>
				<category><![CDATA[Octopus]]></category>
		<category><![CDATA[powershell]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=736</guid>

					<description><![CDATA[<p>In the latest version of Octopus deploy you can now choose to run script steps where the files exist in a package. This might sound like a minor change but opens up some very neat options. You can read more on the details of the change in https://octopus.com/blog/octopus-deploy-3.3#ScriptsInPackages. Note at the time of writing this is [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/octopus-deploy-script-steps-sourced-from-a-package/">Octopus deploy &#8211; Script Steps sourced from a package</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In the latest version of Octopus deploy you can now choose to run script steps where the files exist in a package. This might sound like a minor change but opens up some very neat options. You can read more on the details of the change in <a href="https://octopus.com/blog/octopus-deploy-3.3#ScriptsInPackages" target="_blank">https://octopus.com/blog/octopus-deploy-3.3#ScriptsInPackages</a>. <em>Note at the time of writing this is only available in the beta of 3.3 (<a href="https://octopus.com/downloads/3.3.0-beta0001" target="_blank">https://octopus.com/downloads/3.3.0-beta0001</a>)</em></p>
<p><strong>Ok, so why is this such a good thing?</strong><br />
Step templates are great &#8211; there is even a large library of pre-existing templates to download (<a href="https://library.octopusdeploy.com/#!/listing" target="_blank">https://library.octopusdeploy.com/#!/listing</a>). If you&#8217;ve not used them before, step templates allow additional scripts to run during your deployment. </p>
<p>Examples could be: post to slack, create certain folders, delete given files etc. Basically anything you can achieve with powershell can be done in step templates.</p>
<p><strong>Lets just stick with step templates then?</strong><br />
If you&#8217;ve gone through the process of setting up several deployments with Octopus and find you want to replicate the same functionality across several projects or installs then you need to re-create all the step template configurations each time. It&#8217;s not the slowest process but the idea below helps streamline things.</p>
<p>Now that you can run scripts from a package, why not source control the steps you want to run? One key advantage is that you can then see things like history of all the deployment steps.</p>
<p><strong>What needs setting up?</strong><br />
You need to be running version 3.3 or higher of Octopus &#8211; see above for the link.</p>
<p>I&#8217;ve been using a simple test deployment of an out the box MVC project along with a new project specifically for the scripts.:<br />
<a href="http://blog.boro2g.co.uk/wp-content/uploads/2016/02/solution-setup.png" rel="attachment wp-att-738"><img decoding="async" class="alignnone size-full wp-image-738" src="http://blog.boro2g.co.uk/wp-content/uploads/2016/02/solution-setup.png" alt="solution setup" width="262" height="170" /></a></p>
<p>In Octopus this has 2 steps:<br />
<a href="http://blog.boro2g.co.uk/wp-content/uploads/2016/02/steps.png" rel="attachment wp-att-739"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-739" src="http://blog.boro2g.co.uk/wp-content/uploads/2016/02/steps.png" alt="steps" width="672" height="285" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2016/02/steps.png 672w, https://blog.boro2g.co.uk/wp-content/uploads/2016/02/steps-300x127.png 300w" sizes="(max-width: 672px) 100vw, 672px" /></a></p>
<p>The first is a vanilla website deployment of &#8216;WebApplication1&#8217;. The second the startup scripts:<br />
<a href="http://blog.boro2g.co.uk/wp-content/uploads/2016/02/script-step.png" rel="attachment wp-att-737"><img decoding="async" class="alignnone size-full wp-image-737" src="http://blog.boro2g.co.uk/wp-content/uploads/2016/02/script-step.png" alt="script step" width="780" height="789" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2016/02/script-step.png 780w, https://blog.boro2g.co.uk/wp-content/uploads/2016/02/script-step-297x300.png 297w, https://blog.boro2g.co.uk/wp-content/uploads/2016/02/script-step-768x777.png 768w" sizes="(max-width: 780px) 100vw, 780px" /></a></p>
<p><strong>Note the package id.</strong> The idea behind using a separate projects is that the powershell scripts never need to exist in the website project.</p>
<p><strong>The startup script project</strong><br />
<a href="http://blog.boro2g.co.uk/wp-content/uploads/2016/02/solution-setup.png" rel="attachment wp-att-738"><img decoding="async" class="alignnone size-full wp-image-738" src="http://blog.boro2g.co.uk/wp-content/uploads/2016/02/solution-setup.png" alt="solution setup" width="262" height="170" /></a></p>
<p>I chose to use a class library for the simple reason that I could include a reference to Octopack and hence building the output nuget file was trivial.</p>
<p>The nuspec file is important as it tells the packaging to include all powershell files:</p><pre class="crayon-plain-tag">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;package xmlns=&quot;http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd&quot;&gt;
  &lt;metadata&gt;
    &lt;id&gt;WebApplication1.Startup&lt;/id&gt;
    &lt;title&gt;TODO Your Web Application&lt;/title&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;authors&gt;TODO Your name&lt;/authors&gt;
    &lt;owners&gt;TODO Your name&lt;/owners&gt;
    &lt;licenseUrl&gt;http://TODO.com&lt;/licenseUrl&gt;
    &lt;projectUrl&gt;http://TODO.com&lt;/projectUrl&gt;
    &lt;requireLicenseAcceptance&gt;false&lt;/requireLicenseAcceptance&gt;
    &lt;description&gt;TODO A sample project&lt;/description&gt;
    &lt;releaseNotes&gt;TODO This release contains the following changes...&lt;/releaseNotes&gt;	
  &lt;/metadata&gt;
  &lt;files&gt;
	  &lt;file src=&quot;**\*.ps1&quot; target=&quot;\&quot; /&gt;
  &lt;/files&gt;
&lt;/package&gt;</pre><p></p>
<p>Packages simply contains a reference to Octopack:</p><pre class="crayon-plain-tag">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;packages&gt;
  &lt;package id=&quot;OctoPack&quot; version=&quot;3.0.53&quot; targetFramework=&quot;net46&quot; developmentDependency=&quot;true&quot; /&gt;
&lt;/packages&gt;</pre><p></p>
<p>And finally the scripts:<br />
<strong>Helloworld.ps1</strong></p><pre class="crayon-plain-tag">Write-Host &quot;Hello World PS script example&quot;

if ($OctopusParameters) {
	##enable the next line to show all Octopus variables
	#$OctopusParameters.GetEnumerator() | Sort-Object Name | % { &quot;$($_.Key)=$($_.Value)&quot; }
}

#use this approach to pull in additional script files
. .\Startup.ps1</pre><p></p>
<p>And the more important one, <strong>Startup.ps1</strong></p><pre class="crayon-plain-tag">function createFolderIfNotExists{
	param([string]$folder)
	if (!(Test-Path $folder)) {
		Write-Host &quot;Creating $folder&quot;
		New-Item -Path $folder -ItemType Directory
	} else {
		Write-Host &quot;Directory already exists!&quot;
	}
}

function assignReadPermissionsTo {
    param([string] $item, [string] $readPermissionsTo)
    if(!(Test-Path $item)) {
        throw &quot;$item does not exist&quot;
    }
    $users = $readPermissionsTo.Split(&quot;,&quot;)
    foreach($user in $users) {
        Write-Host &quot;Adding read permissions for $user&quot;
        $acl = Get-Acl $item
        $acl.SetAccessRuleProtection($False, $False)
        $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($user, &quot;Read&quot;, &quot;ContainerInherit, ObjectInherit&quot;, &quot;None&quot;, &quot;Allow&quot;)
        $acl.AddAccessRule($rule)
        Set-Acl $item $acl
    }
}

function assignWritePermissionsTo {
    param([string] $item, [string] $writePermissionsTo)
    if(!(Test-Path $item)) {
        throw &quot;$item does not exist&quot;
    }
    $users = $writePermissionsTo.Split(&quot;,&quot;)
    foreach($user in $users) {
        Write-Host &quot;Adding write permissions for $user&quot;
        $acl = Get-Acl $item
        $acl.SetAccessRuleProtection($False, $False)
        $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($user, &quot;Write&quot;, &quot;ContainerInherit, ObjectInherit&quot;, &quot;None&quot;, &quot;Allow&quot;)
        $acl.AddAccessRule($rule)
        Set-Acl $item $acl
    }
}

$foldersToCreate = @(&quot;_logs&quot;,&quot;test\deep&quot;)
$usersToGiveAccess = &quot;IIS_IUSRS&quot;

if ($OctopusParameters) {
	$installFolder = $OctopusParameters['Octopus.Action[Deploy website].Output.Package.InstallationDirectoryPath']

	Write-Host &quot;Environment: &quot; $OctopusParameters['Octopus.Environment.Name']
	Write-Host &quot;Deployment folder: &quot; $installFolder
	
    Foreach ($folder in $foldersToCreate) {
        $newFolder = [io.path]::combine($installFolder,$folder)
        createFolderIfNotExists -folder $newFolder
        assignWritePermissionsTo -item $newFolder -writePermissionsTo $usersToGiveAccess
    }	
}
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Write-Host &quot;Current running folder is: &quot; $PSScriptRoot</pre><p></p>
<p>It&#8217;s worth noting this should be considered a POC of the approach. The next steps would be to split the scripts up into more meaningful units, remove hello world and update the nuspec with more valid information.</p>
<p>If you struggle with accessing the Octopus Parameters you require, the script in helloworld allows you to dump out all parameters and their values. In the startup script the parameter: <em>$OctopusParameters[&#8216;Octopus.Action[Deploy website].Output.Package.InstallationDirectoryPath&#8217;]</em> depends on the name of your deployment in step 1 of your deployment process <em>(Deploy website)</em></p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/octopus-deploy-script-steps-sourced-from-a-package/">Octopus deploy &#8211; Script Steps sourced from a package</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/octopus-deploy-script-steps-sourced-from-a-package/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
