0

I have an issue were I am the owner of a project on a gitlab server, and I can successfully delete all artifacts for a job as listed in the docs..

curl --request DELETE --header "PRIVATE-TOKEN: <token>" "https://gitlab.corp.com/api/v4/projects/1/jobs/1/artifacts"

and I get a 204, meaning it has successfully deleted them.

But , when I try to do the similar command to delete the job. I get a 403 forbidden error...

curl --request POST --header "PRIVATE-TOKEN: <token>" "https://gitlab.corp.com/api/v4/projects/1/jobs/1/erase"

This is the offical documentation, I am using. I am a gitlab admin and project owner. So, it can't be a permission thing, so it is something else...

Can someone tell me what I am missing? I am the owner, why can't I delete the bloody thing?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mr. E
  • 457
  • 1
  • 5
  • 20
  • 1
    Q: "Can someone tell me what I am missing" A: some example of what you did/ code/ anything for us to go on – SuperDJ May 12 '20 at 16:48
  • What other setting do you need? I am not sure what to provide? – Mr. E May 12 '20 at 17:19
  • 1
    After a bit of research, you can't delete an archived job. So, change the arcive settings to a high value and you can clean the jobs.... – Mr. E May 19 '20 at 11:54
  • If that answers your question you should put is as an answer. Then wait a few days and mark it as such. This will help other users as well. – SuperDJ May 19 '20 at 12:23

3 Answers3

0

Find out that pipeline with all jobs can be deleted. Maybe because there is several jobs inside.

0

I've found one solution here.

Full code next:

#!/bin/bash
#
# Written by Chris Arceneaux
# GitHub: https://github.com/carceneaux
# Email: carcenea@gmail.com
# Website: http://arsano.ninja
#
# Note: This code is a stop-gap to erase Job Artifacts for a project. I HIGHLY recommend you leverage
#       "artifacts:expire_in" in your .gitlab-ci.yml
#
# https://docs.gitlab.com/ee/ci/yaml/#artifactsexpire_in
#
# Software Requirements: curl, jq
#
# This code has been released under the terms of the Apache-2.0 license
# http://opensource.org/licenses/Apache-2.0


# project_id, find it here: https://gitlab.com/[organization name]/[repository name] at the top underneath repository name
project_id="207"

# token, find it here: https://gitlab.com/profile/personal_access_tokens
token="TODO_ADD_YOUR_TOKEN"
server="gitlab.com"

# Retrieving Jobs list page count
total_pages=$(curl -sD - -o /dev/null -X GET \
  "https://$server/api/v4/projects/$project_id/jobs?per_page=100" \
  -H "PRIVATE-TOKEN: ${token}" | grep -Fi X-Total-Pages | sed 's/[^0-9]*//g')

# Creating list of Job IDs for the Project specified with Artifacts
job_ids=()
echo ""
echo "Creating list of all Jobs that currently have Artifacts..."
echo "Total Pages: ${total_pages}"
for ((i=2;i<=${total_pages};i++)) #starting with page 2 skipping most recent 100 Jobs
do
  echo "Processing Page: ${i}/${total_pages}"
  response=$(curl -s -X GET \
    "https://$server/api/v4/projects/$project_id/jobs?per_page=100&page=${i}" \
    -H "PRIVATE-TOKEN: ${token}")
  length=$(echo $response | jq '. | length')
  for ((j=0;j<${length};j++))
  do
    if [[ $(echo $response | jq ".[${j}].artifacts_file | length") > 0 ]]; then
        echo "Job found: $(echo $response | jq ".[${j}].id")"
        job_ids+=($(echo $response | jq ".[${j}].id"))
    fi
  done
done

# Loop through each Job erasing the Artifact(s)
echo ""
echo "${#job_ids[@]} Jobs found. Commencing removal of Artifacts..."
for job_id in ${job_ids[@]};
do
  response=$(curl -s -X DELETE \
    -H "PRIVATE-TOKEN:${token}" \
    "https://$server/api/v4/projects/$project_id/jobs/$job_id/artifacts")
  echo "Processing Job ID: ${job_id} - Status: $(echo $response | jq '.status')"
done
GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
pringi
  • 3,987
  • 5
  • 35
  • 45
0

Unfortunately the X-Total-Pages header is now missing in big projects (for performance reasons). Fortunately when a page number is too high, an empty json list ([]) is returned so it is quite easy to use a loop such as (here in bash):

PER_PAGE=100
PAGE=1
while JOBS=$(curl -s --header "PRIVATE-TOKEN: $TOKEN" "$GITLAB_INSTANCE/$PROJECT_ID/jobs?per_page=$PER_PAGE&page=$PAGE&sort=asc") && [ "$JOBS" != "[]" ]
do
   for JOB in $(echo $JOBS | jq .[].id)
   do
      [...]
   done
   PAGE=$((PAGE+1))
done
Christophe Muller
  • 4,850
  • 1
  • 23
  • 31