【AWS】AWS CodeArtifact ~ AWS CLI ~

■ はじめに

https://dk521123.hatenablog.com/entry/2023/12/26/012343
https://dk521123.hatenablog.com/entry/2024/01/11/233802

の続き。

今回は、CI/CD で Github Actions から、
AWS CodeArtifact にアクセスするために必要そうな
AWS CLI を調べたので、まとめておく。

なお、以下の関連記事は、実際の使用例。

AWS CodeArtifact ~ SBTでCodeArtifactを使う ~
https://dk521123.hatenablog.com/entry/2024/01/20/000329

目次

【1】list-repositories / list-repositories-in-domain
【2】get-repository-endpoint
【3】login
【4】get-authorization-token
【5】update-package-versions-status / dispose-package-versions
【6】update-repository
【7】get-package-version-asset

【1】list-repositories / list-repositories-in-domain

* リポジトリ一覧の取得

https://docs.aws.amazon.com/ja_jp/codeartifact/latest/ug/list-repos.html

list-repositories
https://docs.aws.amazon.com/cli/latest/reference/codeartifact/list-repositories.html
list-repositories-in-domain
https://docs.aws.amazon.com/cli/latest/reference/codeartifact/list-repositories-in-domain.html

1)サンプル

aws codeartifact list-repositories

# list-repositories-in-domain の場合
# aws codeartifact list-repositories-in-domain --domain my-domain

# --query を使う例
# aws codeartifact list-repositories --query 'repositories[*].[name,domainName]'

Output

{
  "repositories": [
    {
      "name": "npm-store",
      "administratorAccount": "111122223333",
      "domainName": "my-domain",
      "domainOwner": "111122223333",
      "arn": "arn:aws:codeartifact:us-west-2:111122223333:repository/my-domain/npm-store",
      "description": "Provides npm artifacts from npm, Inc."
    },
    ...
  ]
}

【2】get-repository-endpoint

* リポジトリのURL先

https://docs.aws.amazon.com/cli/latest/reference/codeartifact/get-repository-endpoint.html

1)サンプル

aws codeartifact get-repository-endpoint \
 --domain my_domain --repository my_repo \
 --format maven --region us-west-2

Output

{
    "repositoryEndpoint": "https://test-domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/maven/test-repo/"
}

【3】login

* ログイン

https://docs.aws.amazon.com/cli/latest/reference/codeartifact/login.html
https://docs.aws.amazon.com/ja_jp/codeartifact/latest/ug/tokens-authentication.html#auth-token-login

1)サンプル

-- tool npm | pip | twine
aws codeartifact login --tool npm \
  --repository my-repo --domain my-domain --domain-owner 111122223333

Output

Successfully configured npm to use AWS CodeArtifact repository
 https://test-domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/npm/test-repo/
Login expires in 12 hours at 2020-11-12 01:53:16-05:00

【4】get-authorization-token

* CodeArtifactから認証トークンを取得する

https://docs.aws.amazon.com/cli/latest/reference/codeartifact/get-authorization-token.html
https://docs.aws.amazon.com/ja_jp/codeartifact/latest/ug/tokens-authentication.html#get-auth-token-api

1)サンプル

aws codeartifact get-authorization-token \
  --domain my_domain --domain-owner 111122223333 \
  --duration-seconds 3600 \
  --query authorizationToken --output text

# --duration-seconds: トークンの有効期限(1時間(3600 秒))
#  => 0 (ユーザのロールの一時クレデンシャル有効期限と同じ) 
#  => 900(15 minutes) ~ 43200 (12 hours)

【5】update-package-versions-status / dispose-package-versions

update-package-versions-status

* 対象バージョンのパッケージ状態(Archived/Published/Unlisted)を更新する

https://docs.aws.amazon.com/cli/latest/reference/codeartifact/update-package-versions-status.html

より抜粋
~~~~~~~~
Updates the status of one or more versions of a package.
[訳] 1又はそれ以上の対象バージョンのパッケージ状態を更新する

Using UpdatePackageVersionsStatus ,
 you can update the status of package versions to Archived , Published , or Unlisted .
UpdatePackageVersionsStatus を使うと
[訳] パッケージ状態をArchived , Published , Unlisted のいずれかに更新できます。

To set the status of a package version to Disposed , use DisposePackageVersions.
[訳] パッケージ状態をDisposed にするには、DisposePackageVersionsを使ってください。
~~~~~~~~

dispose-package-versions

* パッケージのアセットを削除し、パッケージ状態をDisposed にする。

https://docs.aws.amazon.com/cli/latest/reference/codeartifact/update-package-versions-status.html

1)サンプル

aws codeartifact update-package-versions-status \
    --domain test-domain \
    --repo test-repo \
    --format npm \ # npm/pypi/maven/nuget/generic/swift
    --package test-package \ # The name of the package
    --versions 4.0.0 \ # You can set like "string" "string" ...
    --target-status Archived # Archived / Published / Unlisted

【6】update-repository

* リポジトリのプロパティを更新する

https://docs.aws.amazon.com/cli/latest/reference/codeartifact/update-repository.html

1)サンプル

aws codeartifact update-repository \
    --domain test-domain \
    --repository test-repo \
    --description "this is an updated description"

【7】get-package-version-asset

* リポジトリから指定バージョンのアセット(MavenならJAR)を取得する

https://docs.aws.amazon.com/cli/latest/reference/codeartifact/get-package-version-asset.html

1)サンプル

aws codeartifact get-package-version-asset \
    --domain my_domain \
    --repository my_repo \
    --format maven \
    --package my-app \
    --package-version 1.0 \
    --asset 'my-app-1.0.jar' \
    output.jar

参考文献
https://qiita.com/SAITO_Keita/items/fd3c394600b04b6c85b8
https://ktrysmt.github.io/blog/jmespath-awscli-query/

関連記事

AWS CodeArtifact ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/12/26/012343
AWS CodeArtifact ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/01/11/233802
AWS CodeArtifact ~ SBTでCodeArtifactを使う ~
https://dk521123.hatenablog.com/entry/2024/01/20/000329
AWS CodeArtifact ~ 独自JARをリポジトリにあげる ~
https://dk521123.hatenablog.com/entry/2024/01/25/161926
jq コマンド ~ コマンドで JSON を扱う ~
https://dk521123.hatenablog.com/entry/2020/02/01/000000
SBT ~ リポジトリ先を変更・追加するには ~
https://dk521123.hatenablog.com/entry/2024/01/12/191252
AWS CLI ~ --query / JMESPath ~
https://dk521123.hatenablog.com/entry/2024/01/07/000000