|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + # Validate the squash merge commit message on pushes to main. |
| 14 | + # PR title validation is handled separately in pr-title.yml. |
| 15 | + commitlint: |
| 16 | + if: github.event_name == 'push' |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Checkout |
| 20 | + uses: actions/checkout@v6 |
| 21 | + with: |
| 22 | + fetch-depth: 2 |
| 23 | + |
| 24 | + - name: Install uv |
| 25 | + uses: astral-sh/setup-uv@v7 |
| 26 | + with: |
| 27 | + version: "latest" |
| 28 | + |
| 29 | + - name: Install Python and dependencies |
| 30 | + run: | |
| 31 | + uv python install 3.11 |
| 32 | + uv sync --all-extras |
| 33 | +
|
| 34 | + - name: Validate commit message |
| 35 | + run: uv run cz check --rev-range HEAD~1..HEAD |
| 36 | + |
| 37 | + lint: |
| 38 | + runs-on: ubuntu-latest |
| 39 | + strategy: |
| 40 | + matrix: |
| 41 | + python-version: ["3.11"] # TODO see https://github.com/mitre-attack/mitreattack-python/issues/176 |
| 42 | + steps: |
| 43 | + - uses: actions/checkout@v6 |
| 44 | + |
| 45 | + - name: Install the latest version of uv |
| 46 | + uses: astral-sh/setup-uv@v7 |
| 47 | + with: |
| 48 | + python-version: ${{ matrix.python-version }} |
| 49 | + |
| 50 | + - name: Install dependencies |
| 51 | + run: uv sync --all-extras |
| 52 | + |
| 53 | + - name: Lint with ruff |
| 54 | + run: uv run ruff check --output-format github |
| 55 | + |
| 56 | + - name: Check formatting with ruff |
| 57 | + run: uv run ruff format --check |
| 58 | + |
| 59 | + test: |
| 60 | + runs-on: ubuntu-latest |
| 61 | + strategy: |
| 62 | + matrix: |
| 63 | + python-version: ["3.11"] # TODO see https://github.com/mitre-attack/mitreattack-python/issues/176 |
| 64 | + steps: |
| 65 | + - uses: actions/checkout@v6 |
| 66 | + |
| 67 | + - name: Install the latest version of uv |
| 68 | + uses: astral-sh/setup-uv@v7 |
| 69 | + with: |
| 70 | + python-version: ${{ matrix.python-version }} |
| 71 | + |
| 72 | + - name: Install dependencies |
| 73 | + run: uv sync --all-extras |
| 74 | + |
| 75 | + - name: Run pytest |
| 76 | + run: uv run pytest --cov=mitreattack |
| 77 | + |
| 78 | + release: |
| 79 | + needs: [commitlint, lint, test] |
| 80 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 81 | + strategy: |
| 82 | + matrix: |
| 83 | + python-version: ["3.11"] # TODO see https://github.com/mitre-attack/mitreattack-python/issues/176 |
| 84 | + runs-on: ubuntu-latest |
| 85 | + # The concurrency block prevents multiple release jobs from running simultaneously for the same branch. |
| 86 | + # This is particularly useful for releases since you typically want sequential deployments (finish |
| 87 | + # current release before starting next) rather than canceling in-progress releases or running them |
| 88 | + # in parallel. |
| 89 | + concurrency: |
| 90 | + # Creates a concurrency group keyed by workflow name + "release" + branch name (e.g., "Continuous Delivery-release-main") |
| 91 | + group: ${{ github.workflow }}-release-${{ github.ref_name }} |
| 92 | + # If a release is already running and a new push triggers another, the new job waits rather than canceling the running one |
| 93 | + # If you changed cancel-in-progress to true, a new push would cancel the currently running release job. |
| 94 | + cancel-in-progress: false |
| 95 | + |
| 96 | + permissions: |
| 97 | + contents: write |
| 98 | + |
| 99 | + steps: |
| 100 | + # Note: We checkout the repository at the branch that triggered the workflow. |
| 101 | + # Python Semantic Release will automatically convert shallow clones to full clones |
| 102 | + # if needed to ensure proper history evaluation. However, we forcefully reset the |
| 103 | + # branch to the workflow sha because it is possible that the branch was updated |
| 104 | + # while the workflow was running, which prevents accidentally releasing un-evaluated |
| 105 | + # changes. |
| 106 | + - name: Setup | Checkout Repository on Release Branch |
| 107 | + uses: actions/checkout@v6 |
| 108 | + with: |
| 109 | + fetch-depth: 0 |
| 110 | + ref: ${{ github.ref_name }} |
| 111 | + |
| 112 | + - name: Setup | Force release branch to be at workflow sha |
| 113 | + run: | |
| 114 | + git reset --hard ${{ github.sha }} |
| 115 | +
|
| 116 | + - name: Setup | Install uv |
| 117 | + uses: astral-sh/setup-uv@v7 |
| 118 | + with: |
| 119 | + python-version: ${{ matrix.python-version }} |
| 120 | + |
| 121 | + - name: Setup | Install Python |
| 122 | + run: uv python install ${{ matrix.python-version }} |
| 123 | + |
| 124 | + - name: Setup | Install dependencies |
| 125 | + run: uv sync --all-extras |
| 126 | + |
| 127 | + - name: Semantic Release |
| 128 | + id: release |
| 129 | + uses: python-semantic-release/python-semantic-release@v10.5.3 |
| 130 | + with: |
| 131 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 132 | + # NOTE: git_committer_name and git_committer_email are optional |
| 133 | + # We omit them because, if set, they must be associated with the provided token |
| 134 | + # and we don't really care to have a specific committer for automated releases. |
| 135 | + |
| 136 | + - name: Upload to GitHub Release Assets |
| 137 | + uses: python-semantic-release/publish-action@v10.5.3 |
| 138 | + if: steps.release.outputs.released == 'true' |
| 139 | + with: |
| 140 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 141 | + tag: ${{ steps.release.outputs.tag }} |
| 142 | + |
| 143 | + - name: Upload distribution artifacts |
| 144 | + uses: actions/upload-artifact@v6 |
| 145 | + if: steps.release.outputs.released == 'true' |
| 146 | + with: |
| 147 | + name: distribution-artifacts |
| 148 | + path: dist |
| 149 | + if-no-files-found: error |
| 150 | + |
| 151 | + outputs: |
| 152 | + released: ${{ steps.release.outputs.released || 'false' }} |
| 153 | + |
| 154 | + publish: |
| 155 | + # 1. Separate out the deploy step from the publish step to run each step at |
| 156 | + # the least amount of token privilege |
| 157 | + # 2. Also, deployments can fail, and its better to have a separate job if you need to retry |
| 158 | + # and it won't require reversing the release. |
| 159 | + needs: release |
| 160 | + if: needs.release.outputs.released == 'true' |
| 161 | + runs-on: ubuntu-latest |
| 162 | + environment: release |
| 163 | + |
| 164 | + permissions: |
| 165 | + contents: read |
| 166 | + id-token: write |
| 167 | + |
| 168 | + steps: |
| 169 | + - name: Download build artifacts |
| 170 | + uses: actions/download-artifact@v7 |
| 171 | + id: artifact-download |
| 172 | + with: |
| 173 | + name: distribution-artifacts |
| 174 | + path: dist |
| 175 | + |
| 176 | + - name: Publish to PyPI |
| 177 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 178 | + with: |
| 179 | + packages-dir: dist |
| 180 | + print-hash: true |
| 181 | + verbose: true |
0 commit comments