pipeline {
    agent none
    stages {
        stage('Build') {
            parallel {
                stage('Build backend-glibc') {
                    agent { docker { image 'rust:bullseye' }}
                    steps {
                        dir('backend') {
                            sh 'cargo build --release'
                            dir('target/release') {
                                stash includes: 'backend_rust', name: 'backend-glibc'
                            }
                        }
                    }
                }
                stage('Build backend-muslc') {
                    agent { docker { image 'rust:alpine' }}
                    steps {
                        dir('backend') {
                            sh 'apk add pkgconf musl-dev'
                            sh 'cargo build --release'
                            dir('target/release') {
                                stash includes: 'backend_rust', name: 'backend-muslc'
                            }
                        }
                    }
                }
                stage('Build frontend') {
                    agent { docker { image 'node:current-alpine' }}
                    options {
                        ansiColor('xterm')
                    }
                    steps {
                        dir('frontend') {
                            sh 'yarn install --frozen-lockfile'
                            sh 'yarn lint'
                            sh 'yarn build'
                            dir('dist') {
                                stash name: 'frontend'
                            }
                        }
                    }
                }
            }
        }
        stage('Package') {
            agent { docker {
                image 'alpine:latest'
                reuseNode true
            }}
            steps {
                sh 'apk add tar xz'
                dir('static') {
                    unstash name: 'frontend'
                }
                unstash name: 'backend-glibc'
                sh 'tar -cvJf linux-x64-glibc.tar.xz --transform="flags=r;s|backend_rust|server|" backend_rust static'
                unstash name: 'backend-muslc'
                sh 'tar -cvJf linux-x64-muslc.tar.xz --transform="flags=r;s|backend_rust|server|" backend_rust static'
                archiveArtifacts artifacts: '*.tar.xz', allowEmptyArchive: false, onlyIfSuccessful: true
            }
        }
        stage('Release') {
            agent any
            when { buildingTag() }
            environment {
                TOKEN = credentials('abd7020c-43d6-485b-ae09-2f9b484d9c15')
            }
            steps {
                script {
                    def release_body = [
                        'name': "Version ${TAG_NAME}",
                        'tag_name': "${TAG_NAME}",
                        'is_draft': false,
                        'is_prerelease': false
                    ]
                    def release_body_str = writeJSON returnText: true, json: release_body
                    def release_resp = httpRequest url: 'https://gitea.mattv.de/api/v1/repos/root/fileserver/releases',
                        acceptType: 'APPLICATION_JSON',
                        contentType: 'APPLICATION_JSON',
                        customHeaders: [[name: 'Authorization', value: "token ${TOKEN_PSW}", maskValue: true]],
                        httpMode: 'POST',
                        requestBody: release_body_str
                    def release_json = readJSON text: release_resp.content
                    httpRequest url: "https://gitea.mattv.de/api/v1/repos/root/fileserver/releases/${release_json.id}/assets?name=linux-x64-glibc.tar.xz",
                        httpMode: 'POST',
                        acceptType: 'APPLICATION_JSON',
                        customHeaders: [[name: 'Authorization', value: "token ${TOKEN_PSW}", maskValue: true]],
                        formData: [[contentType: 'application/octet-stream', name: 'attachment', fileName: 'linux-x64-glibc.tar.xz', uploadFile: 'linux-x64-glibc.tar.xz']]
                    httpRequest url: "https://gitea.mattv.de/api/v1/repos/root/fileserver/releases/${release_json.id}/assets?name=linux-x64-muslc.tar.xz",
                        httpMode: 'POST',
                        acceptType: 'APPLICATION_JSON',
                        customHeaders: [[name: 'Authorization', value: "token ${TOKEN_PSW}", maskValue: true]],
                        formData: [[contentType: 'application/octet-stream', name: 'attachment', fileName: 'linux-x64-muslc.tar.xz', uploadFile: 'linux-x64-muslc.tar.xz']]
                }
            }
        }
    }
}