|
| 1 | +name: Contract Tests |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, develop] |
| 6 | + paths: |
| 7 | + - 'src/**' |
| 8 | + - 'test/contracts/**' |
| 9 | + - '.github/workflows/contract-tests.yml' |
| 10 | + pull_request: |
| 11 | + branches: [main] |
| 12 | + repository_dispatch: |
| 13 | + types: [pact-changed] |
| 14 | + |
| 15 | +env: |
| 16 | + NODE_VERSION: '20' |
| 17 | + PACT_BROKER_URL: ${{ secrets.PACT_BROKER_URL }} |
| 18 | + PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }} |
| 19 | + |
| 20 | +jobs: |
| 21 | + consumer-contracts: |
| 22 | + name: Consumer Contract Tests |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + - uses: actions/setup-node@v4 |
| 27 | + with: |
| 28 | + node-version: ${{ env.NODE_VERSION }} |
| 29 | + cache: 'npm' |
| 30 | + - run: npm ci |
| 31 | + - name: Run consumer contract tests |
| 32 | + run: npm run test:consumer |
| 33 | + env: |
| 34 | + CONSUMER_VERSION: ${{ github.sha }} |
| 35 | + GIT_BRANCH: ${{ github.head_ref || github.ref_name }} |
| 36 | + - name: Upload pact files |
| 37 | + if: always() |
| 38 | + uses: actions/upload-artifact@v4 |
| 39 | + with: |
| 40 | + name: pact-files |
| 41 | + path: pact/contracts/ |
| 42 | + retention-days: 30 |
| 43 | + - name: Publish pacts to Pact Broker |
| 44 | + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository |
| 45 | + run: npm run pact:publish |
| 46 | + env: |
| 47 | + CONSUMER_VERSION: ${{ github.sha }} |
| 48 | + GIT_BRANCH: ${{ github.head_ref || github.ref_name }} |
| 49 | + - name: Can I Deploy? (Consumer) |
| 50 | + if: github.event_name != 'pull_request' |
| 51 | + run: | |
| 52 | + npx pact-broker can-i-deploy \ |
| 53 | + --pacticipant ApiGateway \ |
| 54 | + --version ${{ github.sha }} \ |
| 55 | + --to-environment production \ |
| 56 | + --broker-base-url ${{ env.PACT_BROKER_URL }} \ |
| 57 | + --broker-token ${{ env.PACT_BROKER_TOKEN }} |
| 58 | +
|
| 59 | + provider-verification: |
| 60 | + name: Provider Verification |
| 61 | + runs-on: ubuntu-latest |
| 62 | + needs: consumer-contracts |
| 63 | + if: always() && (needs.consumer-contracts.result == 'success' || github.event_name == 'repository_dispatch') |
| 64 | + services: |
| 65 | + user-service: |
| 66 | + image: strellerminds/user-service:latest |
| 67 | + ports: |
| 68 | + - 3001:3001 |
| 69 | + env: |
| 70 | + NODE_ENV: test |
| 71 | + options: >- |
| 72 | + --health-cmd "curl -f http://localhost:3001/health || exit 1" |
| 73 | + --health-interval 10s |
| 74 | + --health-timeout 5s |
| 75 | + --health-retries 5 |
| 76 | + auth-service: |
| 77 | + image: strellerminds/auth-service:latest |
| 78 | + ports: |
| 79 | + - 3002:3002 |
| 80 | + env: |
| 81 | + NODE_ENV: test |
| 82 | + options: >- |
| 83 | + --health-cmd "curl -f http://localhost:3002/health || exit 1" |
| 84 | + --health-interval 10s |
| 85 | + --health-timeout 5s |
| 86 | + --health-retries 5 |
| 87 | + postgres: |
| 88 | + image: postgres:15 |
| 89 | + env: |
| 90 | + POSTGRES_USER: test |
| 91 | + POSTGRES_PASSWORD: test |
| 92 | + POSTGRES_DB: testdb |
| 93 | + options: >- |
| 94 | + --health-cmd pg_isready |
| 95 | + --health-interval 10s |
| 96 | + --health-timeout 5s |
| 97 | + --health-retries 5 |
| 98 | + steps: |
| 99 | + - uses: actions/checkout@v4 |
| 100 | + - uses: actions/setup-node@v4 |
| 101 | + with: |
| 102 | + node-version: ${{ env.NODE_VERSION }} |
| 103 | + cache: 'npm' |
| 104 | + - run: npm ci |
| 105 | + - run: sleep 10 |
| 106 | + - name: Verify UserService |
| 107 | + run: npm run test:provider -- --testPathPattern=user-service |
| 108 | + env: |
| 109 | + CI: true |
| 110 | + USER_SERVICE_URL: http://localhost:3001 |
| 111 | + PROVIDER_VERSION: ${{ github.sha }} |
| 112 | + GIT_BRANCH: ${{ github.head_ref || github.ref_name }} |
| 113 | + - name: Verify AuthService |
| 114 | + run: npm run test:provider -- --testPathPattern=auth-service |
| 115 | + env: |
| 116 | + CI: true |
| 117 | + AUTH_SERVICE_URL: http://localhost:3002 |
| 118 | + PROVIDER_VERSION: ${{ github.sha }} |
| 119 | + GIT_BRANCH: ${{ github.head_ref || github.ref_name }} |
| 120 | + - name: Can I Deploy? (Provider) |
| 121 | + if: github.event_name != 'pull_request' |
| 122 | + run: | |
| 123 | + npx pact-broker can-i-deploy \ |
| 124 | + --pacticipant UserService \ |
| 125 | + --version ${{ github.sha }} \ |
| 126 | + --to-environment production \ |
| 127 | + --broker-base-url ${{ env.PACT_BROKER_URL }} \ |
| 128 | + --broker-token ${{ env.PACT_BROKER_TOKEN }} |
| 129 | +
|
| 130 | + generate-docs: |
| 131 | + name: Generate Contract Documentation |
| 132 | + runs-on: ubuntu-latest |
| 133 | + needs: [consumer-contracts, provider-verification] |
| 134 | + if: github.ref == 'refs/heads/main' && success() |
| 135 | + steps: |
| 136 | + - uses: actions/checkout@v4 |
| 137 | + - uses: actions/setup-node@v4 |
| 138 | + with: |
| 139 | + node-version: ${{ env.NODE_VERSION }} |
| 140 | + cache: 'npm' |
| 141 | + - run: npm ci |
| 142 | + - uses: actions/download-artifact@v4 |
| 143 | + with: |
| 144 | + name: pact-files |
| 145 | + path: pact/contracts/ |
| 146 | + - run: npm run docs:generate |
| 147 | + - uses: actions/upload-artifact@v4 |
| 148 | + with: |
| 149 | + name: contract-docs |
| 150 | + path: docs/contracts/ |
| 151 | + retention-days: 90 |
| 152 | + - name: Deploy docs to GitHub Pages |
| 153 | + if: github.ref == 'refs/heads/main' |
| 154 | + uses: peaceiris/actions-gh-pages@v3 |
| 155 | + with: |
| 156 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 157 | + publish_dir: ./docs/contracts |
| 158 | + destination_dir: contract-docs |
0 commit comments