Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tobiasbp/ddd96878d264841133edfc974e7986cc to your computer and use it in GitHub Desktop.

Select an option

Save tobiasbp/ddd96878d264841133edfc974e7986cc to your computer and use it in GitHub Desktop.

Revisions

  1. @carceneaux carceneaux revised this gist Oct 21, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions remove_gitlab_artifacts.sh
    Original file line number Diff line number Diff line change
    @@ -54,8 +54,8 @@ echo ""
    echo "${#job_ids[@]} Jobs found. Commencing removal of Artifacts..."
    for job_id in ${job_ids[@]};
    do
    response=$(curl -s -X POST \
    "https://$server/api/v4/projects/$project_id/jobs/$job_id/erase" \
    -H "PRIVATE-TOKEN:${token}")
    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
  2. @carceneaux carceneaux revised this gist Sep 13, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion remove_gitlab_artifacts.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    #
    # Written by Chris Arceneaux
    # GitHub: https://github.com/carceneaux
    # Email: carceneaux@thinksis.com
    # 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
  3. Chris Arceneaux created this gist May 9, 2019.
    61 changes: 61 additions & 0 deletions remove_gitlab_artifacts.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #!/bin/bash
    #
    # Written by Chris Arceneaux
    # GitHub: https://github.com/carceneaux
    # Email: [email protected]
    # 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="9hjGYpwmsMfBxT-Ghuu7"
    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 POST \
    "https://$server/api/v4/projects/$project_id/jobs/$job_id/erase" \
    -H "PRIVATE-TOKEN:${token}")
    echo "Processing Job ID: ${job_id} - Status: $(echo $response | jq '.status')"
    done