From 5dee78564d146fda34baf03dd5c6ea86fba9d39b Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Wed, 30 Oct 2024 09:48:14 +0000 Subject: [PATCH] Add GitHub workflow to scan project with CodeQL - Set up CodeQL analysis workflow to scan both the Kubebuilder CLI and generated projects in 'testdata'. - Run 'make install' in the root directory to build and install the Kubebuilder CLI as part of the setup. - For sample projects in 'testdata' (e.g., project-v4, project-v4-multigroup, and project-v4-with-plugins), add a step to run 'make all' to ensure all resources are generated and ready for analysis. - Configure the manual build mode for Go projects in 'testdata' to run 'make manifests' and 'make build' if autobuild is insufficient. --- .github/workflows/codeql.yml | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 00000000000..c46a671997d --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,79 @@ +name: "CodeQL Advanced" + +on: + # We are checking master and book-v4 because book-v4 has always the code + # from the latest release + push: + branches: ["master", "book-v4"] + pull_request: + branches: ["master", "book-v4"] + schedule: + - cron: '30 20 * * 1' # Runs a periodic scan every Monday at 8:30 PM + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + runs-on: ubuntu-latest + permissions: + security-events: write + packages: read + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: go + build-mode: autobuild + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Set up Go environment for Kubebuilder projects + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '1.22' + + # Install Kubebuilder tools, specifically Kustomize v5.5.0 and controller-gen + - name: Install Kubebuilder tools + run: | + go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.16.4 + go install sigs.k8s.io/kustomize/kustomize/v5@v5.5.0 + + # Run `make install` to install Kubebuilder CLI + - name: Build and install Kubebuilder CLI + run: make install + + # Build and prepare resources in each sample project under testdata + - name: Build sample projects in testdata + run: | + for dir in testdata/*; do + if [ -d "$dir" ]; then + (cd "$dir" && make all) + fi + done + + # Initialize CodeQL analysis for the specified languages + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + + # Manual build mode for Go in generated projects only, if autobuild is insufficient + - if: matrix.language == 'go' && matrix.build-mode == 'manual' + shell: bash + run: | + for dir in testdata/*; do + if [ -d "$dir" ]; then + (cd "$dir" && echo 'Running manual build commands for Go in sample project' && make manifests && make build) + fi + done + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 + with: + category: "/language:${{matrix.language}}"