Build and run static analysis #258
This file contains 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
# This workflow was created to access the repository secrets in a safe way when | |
# a Pull Request is opened. If this workflow was triggered by "PR handler" | |
# completion, it will download the artifacts generated by the completed | |
# workflow (that contains the changes and the resources to compile all the | |
# examples). On the other hand, if this workflow was triggered by a push event, | |
# it will execute a checkout to get all the changes. | |
name: Build and run static analysis | |
on: | |
push: | |
branches: | |
- master | |
workflow_run: | |
workflows: ["PR handler"] | |
types: | |
- completed | |
jobs: | |
build: | |
strategy: | |
matrix: | |
architecture: [{name: x64Linux4gcc7.3.0, os: ubuntu-20.04}] | |
runs-on: ${{ matrix.architecture.os }} | |
name: Build examples | |
# This job will be executed if the "PR handler" finalized successfully or | |
# if this workflow was triggered by a push event. | |
if: > | |
${{ (github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success') || | |
github.event_name == 'push' }} | |
outputs: | |
pr_number: ${{ steps.pr_number.outputs.PR_NUMBER }} | |
steps: | |
# Download the artifacts generated by "PR Handler" if this workflow was | |
# triggered by a workflow_run event. | |
- name: Download artifact | |
if: ${{ github.event_name == 'workflow_run' }} | |
uses: actions/github-script@v3.1.0 | |
with: | |
script: | | |
var fs = require('fs'); | |
var artifacts = await github.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{ github.event.workflow_run.id }}, | |
}); | |
var matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "targetbranch" | |
})[0]; | |
var download = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
fs.writeFileSync('${{github.workspace}}/targetbranch.zip', Buffer.from(download.data)); | |
# Unzip the artifacts. | |
- name: Unzip artifact | |
if: ${{ github.event_name == 'workflow_run' }} | |
run: unzip targetbranch.zip | |
# Save the PR number to send messages to the user that opened it. | |
- name: Get target branch | |
if: ${{ github.event_name == 'workflow_run' }} | |
id: target_branch | |
run: echo "TARGET_BRANCH=$(cat targetbranch)" >> $GITHUB_OUTPUT | |
# Sparse-checkout: we clone just the scripts from the target branch for | |
# sefaty reasons. | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ steps.target_branch.outputs.TARGET_BRANCH }} | |
sparse-checkout: resources/ci_cd/ | |
sparse-checkout-cone-mode: false | |
if: ${{ github.event_name == 'workflow_run' }} | |
# Download the artifacts generated by "PR Handler" if this workflow was | |
# triggered by a workflow_run event. | |
- name: Download artifact | |
if: ${{ github.event_name == 'workflow_run' }} | |
uses: actions/github-script@v3.1.0 | |
with: | |
script: | | |
var fs = require('fs'); | |
var artifacts = await github.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{ github.event.workflow_run.id }}, | |
}); | |
var matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "pr" | |
})[0]; | |
var download = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); | |
# Unzip the artifacts. | |
- name: Unzip artifact | |
if: ${{ github.event_name == 'workflow_run' }} | |
run: unzip -n pr.zip | |
# Save the PR number to send messages to the user that opened it. | |
- name: Get PR number | |
if: ${{ github.event_name == 'workflow_run' }} | |
id: pr_number | |
run: echo "PR_NUMBER=$(cat NR)" >> $GITHUB_OUTPUT | |
# Send an information message to the user that opened the pull request. | |
- name: Send information message | |
if: ${{ github.event_name == 'workflow_run' }} | |
uses: actions/github-script@v3.1.0 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
var issue_number = ${{ steps.pr_number.outputs.PR_NUMBER }}; | |
await github.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue_number, | |
body: `The compilation is starting. Take a look [here](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}).` | |
}); | |
# If this workflow was triggered by a push event, it will download the | |
# repository with the changes. | |
- uses: actions/checkout@v3 | |
with: | |
submodules: true | |
if: ${{ github.event_name == 'push' }} | |
# Setup Python. If you need another version, just change it below. | |
- name: Setup Python 3.11 | |
uses: actions/setup-python@v2 | |
with: | |
python-version: 3.11 | |
- name: Set up Clang | |
uses: egor-tensin/setup-clang@v1 | |
with: | |
version: 13 | |
platform: x64 | |
# Install the dependencies to run the Python scripts. | |
- name: Install dependencies | |
run: | | |
pip install -r resources/ci_cd/requirements.txt | |
# This script downloads the mandatory libraries of RTI Connext DDS for | |
# compiling the examples | |
- name: Install RTI Connext DDS | |
run: python resources/ci_cd/linux_install.py -a ${{ matrix.architecture.name }} | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
RTI_AWS_BUCKET: ${{ secrets.RTI_AWS_BUCKET }} | |
RTI_AWS_PATH: ${{ secrets.RTI_AWS_PATH }} | |
# This script will compile the examples and execute the static analysis. | |
- name: Build all the examples | |
run: python resources/ci_cd/linux_build.py | |
- name: Peform the static analysis | |
run: python resources/ci_cd/linux_static_analysis.py | |
commentpr: | |
runs-on: ubuntu-latest | |
needs: build | |
name: Comment PR | |
# This job will be executed if this workflow was triggered by a | |
# workflow_run event even if the build job failed or succeeded. | |
if: ${{ always() && github.event_name == 'workflow_run' }} | |
steps: | |
# This job will create a comment on the Pull Request reporting if the | |
# build finished successfully or failed. | |
- name: Comment on PR | |
if: ${{ github.event_name == 'workflow_run' }} | |
uses: actions/github-script@v3 | |
env: | |
ISSUE_NUMBER: ${{ needs.build.outputs.pr_number }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
var message; | |
if ('${{ needs.build.result }}' == 'success') | |
message = 'Everything is OK. Thank you for the PR!'; | |
else | |
message = 'Oops, something went wrong!'; | |
var issue_number = ${{ needs.build.outputs.pr_number }}; | |
await github.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue_number, | |
body: message, | |
}); |