server {
    listen 80;
    server_name mufin.life;
    return 301 https://mufin.life$request_uri;
}

server {
    server_name mufin.life;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
    }

    location /api {
        proxy_pass http://localhost:8081/api;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;

    }

    location /api/ws-connection {
        proxy_pass http://localhost:8081/api/ws-connection;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "Upgrade";
    }

    location /api/auth/ {
        proxy_pass http://localhost:3000/api/auth/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mufin.life/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mufin.life/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = mufin.life) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name mufin.life;
    return 404; # managed by Certbot
}
def BE_BUILD = false
def FE_BUILD = false
def CUR_BRANCH = ""
pipeline {
    agent any
    environment{
        DOCKERHUB_CREDENTIAL = credentials('DockerHub')
    }
    stages{
        stage('Checkout'){
            steps{
                git branch: 'develop', credentialsId: 'GitLab', url: "https://lab.ssafy.com/s10-fintech-finance-sub2/S10P22A502.git" 
                script{
                    def beChanges = false
                    def feChanges = false
                    def mybranch = sh(script: "git branch --show-current", returnStdout: true).trim()
                    def diffFiles = sh( script : "git diff --name-only HEAD HEAD~1",
                    returnStdout: true
                    ).trim().split("\n")
                    for(def file : diffFiles){
                        if (file.startsWith("backend/")){
                            beChanges = true
                        }
                        else if (file.startsWith("frontend/")){
                            feChanges = true
                        }
                    }
                    env.CUR_BRANCH = mybranch
                    env.BE_BUILD = beChanges 
                    env.FE_BUILD = feChanges
                }
            }
        }
        
        
        stage("spring env file download"){
            when {
                expression { return env.BE_BUILD == "true"}
            }
            steps {
                dir("./backend/src/main/resources/secret"){
                    withCredentials([file(credentialsId: 'application-secret', variable: 'envFiles')]){
                        script {
                            sh 'sudo cp $envFiles application-secret.yml'
                            sh 'sudo chmod 777 application-secret.yml'
                        }
                    }
                }
            }
        }
        
        stage("Compile Backend"){
            when {
                expression { return env.BE_BUILD == "true"}
            }
            steps{
                dir("./backend"){
                    sh "chmod +x gradlew"
                    // sh "./gradlew test -i"
                    sh "./gradlew clean build "
                }
            }
        }
        
        stage("Backend Docker build"){
            when {
                expression { return env.BE_BUILD == "true"}
            }
            steps{
                dir("./backend"){
                    sh "docker build -t slcloe/jasminback ."
                }
            }
        }
        
        stage("Backend Docker push"){
            when {
                expression { return env.BE_BUILD == "true"}
            }
            steps{
                dir("./backend"){
                    sh "docker login -u ${DOCKERHUB_CREDENTIAL_USR} -p ${DOCKERHUB_CREDENTIAL_PSW}"
                    sh "docker push slcloe/jasminback"
                }
            }
        }
        
        stage("Backend Docker pull"){
            when {
                expression { return env.BE_BUILD == "true"}
            }
            steps{
                script{
                    sh "docker pull slcloe/jasminback"
                }
            }
        }
        

        
        stage("front env file download"){
            when {
                expression { return env.FE_BUILD == "true"}
            }
            steps {
                dir("./frontend"){
                    withCredentials([file(credentialsId: 'frontEnvFile', variable: 'envFile')]){
                        script {
                            sh 'sudo cp ${envFile} .env'
                            sh 'sudo chmod +x .env'
                        }
                    }
                }
            }
        }
        
        stage("Frontend Docker Container Down & Up"){
            when {
                expression { return env.FE_BUILD == "true"}
            }
            steps{
                dir("./frontend"){
                    sh """
                    sudo docker-compose down
                    sudo docker-compose up -d --build
                    """
                }
            }
        }
        
        stage("Backend Docker Container Down & Up"){
            when {
                expression { return env.BE_BUILD == "true"}
            }
            steps{
                dir("./backend"){
                    sh """
                    sudo docker-compose down
                    sudo docker-compose up -d
                    """
                }
            }
        }
        
    }
    
    
    post {
        success {
            withCredentials([string(credentialsId: 'Discord-Webhook', variable: 'DISCORD')]) {
                discordSend description: """
                제목 : ${currentBuild.displayName}
                결과 : ${currentBuild.result}
                실행 시간 : ${currentBuild.duration / 1000}s
                FE빌드 : ${env.FE_BUILD}
                BE빌드 : ${env.BE_BUILD}
                빌드 브랜치 : ${env.CUR_BRANCH}
                """,
                link: env.BUILD_URL, result: currentBuild.currentResult, 
                title: "${env.JOB_NAME} : ${currentBuild.displayName} 성공", 
                webhookURL: "$DISCORD",
                footer: "테스트 빌드가 성공했습니다."
            }
        }
        failure {
            withCredentials([string(credentialsId: 'Discord-Webhook', variable: 'DISCORD')]) {
                discordSend description: """
                제목 : ${currentBuild.displayName}
                결과 : ${currentBuild.result}
                실행 시간 : ${currentBuild.duration / 1000}s
                FE빌드 : ${env.FE_BUILD}
                BE빌드 : ${env.BE_BUILD}
                """,
                link: env.BUILD_URL, result: currentBuild.currentResult, 
                title: "${env.JOB_NAME} : ${currentBuild.displayName} 실패", 
                webhookURL: "$DISCORD",
                footer: "테스트 빌드가 실패했습니다."
            }
        }
    }
}
REACT_APP_WEATHER_API_KEY = 'cd7b27eabcf3be0c3d96c4012b40f1dd'
REACT_APP_WEATHER_API = 'https://api.openweathermap.org/data/2.5/weather'
NEXT_PUBLIC_BASE_URL = 'https://mufin.life'
NEXTAUTH_SECRET='Ex0kSzMKGh7BoKULqM7sNZ1Wat0YcLfDhncAC2hCS7c='
NEXTAUTH_URL = 'https://mufin.life'
NEXT_PUBLIC_GPT_API_KEY = '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'
NEXT_PUBLIC_GPT_API='https://api.openai.com/v1/completions'
spring:
  datasource:
    driver-class-name: *driver-class-name*
    url: *url*
    username: *username*
    password: *password*

  redis:
    port: port
    host: *host*
    password: *password*

jwt:
  header: Authorization
  secret: *secret*
  token-validity-in-seconds: 86400

weatherKey: *weatherKey*
schedule:
  cron:
    start: 0 0 9 ? * *
    end : 0 0 17 ? * *
    saving : 0 0 0 ? * *
    loan : 0 0 1 ? * *
    interest : 0 0 2 ? * *
    rank : 0 0 0/1 * * *
    test: 0 0/1 * * * ?
  use : true

cloud:
  aws:
    s3:
      bucket: *bucket*
    stack.auto: false
    region.static: ap-northeast-2
    credentials:
      access-key: *access-key*
      secret-key: *secret-key*

receipt:
  apiURL: *apiURL*
  secretKey: *secretKey*