首页 > 解决方案 > 如何通过 REST API 删除本地 Artifactory 存储库的所有内容?

问题描述

我正在使用本地存储库作为临时存储库,并希望能够通过 REST 清除整个临时存储库。如何在不删除 repo 本身的情况下删除 repo 的内容?

标签: artifactory

解决方案


由于我在我的一个环境中有类似的要求,我喜欢提供一种可能的解决方案。

假设 JFrog Artifactory 实例有一个名为的本地存储库JFROG-ARTIFACTORY,其中包含最新的 JFrog Artifactory Pro 安装 RPM。对于列出和删除,我创建了以下脚本:

#!/bin/bash

# The logged in user will be also the admin account for Artifactory REST API

A_ACCOUNT=$(who am i | cut -d " " -f 1)

LOCAL_REPO=$1
PASSWORD=$2
STAGE=$3
URL="example.com"

# Check if a stage were provided, if not set it to PROD

if [ -z "$STAGE" ]; then
    STAGE="repository-prod"
fi

# Going to list all files within the local repository
# Doc: https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API#ArtifactoryRESTAPI-FileList

curl --silent \
     -u"${A_ACCOUNT}:${PASSWORD}" \
     -i \
     -X GET "https://${STAGE}.${URL}/artifactory/api/storage/${LOCAL_REPO}/?list&deep=1" \
     -w "\n\n%{http_code}\n"
echo

# Going to delete all files in the local repository
# Doc: https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API#ArtifactoryRESTAPI-DeleteItem

curl --silent \
     -u"${A_ACCOUNT}:${PASSWORD}" \
     -i \
     -X DELETE "https://${STAGE}.${URL}/artifactory/${LOCAL_REPO}/" \
     -w "\n\n%{http_code}\n"
echo

所以打电话后

./Scripts/deleteRepository.sh JFROG-ARTIFACTORY Pa\$\$w0rd! repository-dev

对于开发实例,它列出了本地存储库中名为JFROG-ARTIFACTORYJFrog Artifactory Pro 安装 RPM 的所有文件,删除了它们,但保留了本地存储库本身。

您可以根据需要更改和增强脚本,还可以查看如何从 Artifactory 中完全删除工件?


推荐阅读