3

I'm extremely new to this, so apologies if it's a dumb question but I couldn't find anything about it either here, at help.octopusdeploy.com, or google.

Additionally, I'm a DevOps engineer, not a developer and have been using TC and Octopus for about 3 weeks. I'm loving it so far, but it's probably best if you consider me a total rookie ;)

I currently have a build configuration in TeamCity that on a successful build run, creates a release in Octopus and deploys the project to a test server on a succssful build. It is kept separate but deployed alongside the master build. So, in IIS it looks like:

IIS Sites
    site.domain.com (master build)
    featurebuild1-site.domain.com (feature branch 1)
    featurebuild2-site.domain.com (feature branch 2)
    etc...

Obviously, this makes life really easy for the devs when testing their feature builds, but it leaves a hell of a mess on the test and integration servers. I can go in and clean them up manually, but I'd vastly prefer it to not leave crap lying around after they've removed the branch in TeamCity.

So, the Project in TeamCity looks like:

Project Name
    Feature
        /Featurebuild1
        /Featurebuild2
        /Featurebuild3
    Master

Assuming all three feature builds run successfully, I will have 3 feature build IIS sites on the test server alongside the master. If they decide they're done with Featurebuild3 and remove it, I want to somehow automate the removal of featurebuild3-site.domain.com in IIS on my test server. Is this possible? If so, how?

My initial thoughts are to have another Octopus project that will go in and remove the site(s), but I can't figure out if I can/how to trigger it.

Relevant details:
TeamCity version: 9.1.1 (build 37059)
Octopus Deploy version: 3.0.10.2278

Gesthemene
  • 79
  • 7
  • I'm glad you have been loving Octopus, but after a few months using it I came to the opposite conclusion! Once you try doing something outside of how Octopus expects you to work, you quickly run into trouble. I think the problem you have encountered is exactly that. It supports deployment but not undeployment. You'll have to hack a pre-deployment script that somehow looks for deleted branches and removes those apps. Not pretty – Jack Ukleja Dec 16 '15 at 20:09

1 Answers1

1

Ok, it took me a little while to figure it out, but here's what I ended up doing (just in the event that anyone else is attempting to do the same thing).

I ended up bypassing TeamCity entirely and using our Stash repositories as the source. Also, as I didn't need it to clean up IMMEDIATELY upon deletion, I was happy to have it run nightly. Once I'd decided that, it was then down to a bunch of nested REST API calls to loop through each project and team to enumerate all the different repositories (apologies if I'm butchering terminology here).

$stashroot = "http://<yourstashsite>/rest/api/1.0"
$stashsuffix = "/repos/"
$stashappendix = "/branches"

$teamquery = curl $stash -erroraction silentlycontinue

At this point, I started using jq (https://stedolan.github.io/jq/) to do some better parsing of the text I was getting back

$teams = $teamquery.content | jq -r ".values[].link.url"

Foreach ($team in $teams)
{
# Get the list of branches in the repository
# Feature branch URL format be like: http://<yourstashsite>/projects/<projectname>/repos/<repositoryname>/branches #
$project = $stashroot +$team +$stashsuffix
$projectquery = curl $project -erroraction silentlycontinue
$repos = $projectquery.content | jq -r ".values[].name"
Foreach ($repo in $repos)
    {
    Try
        {
        $repository = $stashroot +$team +$stashsuffix +$repo +$stashappendix
        $repositoryquery = curl $repository -erroraction silentlycontinue
        $reponames = $repositoryquery.content | jq -r ".values[].displayId"
        Foreach ($reponame in $reponames)
            {
            #write-host $team "/" $repo "/" $reponame -erroraction silentlycontinue
            $NewObject = new-object PSObject
            $NewObject | add-member -membertype NoteProperty -name "Team" -value $team
            $NewObject | add-member -membertype NoteProperty -name "Repository" -value $repo
            $NewObject | add-member -membertype NoteProperty -name "Branch" -value $reponame
            $NewObject | export-csv <desiredfilepath> -notype -append
            }
        }
    Catch{} # Yes, I know this is terrible; it makes me sad too :(
    }
}

After that, it was simply a matter of doing a compare-item against the CSV files from two different days (I have logic in place to look for a pre-existing csv and rename it to append "_yesterday" to it), outputting to a file, all the repositories/builds that have been nuked since yesterday.

After that, it strips out the feature branch names (which we use to prefix test site names in IIS, and loops through looking for any sites in IIS that match that site prefix, removes them, the associated application pool, and deletes the directory on the server that stored the site content.

I'm sure there are far better ways to achieve this, especially if you know how to code. I'm just a poor little script monkey though, so I have to make do with what I have :)

Gesthemene
  • 79
  • 7