Your IP : 172.16.0.223


Current Path : /var/app/current/
Upload File :
Current File : /var/app/current/.gitlab-ci.yml

stages:
  - deploy

deploy_aws:
  stage: deploy
  image: python:3.6-stretch
  before_script:
    - pip install awsebcli --upgrade --user
    - |
      if [[ "$CI_COMMIT_BRANCH" == "staging" ]]; then
        git checkout staging
        git pull
      else
        git checkout master
        git pull
      fi
  script:
    - |
      echo "Current working directory: $(pwd)"
      echo "Git version: $(git --version)"
      aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
      aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
      eb --version
    - eb deploy Enquete-sys-env-prod-env --nohang
  only:
    - master

deploy_staging:
  tags:
    - development
  stage: deploy
  image: ubuntu:20.04
  before_script:
    - sudo apt-get update && sudo apt-get install -y openssh-client git
    - eval $(ssh-agent -s)    
    - mkdir -p ~/.ssh

    # Ensure SSH key is available for GitLab access
    - mkdir -p ~/.ssh
    - echo "$SSH_KEY" | tr -d '\r' | ssh-add -
    - ssh-add -l

    # Add Git server's SSH key to known_hosts to avoid authenticity check prompts
    - ssh-keyscan -H gitlab.chvps.aozora-okinawa.com >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    # SSH into the server and run deployment commands
    - |
      ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP << 'EOF'
        echo "SSH connection established, starting deployment..."

        # Navigate to the Laravel app directory (check if exists)
        cd /var/www/html/enquete/fukushisurvey.jp/quill || exit 1

        eval $(ssh-agent -s)  

        ssh-add ~/.ssh/gitlab_rsa

        # Stash unwanted modified and added files
        git add .
        git commit -m "update"
        git stash
        
        # Ensure we're on the correct branch (and avoid potential issues with uncommitted changes)
        git checkout staging
        git pull origin staging
        
        # Update the Permissions
        sudo chmod -R 775 storage/logs
        sudo chmod -R 775 bootstrap/cache

        # Install Composer dependencies
        composer install --no-dev --optimize-autoloader
        
        # Run database migrations
        php artisan migrate
        
        # Clear cache and optimize config
        php artisan cache:clear
        php artisan config:clear
        
        echo "Deployment to staging completed successfully!"
      EOF
  only:
    - staging