def DockerApp
node ('Docker') {
    stage ('Checkout') {
        checkout([
            $class: 'SubversionSCM',
            additionalCredentials: [],
            excludedCommitMessages: '',
            excludedRegions: '',
            excludedRevprop: '',
            excludedUsers: '',
            filterChangelog: false,
            ignoreDirPropChanges: false,
            includedRegions: '',
            locations: [[
                credentialsId: '83601f39-7c4c-4a13-a0d3-59fcda340753',
                depthOption: 'infinity',
                ignoreExternalsOption: true,
                local: '.',
                remote: 'http://svn.dyalog.bramley/svn/dyalogtools/Causeway/trunk/sharpplot-help/sharpplot'
            ]],
            workspaceUpdater: [$class: 'UpdateUpdater']
        ])
    }
    stage ('link index.htm to index.html'){
        sh 'ln -sd index.htm index.html'
    }
    stage ('Test Website') {
        DockerApp = docker.image('httpd:2.4.46')
        def SharpPlotSite = DockerApp.run ('-v ${WORKSPACE}:/usr/local/apache2/htdocs/')
        def DockerIP = sh (
            script: "docker inspect -f \'{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' ${SharpPlotSite.id}",
            returnStdout: true
        ).trim()
        println "sharpplot container IP: ${DockerIP}"
        try {
            sh "sleep 5; wget -qO- -T 30 -t 1 http://${DockerIP} | grep \"Automated charting and reporting\" >/dev/null"
            SharpPlotSite.stop()
            } 
        catch (Exception e) {
            println 'Failed to start SharpPlot website'
            sh "docker logs ${SharpPlotSite.id}"
            SharpPlotSite.stop()
            throw e;
        }
    }
    stage ('Copy files') {
            ftpPublisher alwaysPublishFromMaster: false, continueOnError: false, failOnError: false, publishers: [[
                configName: 'sharpplot-web',
                transfers: [[
                    asciiMode: false,
                    cleanRemote: true,
                    excludes: '',
                    flatten: false,
                    makeEmptyDirs: false,
                    noDefaultExcludes: false,
                    patternSeparator: '[, ]+',
                    remoteDirectory: "/",
                    remoteDirectorySDF: false,
                    removePrefix: '',
                    sourceFiles: '**/*'
                ]],
                usePromotionTimestamp: false,
                useWorkspaceInPromotion: false,
                verbose: true
            ]]
    }
    stage ('Deploy Website to Rancher') {
        withCredentials([usernamePassword(credentialsId: '	02543ae7-7ed9-4448-ba20-6b367d302ecc', passwordVariable: 'SECRETKEY', usernameVariable: 'ACCESSKEY')]) {
            sh '/usr/local/bin/rancher-compose --access-key $ACCESSKEY --secret-key $SECRETKEY --url http://rancher.dyalog.com:8080/v2-beta/projects/1a5/stacks/1st8 -p Sharpplot up --force-upgrade --confirm-upgrade --pull -d'
        }
    }
}