Skip to content

Commit fb22931

Browse files
committed
add indirect build tracing content and example
1 parent 8f73c69 commit fb22931

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

docs/codeql/codeql-cli/creating-codeql-databases.rst

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,113 @@ commands that you can specify for compiled languages.
228228
This command runs a custom script that contains all of the commands required
229229
to build the project.
230230

231+
Using indirect build tracing
232+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233+
234+
If the CodeQL CLI autobuilders for compiled languages do not work with your CI workflow and you cannot specify
235+
build commands, you can use indirect build tracing to create a CodeQL database. To use indirect build tracing, your CI system must be able to set custom environment variables for each build action.
236+
237+
CodeQL databases are created with indirect build tracing when you run the following command from the checkout root of your project:
238+
239+
::
240+
241+
codeql database init ... --begin-tracing <database>
242+
243+
You must specify:
244+
245+
- ``<database>``: a path to the new database to be created. This directory will
246+
be created when you execute the command---you cannot specify an existing
247+
directory.
248+
- ``--begin-tracing``: creates scripts that can be used to set up an environment in which build commands will be traced.
249+
250+
You may specify other options for the ``codeql database init`` command as normal.
251+
252+
.. pull-quote:: Note
253+
254+
If you are on Windows, set either ``--trace-process-level <number>`` or ``--trace-process-name <parent process name>`` so that the option points to the parent CI process.
255+
256+
257+
The ``codeql database init`` command will output a message:
258+
```
259+
Created skeleton <database>. This in-progress database is ready to be populated by an extractor.
260+
In order to initialise tracing, some environment variables need to be set in the shell your build will run in.
261+
A number of scripts to do this have been created in <database>/temp/tracingEnvironment.
262+
Please run one of these scripts before invoking your build command.
263+
264+
Based on your operating system, we recommend you run: ...
265+
```
266+
267+
The ``codeql database init`` command will produce files in ``<database>/temp/tracingEnvironment`` containing environment variables and their values for CodeQL to trace subsequent build steps. These files are named ``start-tracing.{json,sh,bat,ps1}``. Use one of these files with your CI system's mechanism for setting environment variables for future steps. You can:
268+
269+
* Read the JSON file, process it, and print out environment variables in the format expected by your CI system. For example, Azure DevOps expects ``echo "##vso[task.setvariable variable=NAME]VALUE"``.
270+
* Or source the ``sh/bat/ps1`` script so that its variables go into your shell environment.
271+
272+
Build your code and then run the command ``codeql database finalize <database>``.
273+
274+
You can optionally clean up the environment variables by following the same process as with the ``--begin-tracing`` scripts, except now with ``--end-tracing`` scripts in the same directory.
275+
276+
Once you have created a CodeQL database using indirect build tracing, you can work with it like any other CodeQL database. For example, analyze the database, and upload the results if using Code Scanning.
277+
278+
Example of creating a CodeQL database using indirect build tracing
279+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
280+
281+
The following example shows how you could use indirect build tracing in an Azure DevOps pipeline to create a CodeQL database::
282+
283+
steps:
284+
# Download the CodeQL CLI and query packs...
285+
# Check out the repository ...
286+
287+
# Tasks prior to executing the build, e.g. restore NuGet dependencies...
288+
289+
# Initialize the CodeQL database.
290+
# In this example, the CodeQL CLI has been downloaded and placed on the PATH.
291+
# If no language is specified, a GitHub Apps or personal access token must be passed through stdin
292+
# to autodetect the language.
293+
- task: CmdLine@1
294+
displayName: Initialize CodeQL database
295+
inputs:
296+
# Assumes the source code is checked out to the current working directory.
297+
# Creates a database at `<current working directory>/db`
298+
script: "codeql database init --language csharp --trace-process-level 3 --source-root --begin-tracing db"
299+
300+
# Read the generated environment variables and values,
301+
# and set them so they are available for subsequent commands
302+
# in the build pipeline. This is done in PowerShell in this example.
303+
- task: PowerShell@1
304+
displayName: Set CodeQL environment variables
305+
inputs:
306+
targetType: inline
307+
script: >
308+
$json = Get-Content $(System.DefaultWorkingDirectory)/db/temp/tracingEnvironment/start-tracing.json | ConvertFrom-Json
309+
$json.PSObject.Properties | ForEach-Object {
310+
$template = "##vso[task.setvariable variable="
311+
$template += $_.Name
312+
$template += "]"
313+
$template += $_.Value
314+
echo "$template"
315+
}
316+
317+
# Execute the pre-defined build step. Note the `msbuildArgs` variable.
318+
- task: VSBuild@1
319+
inputs:
320+
solution: '**/*.sln'
321+
# Disable MSBuild shared compilation for C# builds.
322+
msbuildArgs: /p:OutDir=$(Build.ArtifactStagingDirectory) /p:UseSharedCompilation=false
323+
platform: Any CPU
324+
configuration: Release
325+
# Execute a clean build, in order to remove any existing build artifacts prior to the build.
326+
clean: True
327+
displayName: Visual Studio Build
328+
329+
- task: CmdLine@2
330+
displayName: Finalize CodeQL database
331+
inputs:
332+
script: 'codeql database finalize db'
333+
334+
# Other tasks go here,
335+
# e.g. `codeql database analyze`
336+
# and `codeql github upload-results` ...
337+
231338
Obtaining databases from LGTM.com
232339
---------------------------------
233340

0 commit comments

Comments
 (0)