image: atlassian/default-image:3

definitions:
  steps:
    - step: &build-and-test
        name: Build and Test
        script:
          - npm install
          - npm test
        artifacts:
          - node_modules/**
          - src/**
          - scripts/**
          - appspec.yml
          - package*.json

pipelines:
  default:
    - step: *build-and-test
  
  branches:
    main:
      - step: *build-and-test
      - step:
          name: Deploy to AWS CodeDeploy
          script:
            # Install AWS CLI
            - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
            - unzip awscliv2.zip
            - ./aws/install
            
            # Configure AWS credentials
            - aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
            - aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
            - aws configure set default.region $AWS_DEFAULT_REGION
            
            # Create deployment bundle
            - zip -r deployment.zip . -x "*.git*"
            
            # Upload to S3
            - aws s3 cp deployment.zip s3://$AWS_S3_BUCKET/lambda-load-test/deployment.zip
            
            # Create CodeDeploy deployment
            - aws deploy create-deployment 
                --application-name lambda-load-test
                --deployment-group-name lambda-load-test-group
                --s3-location bucket=$AWS_S3_BUCKET,bundleType=zip,key=lambda-load-test/deployment.zip
                --file-exists-behavior OVERWRITE
