image: atlassian/default-image:latest
clone:
  lfs: true
definitions:
  steps:
    - step: &prepare-ssh
        name: 'Prepare ssh Connection'
        script:
          - eval $(ssh-agent -s)
          - ssh-add /opt/atlassian/pipelines/agent/ssh/id_rsa
          - chmod 664 ~/.ssh/known_hosts
    - step: &composer-prod-install
        name: 'Composer Install'
        image: composer:2.2
        script:
          - composer install --no-dev --ignore-platform-reqs --no-progress --no-interaction
        artifacts:
          - bin/**
          - web/**
          - src/**
          - config/**
          - vendor/**
          # needed for deployer tool
          - build/**
    - step: &code-analysis
        name: 'Code Analysis'
        image: composer:2.0.8
        script:
          - composer install --ignore-platform-reqs --no-progress --no-interaction
          - if [ -e bin/phpstan ]; then php -d memory_limit=2G ./bin/phpstan analyse; fi
          - if [ -e bin/php-cs-fixer ]; then php -d memory_limit=2G ./bin/php-cs-fixer fix -v --dry-run --stop-on-violation --using-cache=no; fi
    - step: &production-deployment
        <<: *prepare-ssh
        name: 'Deployment - Production'
        image: composer:2.0.8
        deployment: production
        trigger: manual
        script:
          - apk add rsync --update
          - composer global require deployer/deployer:^6.8 deployer/recipes --dev --no-progress --no-interaction
          - /tmp/vendor/bin/dep --file=./build/deploy.php deploy -vv --hosts=production
pipelines:
  default:
    - parallel:
    - step:
        <<: *composer-prod-install
  branches:
    # pipeline for main branch. Allowed to deploy to staging
    main:
      - step:
          <<: *composer-prod-install
      - step:
          <<: *production-deployment
  custom:
    # define deployment separately again, so they can be manually called from branch -> run pipeline for a branch
    production-deployment:
      - step:
          <<: *composer-prod-install
      - step:
          <<: *production-deployment