Skip to content

Commit 5128115

Browse files
authored
feat: add install_semgrep command (#50)
The command to install Semgrep static analysis tool. By default, the latest version is installed. Useful for cases when using Docker daemon and machine executor.
1 parent 1cb250b commit 5128115

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

.circleci/test-deploy.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ jobs:
123123
echo "Failed to install chosen grype version"
124124
exit 1
125125
fi
126+
install_semgrep:
127+
machine:
128+
image: ubuntu-2404:current
129+
steps:
130+
- security/install_semgrep:
131+
version: v1.121.0
132+
- run:
133+
name: Validate installation
134+
command: |
135+
if ! semgrep --version | grep -q "1.121.0"; then
136+
echo "Failed to install chosen semgrep version"
137+
exit 1
138+
fi
126139
127140
workflows:
128141
test-deploy:
@@ -171,6 +184,8 @@ workflows:
171184
filters: *filters
172185
- install_grype:
173186
filters: *filters
187+
- install_semgrep:
188+
filters: *filters
174189
- orb-tools/pack:
175190
filters: *release-filters
176191
- orb-tools/publish:
@@ -191,5 +206,6 @@ workflows:
191206
- install_trivy
192207
- install_syft
193208
- install_grype
209+
- install_semgrep
194210
context: orb-publishing
195211
filters: *release-filters

src/commands/install_semgrep.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
description: >
2+
Install Semgrep (https://github.com/semgrep/semgrep) a fast open-source static
3+
analysis tool.
4+
Requires the runtime environment with Python 3 and Pip. Installs Semgrep in
5+
the user-specific location, not system-wide.
6+
7+
parameters:
8+
version:
9+
type: string
10+
default: ""
11+
description: >
12+
Choose the specific version of Semgrep from https://github.com/semgrep/semgrep/releases.
13+
By default, the latest version is picked.
14+
15+
steps:
16+
- run:
17+
name: Install Semgrep
18+
environment:
19+
PARAM_STR_VERSION: <<parameters.version>>
20+
command: <<include(scripts/install-semgrep.sh)>>

src/scripts/install-semgrep.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
function install_semgrep() {
4+
local semgrep_arg
5+
local install_path
6+
7+
[[ -n "${PARAM_STR_VERSION}" ]] && semgrep_arg="semgrep==${PARAM_STR_VERSION#v}" || semgrep_arg="semgrep"
8+
9+
set -x
10+
# Installing without the '--user' flag results in the command not found error
11+
# due to issue how pip installed packages are added to the PATH in CI environments.
12+
# Adding the '--user' flag, (alongside the '--no-warn-script-location' to suppress
13+
# the location warnings) installs the package in a user specific directory which
14+
# is afterwards added to the PATH.
15+
python3 -m pip install --no-warn-script-location --user "${semgrep_arg}"
16+
set +x
17+
18+
install_path="$(python3 -m site --user-base)/bin"
19+
20+
echo "Adding Semgrep installation path (${install_path}) to the PATH"
21+
echo "export PATH=${install_path}:${PATH}" >>"${BASH_ENV}"
22+
}
23+
24+
if ! command -v python3 >/dev/null 2>&1 || ! command -v pip3 >/dev/null 2>&1; then
25+
echo "Python 3 and Pip are required"
26+
exit 1
27+
fi
28+
29+
if ! command -v semgrep >/dev/null 2>&1; then
30+
echo "Failed to detect Semgrep, installing..."
31+
32+
install_semgrep
33+
fi

0 commit comments

Comments
 (0)