1+ name : Reusable Rust Testing Workflow
2+
3+ on :
4+ workflow_call :
5+ inputs :
6+ target-branch :
7+ description : ' Branch to checkout and test (defaults to the calling branch)'
8+ required : false
9+ type : string
10+ default : ' '
11+ rust-versions :
12+ description : ' JSON array of Rust versions to test against'
13+ required : false
14+ type : string
15+ default : ' ["stable"]'
16+ platforms :
17+ description : ' JSON array of platforms to run tests on'
18+ required : false
19+ type : string
20+ default : ' ["ubuntu-latest"]'
21+ test-script :
22+ description : ' Test script to execute'
23+ required : false
24+ type : string
25+ default : ' ./run-tests.sh'
26+ examples-command :
27+ description : ' Examples command to execute'
28+ required : false
29+ type : string
30+ default : ' cargo check --examples'
31+ enable-status-reporting :
32+ description : ' Whether to post status checks to datadog-api-spec repo'
33+ required : false
34+ type : boolean
35+ default : false
36+ status-context :
37+ description : ' Context for status checks'
38+ required : false
39+ type : string
40+ default : ' master/unit'
41+ secrets :
42+ PIPELINE_GITHUB_APP_ID :
43+ required : false
44+ PIPELINE_GITHUB_APP_PRIVATE_KEY :
45+ required : false
46+
47+ jobs :
48+ pre-commit :
49+ runs-on : ubuntu-latest
50+ if : >
51+ (github.event.pull_request.draft == false &&
52+ !contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
53+ !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
54+ github.event_name == 'schedule'
55+ steps :
56+ - name : Get GitHub App token
57+ if : github.event_name == 'pull_request'
58+ id : get_token
59+ uses : actions/create-github-app-token@v1
60+ with :
61+ app-id : ${{ secrets.PIPELINE_GITHUB_APP_ID }}
62+ private-key : ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
63+ repositories : datadog-api-spec
64+ - uses : actions/checkout@v3
65+ with :
66+ ref : ${{ inputs.target-branch || github.ref }}
67+ with :
68+ fetch-depth : 0
69+ ref : ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
70+ token : ${{ steps.get_token.outputs.token }}
71+ - name : Install pre-commit
72+ run : python -m pip install pre-commit
73+ - name : set PY
74+ run : echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
75+ - uses : actions/cache@v3
76+ with :
77+ path : ~/.cache/pre-commit
78+ key : pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
79+ - id : pre_commit
80+ name : Run pre-commit
81+ if : github.event.action != 'closed' && github.event.pull_request.merged != true
82+ run : |
83+ pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
84+ env :
85+ FROM_REF : ${{ github.event.pull_request.base.sha }}
86+ TO_REF : ${{ github.event.pull_request.head.sha }}
87+ - name : Commit changes
88+ if : ${{ failure() }}
89+ run : |-
90+ git add -A
91+ git config user.name "${GIT_AUTHOR_NAME}"
92+ git config user.email "${GIT_AUTHOR_EMAIL}"
93+ git commit -m "pre-commit fixes"
94+ git push origin "HEAD:${HEAD_REF}"
95+ exit 1
96+ env :
97+ HEAD_REF : ${{ github.event.pull_request.head.ref }}
98+ GIT_AUTHOR_EMAIL :
" [email protected] " 99+ GIT_AUTHOR_NAME : " ci.datadog-api-spec"
100+ - id : pre_commit_schedule
101+ name : Run pre-commit in schedule
102+ if : github.event_name == 'schedule'
103+ run : |
104+ pre-commit run --all-files --show-diff-on-failure --color=always
105+
106+ test :
107+ strategy :
108+ matrix :
109+ rust-version : ${{ fromJSON(inputs.rust-versions) }}
110+ platform : ${{ fromJSON(inputs.platforms) }}
111+ runs-on : ${{ matrix.platform }}
112+ 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'
113+ steps :
114+ - name : Checkout code
115+ uses : actions/checkout@v3
116+ with :
117+ ref : ${{ inputs.target-branch || github.ref }}
118+ - name : Install Rust
119+ uses : dtolnay/rust-toolchain@v1
120+ with :
121+ toolchain : ${{ matrix.rust-version }}
122+ - uses : Swatinem/rust-cache@v2
123+ - name : Test
124+ run : ${{ inputs.test-script }}
125+
126+ examples :
127+ runs-on : ubuntu-latest
128+ 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'
129+ steps :
130+ - uses : actions/checkout@v3
131+ with :
132+ ref : ${{ inputs.target-branch || github.ref }}
133+ - name : Install Rust
134+ uses : dtolnay/rust-toolchain@master
135+ with :
136+ toolchain : stable
137+ - name : Check examples
138+ run : ${{ inputs.examples-command }}
139+ shell : bash
140+
141+ report :
142+ runs-on : ubuntu-latest
143+ if : always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && inputs.enable-status-reporting
144+ needs :
145+ - test
146+ - examples
147+ steps :
148+ - name : Get GitHub App token
149+ if : github.event_name == 'pull_request'
150+ id : get_token
151+ uses : actions/create-github-app-token@v1
152+ with :
153+ app-id : ${{ secrets.PIPELINE_GITHUB_APP_ID }}
154+ private-key : ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
155+ repositories : datadog-api-spec
156+ - name : Post status check
157+ uses : DataDog/github-actions/post-status-check@v2
158+ with :
159+ github-token : ${{ steps.get_token.outputs.token }}
160+ repo : datadog-api-spec
161+ status : ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }}
162+ context : ${{ inputs.status-context }}
0 commit comments