Skip to content

Commit 6d9f12c

Browse files
committed
fix(ci): sanitize matrix.service to remove newlines
- Remove CR/LF from services string before matrix generation - Use jq gsub to trim whitespace from each service name - Add defensive sanitization in build and scan steps - Use tr -d to strip all whitespace from matrix.service - Fix root cause: echo adds newline that jq -s preserves - Prevents 'NX Cannot find project checkout\n' errors
1 parent 11409dc commit 6d9f12c

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

.github/workflows/build-push-images.yml

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,19 @@ jobs:
102102
- name: Set matrix
103103
id: set-matrix
104104
run: |
105-
SERVICES="${{ steps.filter.outputs.services }}"
106-
if [ "$SERVICES" == "none" ]; then
107-
echo "matrix={\"include\":[]}" >> $GITHUB_OUTPUT
105+
# Sanitize services string - remove CR/LF characters
106+
SERVICES="$(printf '%s' "${{ steps.filter.outputs.services }}" | tr -d '\r\n')"
107+
108+
if [ "$SERVICES" = "none" ] || [ -z "$SERVICES" ]; then
109+
echo 'matrix={"include":[]}' >> "$GITHUB_OUTPUT"
108110
else
109-
# Convert comma-separated list to JSON array
110-
MATRIX_JSON=$(echo "$SERVICES" | jq -R -s -c 'split(",") | map(select(length > 0)) | map({service: .})')
111-
echo "matrix={\"include\":$MATRIX_JSON}" >> $GITHUB_OUTPUT
111+
# Convert comma-separated list to JSON array with whitespace trimming
112+
MATRIX_JSON="$(printf '%s' "$SERVICES" \
113+
| jq -Rsc 'split(",")
114+
| map(gsub("^\\s+|\\s+$";""))
115+
| map(select(length > 0))
116+
| map({service: .})')"
117+
echo "matrix={\"include\":$MATRIX_JSON}" >> "$GITHUB_OUTPUT"
112118
fi
113119
114120
build-and-push:
@@ -149,44 +155,66 @@ jobs:
149155
- name: Build and push image
150156
env:
151157
COMMIT_SHA: ${{ github.sha }}
158+
SERVICE_RAW: ${{ matrix.service }}
152159
run: |
153160
set -euo pipefail
154161
162+
# Sanitize service name - remove any whitespace/newlines
163+
SERVICE="$(printf '%s' "${SERVICE_RAW}" | tr -d '[:space:]')"
164+
155165
SHORT_SHA="$(echo "${COMMIT_SHA}" | cut -c1-7)"
156166
TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
157167
REGISTRY="${{ steps.login-ecr.outputs.registry }}"
158-
PREFIX="${{ env.ECR_REGISTRY_PREFIX }}-${{ matrix.service }}"
168+
PREFIX="${{ env.ECR_REGISTRY_PREFIX }}-${SERVICE}"
159169
160170
TAGS="${REGISTRY}/${PREFIX}:${SHORT_SHA},${REGISTRY}/${PREFIX}:latest,${REGISTRY}/${PREFIX}:${TIMESTAMP}"
161171
172+
echo "SERVICE=${SERVICE}"
162173
echo "REGISTRY=${REGISTRY}"
163174
echo "PREFIX=${PREFIX}"
164175
echo "TAGS=${TAGS}"
165176
166-
yarn nx container "${{ matrix.service }}" \
177+
yarn nx container "${SERVICE}" \
167178
--tags="${TAGS}" \
168179
--push="${{ github.event_name != 'pull_request' }}"
169180
170181
- name: Scan image for vulnerabilities
171182
if: github.event_name != 'pull_request'
172183
env:
173184
COMMIT_SHA: ${{ github.sha }}
185+
SERVICE_RAW: ${{ matrix.service }}
174186
run: |
175-
SHORT_SHA=$(echo $COMMIT_SHA | cut -c1-7)
187+
# Sanitize service name
188+
SERVICE="$(printf '%s' "${SERVICE_RAW}" | tr -d '[:space:]')"
189+
SHORT_SHA="$(echo "${COMMIT_SHA}" | cut -c1-7)"
176190
177191
# Wait for scan to complete
178192
aws ecr wait image-scan-complete \
179-
--repository-name ${{ env.ECR_REGISTRY_PREFIX }}-${{ matrix.service }} \
193+
--repository-name ${{ env.ECR_REGISTRY_PREFIX }}-${SERVICE} \
180194
--image-id imageTag=${SHORT_SHA} \
181195
--region ${{ env.AWS_REGION }} || true
182-
196+
183197
# Get scan findings
184198
SCAN_FINDINGS=$(aws ecr describe-image-scan-findings \
185-
--repository-name ${{ env.ECR_REGISTRY_PREFIX }}-${{ matrix.service }} \
199+
--repository-name ${{ env.ECR_REGISTRY_PREFIX }}-${SERVICE} \
186200
--image-id imageTag=${SHORT_SHA} \
187201
--region ${{ env.AWS_REGION }} \
188202
--query 'imageScanFindings.findingSeverityCounts' \
189203
--output json || echo '{}')
204+
205+
echo "Scan findings for ${SERVICE}:"
206+
echo "$SCAN_FINDINGS" | jq .
207+
208+
# Check for critical vulnerabilities
209+
CRITICAL=$(echo "$SCAN_FINDINGS" | jq -r '.CRITICAL // 0')
210+
HIGH=$(echo "$SCAN_FINDINGS" | jq -r '.HIGH // 0')
211+
212+
if [ "$CRITICAL" -gt 0 ]; then
213+
echo "::warning::Found $CRITICAL CRITICAL vulnerabilities in ${SERVICE}"
214+
fi
215+
if [ "$HIGH" -gt 0 ]; then
216+
echo "::warning::Found $HIGH HIGH vulnerabilities in ${SERVICE}"
217+
fi
190218
191219
echo "Scan findings for ${{ matrix.service }}:"
192220
echo "$SCAN_FINDINGS" | jq .

0 commit comments

Comments
 (0)