Archiviare pacchetti Conda in Artifact Registry

Questa guida rapida mostra come configurare un repository Conda privato di Artifact Registry e poi caricare un pacchetto in questo repository.

Prima di iniziare

  1. Accedi al tuo account Google Cloud . Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti senza costi per l'esecuzione, il test e il deployment dei carichi di lavoro.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Artifact Registry API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  10. Per utilizzare la CLI Conda all'interno dell'ambiente Google Cloud , installa uno dei seguenti gestori di pacchetti Conda:

Avvia Cloud Shell

In questa guida rapida utilizzerai Cloud Shell, un ambiente shell per la gestione delle risorse ospitate su Google Cloud.

Cloud Shell include preinstallati Google Cloud CLI e Conda. gcloud CLI fornisce l'interfaccia a riga di comando principale per Google Cloud.

Avvia Cloud Shell:

  1. Vai alla console Google Cloud .

    Google Cloud console

  2. Nella barra degli strumenti della console Google Cloud , fai clic su Attiva Cloud Shell.

Una sessione di Cloud Shell si apre in un frame nella parte inferiore della console. Utilizza questa shell per eseguire i comandi gcloud.

Configura l'autenticazione

Un canale Conda è l'indirizzo di una posizione in cui sono archiviati uno o più pacchetti Conda. Prima di poter installare pacchetti nell'ambiente Conda, devi aggiungere il canale per quel pacchetto al file di configurazione Conda.

Per consentire all'interfaccia a riga di comando Conda di chiamare direttamente Artifact Registry, devi autenticare il tuo canale Conda in Google Cloud nel seguente modo:

  1. In gcloud CLI, esegui questo comando per archiviare l'identità di un token oauth in una variabile TOKEN:

    export TOKEN="oauth2accesstoken:$(gcloud auth print-access-token)"
    
  2. Aggiungi il repository Conda come canale nel file di configurazione di Conda:

    Conda

    conda config --add channels \
    https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORY
    

    Miniforge

    conda config --add channels \
    https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORY
    

    Mamba

    mamba config --add channels \
    https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORY
    

    Micromamba

    micromamba config --add channels \
    https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORY
    

    Dove:

    • TOKEN memorizza il valore del token oauth generato eseguendo oauth2accesstoken:$(gcloud auth print-access-token).
    • LOCATION è la posizione regionale o multiregionale del repository.
    • PROJECT è l'ID del progetto contenente il repository.
    • REPOSITORY è l'ID del repository.

Aggiornare un token oauth scaduto

Un token oauth scade dopo un'ora. Per eseguire nuovamente l'autenticazione del canale con un nuovo token:

  1. Rimuovi il canale dal file di configurazione:

    Conda

    conda config --remove channels CHANNEL_NAME
    

    Miniforge

    conda config --remove channels CHANNEL_NAME
    

    Mamba

    mamba config --remove channels CHANNEL_NAME
    

    Micromamba

    micromamba config --remove channels CHANNEL_NAME
    
  2. Segui il passaggio precedente che descrive la creazione di un nuovo token oauth.

  3. Aggiungi di nuovo il repository Conda al file di configurazione di Conda utilizzando il nuovo valore del token.

Crea un repository

Crea il repository per il pacchetto.

  1. Esegui il comando seguente per creare un nuovo repository Conda nel progetto corrente denominato quickstart-conda-repo nella località us-west1.

    gcloud artifacts repositories create quickstart-conda-repo \
        --repository-format=conda \
        --location=us-west1 \
        --description="Conda repository"
    
  2. Esegui questo comando per verificare che il repository sia stato creato:

    gcloud artifacts repositories list
    
  3. Per semplificare i comandi gcloud, imposta il repository predefinito su quickstart-conda-repo e la località predefinita su us-west1. Una volta impostati i valori, non è necessario specificarli nei comandi gcloud che richiedono un repository o una posizione.

    Per impostare il repository, esegui questo comando:

    gcloud config set artifacts/repository quickstart-conda-repo
    

    Per impostare la località, esegui questo comando:

    gcloud config set artifacts/location us-west1
    

    Per ulteriori informazioni su questi comandi, consulta la documentazione di gcloud config set.

Carica il pacchetto nel repository

Per caricare un pacchetto nel repository, esegui questo comando:

gcloud artifacts files upload \
    --source=PACKAGE_NAME \
    --repository=REPO_NAME\
    --project=PROJECT \
    --location=LOCATION

Dove:

  • REPO_NAME è l'ID del repository.
  • PROJECT è l'ID del progetto contenente il repository.
  • LOCATION è la posizione regionale o multiregionale del repository.

Per caricare più pacchetti contemporaneamente:

gcloud artifacts files upload \
    --source-directory="DIRECTORY" \
    --repository="REPO_NAME"\
    --project="PROJECT" \
    --location="LOCATION"

Dove:

  • DIRECTORY è l'indirizzo di una cartella locale contenente i tuoi pacchetti Conda.

Visualizza il pacchetto nel repository

Per verificare che il pacchetto sia stato aggiunto, elenca i pacchetti nel repository quickstart-conda-repo.

Esegui questo comando:

gcloud artifacts packages list --repository=quickstart-conda-repo

Per visualizzare le versioni di un pacchetto, esegui questo comando:

gcloud artifacts versions list --package=PACKAGE_NAME

Per visualizzare i file di una versione del pacchetto, esegui questo comando:

gcloud artifacts files list --package=PACKAGE_NAME --version=VERSION

Installa il pacchetto

Per installare il pacchetto che hai appena aggiunto al repository Artifact Registry, esegui questo comando:

Conda

conda install -n ENVIRONMENT_NAME PACKAGE_NAME

Miniforge

conda install -n ENVIRONMENT_NAME PACKAGE_NAME

Mamba

mamba install -n ENVIRONMENT_NAME PACKAGE_NAME

Micromamba

micromamba install -n ENVIRONMENT_NAME PACKAGE_NAME

Dove:

  • ENVIRONMENT_NAME è il nome del tuo ambiente Conda.
  • PACKAGE_NAME è il nome del pacchetto da installare nel repository.

Risoluzione dei problemi

Per ulteriori informazioni, consulta la sezione Risoluzione dei problemi relativi ai pacchetti Conda.

Esegui la pulizia

Per evitare che al tuo account Google Cloud vengano addebitati costi relativi alle risorse utilizzate in questa pagina, segui questi passaggi.

Prima di rimuovere il repository, assicurati che tutti i pacchetti che vuoi conservare siano disponibili in un'altra posizione.

  1. Per eliminare il repository quickstart-conda-repo, esegui questo comando:

    gcloud artifacts repositories delete quickstart-conda-repo
    
  2. Se vuoi rimuovere il repository predefinito e le impostazioni di posizione configurate per la configurazione gcloud attiva, esegui i comandi seguenti:

    gcloud config unset artifacts/repository
    gcloud config unset artifacts/location
    

Passaggi successivi