pipeline {
    agent none
    options {
        timestamps()
    }
    stages {
        stage('test') {
            agent {
                dockerfile true
            }
            environment {
                PROJECT = 'calendar_attach_processor'
            }
            steps {
                sh "pytest --cov=$PROJECT --cov-report html --cov-report xml --cov-report term --junitxml=junit.xml"
            }

            post {
                always {
                    cobertura coberturaReportFile: 'coverage.xml', sourceEncoding: 'UTF_8'
                    junit allowEmptyResults: true, testResults: 'junit.xml'
                }
            }
        }
        stage('publish') {
            agent {
                dockerfile true
            }
            when {
                expression { env.TAG_NAME != null && env.TAG_NAME ==~ /v.+/ }
            }
            environment {
                // https://pypi.yandex-team.ru/accounts/access-keys/
                PYPI_CREDENTIALS = credentials('pypi-credentials')
                TWINE_REPOSITORY_URL = 'https://pypi.yandex-team.ru/simple/'
            }
            steps {
                sh 'rm -rf dist && python setup.py sdist'
                retry(3) {
                    sh "twine upload -u $PYPI_CREDENTIALS_USR -p $PYPI_CREDENTIALS_PSW dist/*"
                }
            }
            post {
                always {
                    deleteDir()
                }
            }
        }
    }
}
