pipeline {
    agent {
        node {
            label 'devenv'
        }
    }
    triggers {
        pollSCM('H/10 * * * *')
    }
    stages {
        stage('Test devenv') {
            steps {
                sh 'devenv test'
            }
        }
        stage('Parallel Build and Deploy') {
            parallel {
                stage('Android') {
                    stages {
                        stage('Build') {
                            steps {
                                sh 'devenv shell flutter build apk'
                            }
                        }
                        stage('Deploy') {
                            stages {
                                stage('Checkout F-Droid Repo') {
                                    steps {
                                        dir("fdroid-repo") {
                                            git branch: 'master', url: "https://gitlab.julian-mutter.de/julian/fdroid-frajul.git", credentialsId: "gitea"
                                        }
                                    }
                                }

                                stage('Decrypt keys') {
                                    steps {
                                        dir("fdroid-repo") {
                                            sh 'devenv shell ./decrypt.sh'
                                        }
                                    }
                                }

                                stage('Copy APK') {
                                    steps {
                                        sh "cp build/app/outputs/apk/release/app-release.apk fdroid-repo/fdroid/repo/sheetless.apk"
                                    }
                                }

                                stage('Update Metadata') {
                                    steps {
                                        dir("fdroid-repo") {
                                            sh 'devenv shell ./update-repo.sh'
                                        }
                                    }
                                }

                                stage('Push updates') {
                                    steps {
                                        dir("fdroid-repo") {
                                            withCredentials([gitUsernamePassword(credentialsId: 'gitea')]) {
                                                sh '''
                                                    git config --global user.email "jenkins@comumail.de"
                                                    git config --global user.name "Jenkins CI"
                                                    ./push-updates.sh 
                                                '''
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                stage('Web') {
                    stages {
                        stage('Build') {
                            steps {
                                sh 'devenv shell flutter build web --release'
                                sh 'docker build -t harbor.julian-mutter.de/sheetless/sheetless-frontend .'
                            }
                        }
                        stage('Deploy') {
                            steps {
                                withDockerRegistry([credentialsId: 'harbor', url: 'https://harbor.julian-mutter.de']) {
                                    sh 'docker push harbor.julian-mutter.de/sheetless/sheetless-frontend'
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    post {
        always {
            step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: emailextrecipients([developers(), requestor()])])
        }
    }
}
