bitbucket-pipelines.yml
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Bitbucket Pipelines workflow for iac-deployments =============================

# Pipeline definitions ---------------------------------------------------------

definitions:
  steps:
    - step: &gated-entrypoint
        name: Gated pipeline entrypoint
        runs-on:
          - self.hosted
          - linux.shell
          - acme.linux.prod
        script:
          - |
            echo "Pipeline execution requires manual approval"
            echo "Review and trigger the next step to proceed."

    - step: &run-diagnostics
        name: Run diagnostics
        runs-on:
          - self.hosted
          - linux.shell
          - acme.linux.prod
        script:
          - python3.12 pipeline.py run-diagnostics

    - step: &run-scan
        name: Run security scans
        runs-on:
          - self.hosted
          - linux.shell
          - acme.linux.prod
        script:
          - python3.12 pipeline.py run-scan

    - step: &run-driftcheck
        name: Run drift check
        runs-on:
          - self.hosted
          - linux.shell
          - acme.linux.prod
        oidc: true
        script:
          - |
            export AWS_WEB_IDENTITY_TOKEN_FILE=$(mktemp)
            echo $BITBUCKET_STEP_OIDC_TOKEN > $AWS_WEB_IDENTITY_TOKEN_FILE
            trap 'rm -f $AWS_WEB_IDENTITY_TOKEN_FILE' EXIT
            python3.12 pipeline.py run-driftcheck

    - step: &validate-stack
        name: Validate changed stack
        runs-on:
          - self.hosted
          - linux.shell
          - acme.linux.prod
        oidc: true
        script:
          - |
            export AWS_WEB_IDENTITY_TOKEN_FILE=$(mktemp)
            echo $BITBUCKET_STEP_OIDC_TOKEN > $AWS_WEB_IDENTITY_TOKEN_FILE
            trap 'rm -f $AWS_WEB_IDENTITY_TOKEN_FILE' EXIT
            python3.12 pipeline.py validate-stack

    - step: &plan-stack
        name: Plan changed stack
        runs-on:
          - self.hosted
          - linux.shell
          - acme.linux.prod
        oidc: true
        script:
          - |
            export AWS_WEB_IDENTITY_TOKEN_FILE=$(mktemp)
            echo $BITBUCKET_STEP_OIDC_TOKEN > $AWS_WEB_IDENTITY_TOKEN_FILE
            trap 'rm -f $AWS_WEB_IDENTITY_TOKEN_FILE' EXIT
            python3.12 pipeline.py plan-stack

    - step: &execute-stack
        name: Execute changed stack
        trigger: manual
        runs-on:
          - self.hosted
          - linux.shell
          - acme.linux.prod
        oidc: true
        script:
          - |
            echo "Stack execution approved on $(date -u)"
            echo ""
            echo "approver uuid  : $BITBUCKET_STEP_TRIGGERER_UUID"
            echo "build number   : $BITBUCKET_BUILD_NUMBER"
            echo "branch name    : $BITBUCKET_BRANCH"
            echo "commit hash    : $BITBUCKET_COMMIT"
            echo ""
            export AWS_WEB_IDENTITY_TOKEN_FILE=$(mktemp)
            echo $BITBUCKET_STEP_OIDC_TOKEN > $AWS_WEB_IDENTITY_TOKEN_FILE
            trap 'rm -f $AWS_WEB_IDENTITY_TOKEN_FILE' EXIT
            python3.12 pipeline.py execute-stack

# Pipeline Triggers ------------------------------------------------------------

pipelines:
  # PR — security scan, validate and plan changed stack
  pull-requests:
    "**":
      - step: *run-scan
      - step: *validate-stack
      - step: *plan-stack

  # Main — execute or destroy changed stack
  branches:
    main:
      - step: *gated-entrypoint
      - step: *execute-stack

  # On-demand — run by manually triggering pipeline
  custom:
    run-scan:
      - step: *run-scan
    run-diagnostics:
      - step: *run-diagnostics
    run-driftcheck:
      - step: *run-driftcheck