Skip to content

Cargo checks

Cargo checks #1

Workflow file for this run

name: Cargo checks
on:
pull_request:
push:
branches: [main]
jobs:
cargo-deny:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # 4.2.2
- uses: EmbarkStudios/cargo-deny-action@f2ba7abc2abebaf185c833c3961145a3c275caad # 2.0.13
with:
command: check advisories bans sources # licenses
version-and-changelog:
runs-on: ubuntu-latest
strategy:
fail-fast: false
# max-parallel: 1
matrix:
# crate: [ddcommon, ddtelemetry, ddsketch, Library-config, Datadog-crashtracker, cc_utils, data-pipeline, datadog-trace-protobuf, datadog-trace-normalization, datadog-trace-utils, dogstatsd-client, tinybytes, datadog-log]
crate: [ddcommon]
env:
CRATE: ${{ matrix.crate }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # 4.2.2
with:
fetch-depth: 0
fetch-tags: true
- uses: r7kamura/rust-problem-matchers@9fe7ca9f6550e5d6358e179d451cc25ea6b54f98 #v1.5.0
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@f13886b937689c021905a6b90929199931d60db1 # v2.8.1
with:
shared-key: stable-cache
save-if: false
- uses: taiki-e/cache-cargo-install-action@v2
with:
tool: tomlq
- name: Extract version from manifest
run: |
CRATE_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "'"$CRATE"'") | .version')
echo "CRATE_VERSION=$CRATE_VERSION" >> $GITHUB_ENV
# this could be enabled when defining dependencies in Cargo.toml
# - name: Enforce version in `workspace.dependencies` matches the latest version
# run: |
# SPECIFIED_VERSION=$(tq "workspace.dependencies.$CRATE.version" -r --file ./Cargo.toml)
# echo "Package version: $CRATE_VERSION";
# echo "Specified version: $SPECIFIED_VERSION";
# test "$CRATE_VERSION" = "$SPECIFIED_VERSION" || test "=$CRATE_VERSION" = "$SPECIFIED_VERSION"
- name: Enforce version in CHANGELOG.md matches the version in manifest
run: |
MANIFEST_PATH=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "'"$CRATE"'") | .manifest_path')
DIR_TO_CRATE=$(dirname "$MANIFEST_PATH")
VERSION_IN_CHANGELOG=$(awk -F' ' '/^## [0-9]+\.[0-9]+\.[0-9]+/{print $2; exit}' "$DIR_TO_CRATE/CHANGELOG.md")
echo "Package version: $CRATE_VERSION";
echo "Changelog version: $VERSION_IN_CHANGELOG";
test "$CRATE_VERSION" = "$VERSION_IN_CHANGELOG"
- name: Ensure manifest and CHANGELOG are properly updated
if: >
github.event_name == 'pull_request' &&
!startsWith(github.event.pull_request.title, 'chore') &&
!startsWith(github.event.pull_request.title, 'refactor') &&
!startsWith(github.event.pull_request.title, 'deps') &&
!startsWith(github.event.pull_request.title, 'docs') &&
!contains(github.event.pull_request.labels.*.name, 'internal-change')
run: |
git fetch origin main:main
git fetch origin ${{ github.event.pull_request.base.ref }}:${{ github.event.pull_request.base.ref }}
./scripts/ensure-version-bump-and-changelog.sh
env:
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
PR_BASE: ${{ github.event.pull_request.base.ref }}
detect-changes:
runs-on: ubuntu-latest
outputs:
changed-packages: ${{ steps.detect.outputs.packages }}
has-changes: ${{ steps.detect.outputs.has-changes }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # 4.2.2
with:
fetch-depth: 0 # Need full history for cargo release changes
- name: Install cargo-release
uses: taiki-e/install-action@d6d752794628f1e1fffa3c4d3c8874e06f043d50 # 2.62.15
with:
tool: cargo-release
- id: detect
name: Detect changed packages and their baseline tags
run: |
# Get changed packages from cargo release changes
# Extract package names from lines like "Changes for {package} from"
packages_list=$(cargo release changes 2>&1 | grep "Changes for" | sed 's/.*Changes for \([^ ]*\) from.*/\1/' | sort -u)
if [ -z "$packages_list" ]; then
echo "has-changes=false" >> $GITHUB_OUTPUT
echo "packages=[]" >> $GITHUB_OUTPUT
echo "No changed packages detected"
else
echo "has-changes=true" >> $GITHUB_OUTPUT
# Build JSON array with package and baseline-rev for each package
packages_json="[]"
while IFS= read -r pkg; do
[ -z "$pkg" ] && continue
# Find the latest tag for this package (format: {package}-v{version})
latest_tag=$(git tag -l "${pkg}-v*" --sort=-version:refname | head -n 1)
if [ -z "$latest_tag" ]; then
echo "Warning: No tag found for package $pkg, skipping baseline"
baseline=""
else
baseline="$latest_tag"
echo "Package $pkg -> baseline $baseline"
fi
# Add to JSON array with compact output (-c flag)
packages_json=$(echo "$packages_json" | jq -c --arg pkg "$pkg" --arg baseline "$baseline" '. += [{"package": $pkg, "baseline-rev": $baseline}]')
done <<< "$packages_list"
# Output as compact JSON (ensure no newlines)
echo "packages=$(echo "$packages_json" | jq -c .)" >> $GITHUB_OUTPUT
echo "Changed packages with baselines: $packages_json"
fi
semver:
needs: detect-changes
if: needs.detect-changes.outputs.has-changes == 'true'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.detect-changes.outputs.changed-packages) }}
env:
# Unset the global `RUSTFLAGS` env to allow warnings.
# cargo-semver-checks intentionally re-locks dependency versions
# before checking, and we shouldn't fail here if a dep has a warning.
#
# More context:
# https://github.com/libp2p/rust-libp2p/pull/4932#issuecomment-1829014527
# https://github.com/obi1kenobi/cargo-semver-checks/issues/589
RUSTFLAGS: ''
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # 4.2.2
with:
fetch-depth: 0
fetch-tags: true
- uses: obi1kenobi/cargo-semver-checks-action@5b298c9520f7096a4683c0bd981a7ac5a7e249ae # v2.8
with:
package: ${{ matrix.package }}
baseline-rev: ${{ matrix.baseline-rev }}