Skip to content

Commit be748ce

Browse files
committed
fix image delete logic
1 parent 02fa0b6 commit be748ce

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

.github/workflows/fatimage.yml

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,13 @@ jobs:
100100
echo "image-name=${IMAGE_NAME}" >> "$GITHUB_OUTPUT"
101101
echo "image-id=$IMAGE_ID" >> "$GITHUB_OUTPUT"
102102
103-
- name: Delete old latest image
104-
run: |
105-
. venv/bin/activate
106-
IMAGE_COUNT=$(openstack image list --name ${{ steps.manifest.outputs.image-name }} -f value -c ID | wc -l)
107-
if [ "$IMAGE_COUNT" -gt 1 ]; then
108-
OLD_IMAGE_ID=$(openstack image list --sort created_at:asc --name "${{ steps.manifest.outputs.image-name }}" -f value -c ID | head -n 1)
109-
openstack image delete "$OLD_IMAGE_ID"
110-
else
111-
echo "Only one image exists, skipping deletion."
112-
fi
113-
114103
- name: Download image
115104
run: |
116105
. venv/bin/activate
117106
sudo mkdir /mnt/images
118107
sudo chmod 777 /mnt/images
119-
openstack image unset --property signature_verified "${{ steps.manifest.outputs.image-name }}"
120-
openstack image save --file /mnt/images/${{ steps.manifest.outputs.image-name }}.qcow2 ${{ steps.manifest.outputs.image-name }}
108+
openstack image unset --property signature_verified "${{ steps.manifest.outputs.image-id }}"
109+
openstack image save --file /mnt/images/${{ steps.manifest.outputs.image-name }}.qcow2 ${{ steps.manifest.outputs.image-id }}
121110
122111
- name: Set up QEMU
123112
uses: docker/setup-qemu-action@v3
@@ -134,6 +123,7 @@ jobs:
134123
run: sudo guestmount -a /mnt/images/${{ steps.manifest.outputs.image-name }}.qcow2 -i --ro -o allow_other './${{ steps.manifest.outputs.image-name }}'
135124

136125
- name: Run Trivy vulnerability scanner
126+
id: trivy_vuln_check
137127
uses: aquasecurity/[email protected]
138128
with:
139129
scan-type: fs
@@ -152,6 +142,7 @@ jobs:
152142
category: "${{ matrix.os_version }}-${{ matrix.build }}"
153143

154144
- name: Fail if scan has CRITICAL vulnerabilities
145+
id: trivy_crit_check
155146
uses: aquasecurity/[email protected]
156147
with:
157148
scan-type: fs
@@ -161,6 +152,26 @@ jobs:
161152
exit-code: '1'
162153
severity: 'CRITICAL'
163154
ignore-unfixed: true
155+
156+
- name: Delete new image if Trivy scan fails
157+
if: steps.trivy_vuln_check.outcome == 'failure' || steps.trivy_crit_check.outcome == 'failure' # Runs if the Trivy scan found crit vulnerabilities or failed
158+
run: |
159+
. venv/bin/activate
160+
echo "Deleting new image due to critical vulnerabilities..."
161+
openstack image delete "${{ steps.manifest.outputs.image-id }}"
162+
163+
- name: Delete old latest image
164+
if: steps.trivy_critical_check.outcome == 'success' # Runs only if Trivy scan passed
165+
run: |
166+
. venv/bin/activate
167+
IMAGE_COUNT=$(openstack image list --name ${{ steps.manifest.outputs.image-name }} -f value -c ID | wc -l)
168+
if [ "$IMAGE_COUNT" -gt 1 ]; then
169+
OLD_IMAGE_ID=$(openstack image list --sort created_at:asc --name "${{ steps.manifest.outputs.image-name }}" -f value -c ID | head -n 1)
170+
echo "Deleting old image ID: $OLD_IMAGE_ID"
171+
openstack image delete "$OLD_IMAGE_ID"
172+
else
173+
echo "Only one image exists, skipping deletion."
174+
fi
164175
165176
upload:
166177
name: upload-nightly-targets

0 commit comments

Comments
 (0)