pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile'
            dir 'misc/jenkins'
        }
    }

    parameters {
        persistentText(name: 'emissaryRelease', defaultValue: 'latest', description: 'Numéro de release Emissary', successfulOnly: false)
    }

    stages {
        stage('Cancel older jobs') {
            steps {
                script {
                    def buildNumber = env.BUILD_NUMBER as int
                    if (buildNumber > 1) milestone(buildNumber - 1)
                    milestone(buildNumber)
                }
            }
        }

        stage('Update emissary release') {
            when {
                branch 'master'
                expression {
                    return params.emissaryRelease != 'latest'
                }
            }
            steps {
                script {
                    currentEmissaryRelease = readFile('emissary_release.txt').trim()

                    if (currentEmissaryRelease == params.emissaryRelease) {
                        currentBuild.result = 'SUCCESS'
                        return
                    }

                    withCredentials([
                        usernamePassword([
                        credentialsId: 'forge-jenkins',
                        usernameVariable: 'GIT_USERNAME',
                        passwordVariable: 'GIT_PASSWORD'
                        ])
                    ]) {
                        sh """
                        git config user.email "jenkins@cadoles.com"
                        git config user.name "Jenkins"
                        git config credential.https://forge.cadoles.com.username "\$GIT_USERNAME"
                        git config credential.https://forge.cadoles.com.helper '!f() { test "\$1" = get && echo "password=\$GIT_PASSWORD"; }; f'

                        echo '${params.emissaryRelease}' > emissary_release.txt
                        git add emissary_release.txt
                        git commit -m "feat: use emissary ${params.emissaryRelease}"
                        git push origin \$(git rev-parse HEAD):${env.GIT_BRANCH}
                        """
                    }
                }
            }
        }

        stage('Build') {
            steps {
                script {
                    withCredentials([
                            usernamePassword([
                            credentialsId: 'forge-jenkins',
                            usernameVariable: 'GITEA_DOWNLOAD_USERNAME',
                            passwordVariable: 'GITEA_DOWNLOAD_PASSWORD'
                        ])
                    ]) {
                        sh '''
                        make download-emissary-release
                        make all
                        '''
                    }
                }
            }
        }

        stage('Release') {
            when {
                branch 'master'
            }
            steps {
                withCredentials([
                        usernamePassword([
                        credentialsId: 'forge-jenkins',
                        usernameVariable: 'GITEA_RELEASE_USERNAME',
                        passwordVariable: 'GITEA_RELEASE_PASSWORD'
                    ])
                ]) {
                    sh 'make gitea-release'
                }
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}