Make Rust test workflow reusable #1
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Reusable Rust Testing Workflow | ||
on: | ||
workflow_call: | ||
inputs: | ||
target-branch: | ||
description: 'Branch to checkout and test (defaults to the calling branch)' | ||
required: false | ||
type: string | ||
default: '' | ||
rust-versions: | ||
description: 'JSON array of Rust versions to test against' | ||
required: false | ||
type: string | ||
default: '["stable"]' | ||
platforms: | ||
description: 'JSON array of platforms to run tests on' | ||
required: false | ||
type: string | ||
default: '["ubuntu-latest"]' | ||
test-script: | ||
description: 'Test script to execute' | ||
required: false | ||
type: string | ||
default: './run-tests.sh' | ||
examples-command: | ||
description: 'Examples command to execute' | ||
required: false | ||
type: string | ||
default: 'cargo check --examples' | ||
enable-status-reporting: | ||
description: 'Whether to post status checks to datadog-api-spec repo' | ||
required: false | ||
type: boolean | ||
default: false | ||
status-context: | ||
description: 'Context for status checks' | ||
required: false | ||
type: string | ||
default: 'master/unit' | ||
secrets: | ||
PIPELINE_GITHUB_APP_ID: | ||
required: false | ||
PIPELINE_GITHUB_APP_PRIVATE_KEY: | ||
required: false | ||
jobs: | ||
pre-commit: | ||
runs-on: ubuntu-latest | ||
if: > | ||
(github.event.pull_request.draft == false && | ||
!contains(github.event.pull_request.labels.*.name, 'ci/skip') && | ||
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || | ||
github.event_name == 'schedule' | ||
steps: | ||
- name: Get GitHub App token | ||
if: github.event_name == 'pull_request' | ||
id: get_token | ||
uses: actions/create-github-app-token@v1 | ||
with: | ||
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }} | ||
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} | ||
repositories: datadog-api-spec | ||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ inputs.target-branch || github.ref }} | ||
with: | ||
fetch-depth: 0 | ||
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }} | ||
token: ${{ steps.get_token.outputs.token }} | ||
- name: Install pre-commit | ||
run: python -m pip install pre-commit | ||
- name: set PY | ||
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV | ||
- uses: actions/cache@v3 | ||
with: | ||
path: ~/.cache/pre-commit | ||
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }} | ||
- id: pre_commit | ||
name: Run pre-commit | ||
if: github.event.action != 'closed' && github.event.pull_request.merged != true | ||
run: | | ||
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always | ||
env: | ||
FROM_REF: ${{ github.event.pull_request.base.sha }} | ||
TO_REF: ${{ github.event.pull_request.head.sha }} | ||
- name: Commit changes | ||
if: ${{ failure() }} | ||
run: |- | ||
git add -A | ||
git config user.name "${GIT_AUTHOR_NAME}" | ||
git config user.email "${GIT_AUTHOR_EMAIL}" | ||
git commit -m "pre-commit fixes" | ||
git push origin "HEAD:${HEAD_REF}" | ||
exit 1 | ||
env: | ||
HEAD_REF: ${{ github.event.pull_request.head.ref }} | ||
GIT_AUTHOR_EMAIL: "[email protected]" | ||
GIT_AUTHOR_NAME: "ci.datadog-api-spec" | ||
- id: pre_commit_schedule | ||
name: Run pre-commit in schedule | ||
if: github.event_name == 'schedule' | ||
run: | | ||
pre-commit run --all-files --show-diff-on-failure --color=always | ||
test: | ||
strategy: | ||
matrix: | ||
rust-version: ${{ fromJSON(inputs.rust-versions) }} | ||
platform: ${{ fromJSON(inputs.platforms) }} | ||
runs-on: ${{ matrix.platform }} | ||
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule' | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ inputs.target-branch || github.ref }} | ||
- name: Install Rust | ||
uses: dtolnay/rust-toolchain@v1 | ||
with: | ||
toolchain: ${{ matrix.rust-version }} | ||
- uses: Swatinem/rust-cache@v2 | ||
- name: Test | ||
run: ${{ inputs.test-script }} | ||
examples: | ||
runs-on: ubuntu-latest | ||
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule' | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ inputs.target-branch || github.ref }} | ||
- name: Install Rust | ||
uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: stable | ||
- name: Check examples | ||
run: ${{ inputs.examples-command }} | ||
shell: bash | ||
report: | ||
runs-on: ubuntu-latest | ||
if: always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && inputs.enable-status-reporting | ||
needs: | ||
- test | ||
- examples | ||
steps: | ||
- name: Get GitHub App token | ||
if: github.event_name == 'pull_request' | ||
id: get_token | ||
uses: actions/create-github-app-token@v1 | ||
with: | ||
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }} | ||
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} | ||
repositories: datadog-api-spec | ||
- name: Post status check | ||
uses: DataDog/github-actions/post-status-check@v2 | ||
with: | ||
github-token: ${{ steps.get_token.outputs.token }} | ||
repo: datadog-api-spec | ||
status: ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }} | ||
context: ${{ inputs.status-context }} |