pipeline
{
    parameters
    {
        string(name: 'REPOSITORY_URL', defaultValue: '', description: '<BR><font color=RED>*</font> URL to GIT repository')
        string(name: 'GIT_REPO_CRED', defaultValue: '', description: '<BR><font color=RED>*</font> Credential name of GIT user')
        string(name: 'BRANCH', defaultValue: 'master', description: '<BR>Name of branch (default: master)')
        string(name: 'REQUIREMENTS_FILE', description: '<BR>Relative path to the requirements file')
        string(name: 'PYTHON_SCRIPT_FILE', description: '<BR><font color=RED>*</font> Relative path to the python script')
    }

    options
    {
        buildDiscarder(logRotator(numToKeepStr: '100', daysToKeepStr: '45'))
        ansiColor('xterm')
        timestamps()
    }

    agent
    {
        dockerfile
        {
            filename 'pipelines/Dockerfile'
            args "-u root -v /var/run/docker.sock:/var/run/docker.sock"
        }
    }

    stages
    {
        stage('Setup')
        {
            steps
            {
                script
                {
                    def script = PYTHON_SCRIPT_FILE.tokenize('/')[-1]
                    currentBuild.displayName = "#${BUILD_ID} | ${script}"
                }
                checkout([
                        $class           : 'GitSCM', branches: [[name: BRANCH]],
                        userRemoteConfigs: [[url: "${REPOSITORY_URL}", credentialsId: "${GIT_REPO_CRED}"]]
                ])
            }
        }

        stage('Install requirements')
        {
            steps
            {
                sh '''
                    if [ "x${REQUIREMENTS_FILE}" != "x" ] && [ -f ${REQUIREMENTS_FILE} ]; then 
                        pip install -r ${REQUIREMENTS_FILE}; 
                    fi
                '''
            }
        }
        stage('Run script')
        {
            steps
            {
                script
                {
                    writeFile encoding: 'UTF-8',file: './variables.groovy', text: GROOVY_SCRIPT
                    load './variables.groovy'
                    
                    sh "python ./${PYTHON_SCRIPT_FILE}"
                }
            }
        }
    }
}
