ci.yml120 lines, 4.6 KB
| 1 | # Every pull request, and every push to main: lint, test, and build the images. |
| 2 | name: ci |
| 3 | |
| 4 | on: |
| 5 | pull_request: |
| 6 | push: |
| 7 | branches: [main] |
| 8 | # New vulnerabilities are found in code that hasn't changed: main gets |
| 9 | # checked every morning too. |
| 10 | schedule: |
| 11 | - cron: "0 6 * * *" |
| 12 | |
| 13 | # A new push to a pull request cancels the run that's still going for it. On |
| 14 | # main, every commit gets a complete run, and its images. |
| 15 | concurrency: |
| 16 | group: ci-${{ github.ref }} |
| 17 | cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} |
| 18 | |
| 19 | # Least privilege: every job can read the code, and nothing else, unless it |
| 20 | # asks for more. |
| 21 | permissions: |
| 22 | contents: read |
| 23 | |
| 24 | # Actions are pinned to a commit: a tag can be moved, a commit can't. |
| 25 | jobs: |
| 26 | lint: |
| 27 | runs-on: ubuntu-24.04 |
| 28 | steps: |
| 29 | - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 30 | - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 |
| 31 | with: |
| 32 | go-version-file: go.mod # the version in go.mod, nothing else |
| 33 | - run: go vet ./... |
| 34 | - run: go tool staticcheck ./... |
| 35 | # Every module is the one go.sum says it is, and nothing we call has a |
| 36 | # known vulnerability. |
| 37 | - run: go mod verify |
| 38 | - run: go tool govulncheck ./... |
| 39 | # buf lints our contracts and, on pull requests, refuses breaking changes, |
| 40 | # compared with the base branch. Nothing is pushed to the Buf registry. |
| 41 | - uses: bufbuild/buf-action@37ac33d8960807670a6e9719080e885197b09cc6 # v1.7.0 |
| 42 | with: |
| 43 | version: 1.72.0 |
| 44 | lint: true |
| 45 | format: false |
| 46 | push: false |
| 47 | pr_comment: false |
| 48 | |
| 49 | test: |
| 50 | runs-on: ubuntu-24.04 |
| 51 | steps: |
| 52 | - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 53 | - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 |
| 54 | with: |
| 55 | go-version-file: go.mod # the version in go.mod, nothing else |
| 56 | - run: make test |
| 57 | |
| 58 | # GitHub's Ubuntu runners have Docker, so testcontainers works as it does here. |
| 59 | integration: |
| 60 | runs-on: ubuntu-24.04 |
| 61 | timeout-minutes: 15 |
| 62 | steps: |
| 63 | - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 64 | - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 |
| 65 | with: |
| 66 | go-version-file: go.mod # the version in go.mod, nothing else |
| 67 | - run: make test-race |
| 68 | |
| 69 | # One image per service, in parallel. Layers are cached in GitHub's cache, so |
| 70 | # an unchanged go.mod skips the download. Pull requests only build; main |
| 71 | # pushes to GitHub's registry, which is why this job, alone, may write packages. |
| 72 | images: |
| 73 | needs: [lint, test, integration] |
| 74 | if: github.event_name != 'schedule' |
| 75 | runs-on: ubuntu-24.04 |
| 76 | permissions: |
| 77 | contents: read |
| 78 | packages: write |
| 79 | strategy: |
| 80 | matrix: |
| 81 | service: [wallet, auth, transfer, notification, gateway, webhook] |
| 82 | steps: |
| 83 | - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 |
| 84 | - if: github.ref == 'refs/heads/main' |
| 85 | uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0 |
| 86 | with: |
| 87 | registry: ghcr.io |
| 88 | username: ${{ github.actor }} |
| 89 | password: ${{ secrets.GITHUB_TOKEN }} |
| 90 | - uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 |
| 91 | with: |
| 92 | build-args: | |
| 93 | SERVICE=${{ matrix.service }} |
| 94 | VERSION=${{ github.sha }} |
| 95 | tags: ghcr.io/${{ github.repository }}/${{ matrix.service }}:${{ github.sha }} |
| 96 | push: ${{ github.ref == 'refs/heads/main' }} |
| 97 | cache-from: type=gha,scope=${{ matrix.service }} |
| 98 | cache-to: type=gha,mode=max,scope=${{ matrix.service }} |
| 99 | |
| 100 | # The whole system from its images, like the final demo, and episode 15's |
| 101 | # load test against it: a change that makes transfers slow fails here. |
| 102 | loadtest: |
| 103 | needs: [test] |
| 104 | runs-on: ubuntu-24.04 |
| 105 | timeout-minutes: 20 |
| 106 | steps: |
| 107 | - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 108 | - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 |
| 109 | with: |
| 110 | go-version-file: go.mod # the version in go.mod, nothing else |
| 111 | # Throwaway keys and certificates, readable by the containers' user: on |
| 112 | # Linux, that's not us. |
| 113 | - run: make keys certs && chmod -R a+rX secrets |
| 114 | - run: docker compose --profile app up -d --build --wait |
| 115 | env: |
| 116 | GATEWAY_IP_LIMIT: 1000 # k6 signs up fifty users, from one address |
| 117 | TRANSFER_RISK_MAX_COUNT: 1000 # and each sends far more than five transfers |
| 118 | - run: make loadtest |
| 119 | - if: failure() |
| 120 | run: docker compose --profile app logs |