|
| 1 | +name: Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: ['v*'] |
| 6 | + |
| 7 | +env: |
| 8 | + CARGO_TERM_COLOR: always |
| 9 | + RUST_BACKTRACE: 1 |
| 10 | + |
| 11 | +jobs: |
| 12 | + ci: |
| 13 | + name: CI |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v6 |
| 17 | + |
| 18 | + - name: Install Rust |
| 19 | + uses: dtolnay/rust-toolchain@stable |
| 20 | + with: |
| 21 | + components: rustfmt, clippy |
| 22 | + |
| 23 | + - name: Cache cargo |
| 24 | + uses: Swatinem/rust-cache@v2 |
| 25 | + |
| 26 | + - name: Check formatting |
| 27 | + run: cargo fmt --all -- --check |
| 28 | + |
| 29 | + - name: Build workspace |
| 30 | + run: cargo build --workspace --all-features |
| 31 | + |
| 32 | + - name: Run tests |
| 33 | + run: cargo test --workspace --all-features |
| 34 | + |
| 35 | + - name: Clippy |
| 36 | + run: cargo clippy --workspace --all-features -- -D warnings |
| 37 | + |
| 38 | + - name: Build docs |
| 39 | + run: cargo doc --workspace --no-deps --all-features |
| 40 | + env: |
| 41 | + RUSTDOCFLAGS: -D warnings |
| 42 | + |
| 43 | + - name: Install cargo-audit |
| 44 | + run: cargo install cargo-audit --locked || true |
| 45 | + |
| 46 | + - name: Audit dependencies |
| 47 | + run: cargo audit --ignore RUSTSEC-2023-0071 |
| 48 | + |
| 49 | + - name: Install cargo-deny |
| 50 | + run: cargo install cargo-deny --locked || true |
| 51 | + |
| 52 | + - name: Check dependency policies |
| 53 | + run: cargo deny check |
| 54 | + |
| 55 | + publish: |
| 56 | + name: Publish to crates.io |
| 57 | + needs: [ci] |
| 58 | + runs-on: ubuntu-latest |
| 59 | + environment: release |
| 60 | + permissions: |
| 61 | + id-token: write |
| 62 | + contents: read |
| 63 | + steps: |
| 64 | + - uses: actions/checkout@v6 |
| 65 | + |
| 66 | + - name: Install Rust |
| 67 | + uses: dtolnay/rust-toolchain@stable |
| 68 | + |
| 69 | + - name: Cache cargo |
| 70 | + uses: Swatinem/rust-cache@v2 |
| 71 | + |
| 72 | + - name: Validate tag matches Cargo.toml version |
| 73 | + run: | |
| 74 | + TAG="${GITHUB_REF#refs/tags/v}" |
| 75 | + CORE_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "cdx-core") | .version') |
| 76 | + CLI_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "cdx-cli") | .version') |
| 77 | + echo "Tag version: $TAG" |
| 78 | + echo "cdx-core version: $CORE_VERSION" |
| 79 | + echo "cdx-cli version: $CLI_VERSION" |
| 80 | + if [ "$TAG" != "$CORE_VERSION" ]; then |
| 81 | + echo "ERROR: Tag v$TAG does not match cdx-core version $CORE_VERSION" |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + if [ "$TAG" != "$CLI_VERSION" ]; then |
| 85 | + echo "ERROR: Tag v$TAG does not match cdx-cli version $CLI_VERSION" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | +
|
| 89 | + - name: Dry run cdx-core |
| 90 | + run: cargo publish --dry-run -p cdx-core |
| 91 | + |
| 92 | + - name: Dry run cdx-cli |
| 93 | + run: cargo publish --dry-run -p cdx-cli |
| 94 | + |
| 95 | + - name: Authenticate with crates.io |
| 96 | + uses: rust-lang/crates-io-auth-action@v1 |
| 97 | + |
| 98 | + - name: Publish cdx-core |
| 99 | + run: cargo publish -p cdx-core |
| 100 | + |
| 101 | + - name: Wait for crates.io index update |
| 102 | + run: sleep 30 |
| 103 | + |
| 104 | + - name: Publish cdx-cli |
| 105 | + run: cargo publish -p cdx-cli |
| 106 | + |
| 107 | + release: |
| 108 | + name: Create GitHub Release |
| 109 | + needs: [publish] |
| 110 | + runs-on: ubuntu-latest |
| 111 | + permissions: |
| 112 | + contents: write |
| 113 | + steps: |
| 114 | + - uses: actions/checkout@v6 |
| 115 | + |
| 116 | + - name: Extract changelog for this version |
| 117 | + id: changelog |
| 118 | + run: | |
| 119 | + TAG="${GITHUB_REF#refs/tags/v}" |
| 120 | + # Extract the section for this version from CHANGELOG.md |
| 121 | + # Matches from "## [X.Y.Z]" until the next "## [" or end of file |
| 122 | + CHANGELOG=$(awk -v ver="$TAG" ' |
| 123 | + /^## \[/ { |
| 124 | + if (found) exit |
| 125 | + if ($0 ~ "\\[" ver "\\]") found=1 |
| 126 | + next |
| 127 | + } |
| 128 | + found { print } |
| 129 | + ' CHANGELOG.md) |
| 130 | + if [ -z "$CHANGELOG" ]; then |
| 131 | + CHANGELOG="Release v$TAG" |
| 132 | + fi |
| 133 | + # Write to file to avoid delimiter issues |
| 134 | + echo "$CHANGELOG" > /tmp/changelog.txt |
| 135 | +
|
| 136 | + - name: Create GitHub Release |
| 137 | + uses: softprops/action-gh-release@v2 |
| 138 | + with: |
| 139 | + body_path: /tmp/changelog.txt |
| 140 | + generate_release_notes: false |
0 commit comments