- Contents -
Gitlab.com Artifacts 簡単にダウンロードしたい
gtilab.com のCIで自動的に作成した成果物(Artifacts)を
単純にダウンロードしてくると、zipファイルになっているのでわざわざ解凍してたんですがー。
もうちょっと簡単に必要なファイルだけダウンロードできないのかな〜と思って
調べてたら、ちゃんとありましたわ。笑
Artifacts へのアクセス方法
普通に成果物をダウンロードする場合、以下のようなURLになるんですが、
JOB_IDで指定するのが面倒ですよね?
https://gitlab.com/your_name/your_repo/-/jobs/99999/artifacts/download
でも、ドキュメントみてみると、以下のようなURLでもアクセスできるようです。
(ドキュメント: Downloading the latest artifacts)
<ref>
の部分には、ブランチ名が入ります。
Download the whole artifacts archive
指定ブランチの最新の成果物をダウンロードする。
https://example.com/<namespace>/<project>/-/jobs/artifacts/<ref>/download?job=<job_name>
Download a single file from the artifacts
指定ブランチの最新の成果物の指定ファイルのみを解凍された状態でダウンロードする。
https://example.com/<namespace>/<project>/-/jobs/artifacts/<ref>/raw/<path_to_file>?job=<job_name>
Browse the latest job artifacts
更に、成果物をブラウジングできるURLもあります。
https://example.com/<namespace>/<project>/-/jobs/artifacts/<ref>/browse?job=<job_name>
curlコマンドでダウンロード
curlコマンドはMacやLinuxで標準的にあるコマンドなので、
curlを使ってダウンロードすると便利でしょう。
Artifacts Zipダウンロード
e.g.)
curl -L -o make2usage.zip https://gitlab.com/tyabuta/go-make2usage/-/jobs/artifacts/master/download?job=compile
Artifacts ファイル指定ダウンロード
e.g.)
curl -L -o make2usage https://gitlab.com/tyabuta/go-make2usage/-/jobs/artifacts/master/raw/bin/darwin64/make2usage?job=compile
-L オプションをつける
これらのURLにアクセスして、curlコマンドでダウンロードしてくる際は、
-L オプションを付けておく必要があります。
リダイレクトを伴うURLなので、-Lオプションがないと、以下のようなHTMLが出力されるだけになります。
こういう出力がされる場合は、curl に -Lオプションを付けましょう。
<html><body>You are being <a href="...">redirected</a>.</body></html>
-L, --location
(HTTP) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will
make curl redo the request on the new place. If used together with -i, --include or -I, --head, headers from all requested pages will be shown. When authentication is
used, curl only sends its credentials to the initial host. If a redirect takes curl to a different host, it won't be able to intercept the user+password. See also
--location-trusted on how to change this. You can limit the amount of redirects to follow by using the --max-redirs option.
When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP response was 301,
302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.
You can tell curl to not change the non-GET request method to GET after a 30x response by using the dedicated options for that:--post301, --post302 and --post303.
プライベートトークン
もし、リポジトリがプライベートリポジトリの場合は、
プライベートトークンをHeaderにつけてリクエストする必要があるはずです。
curl --header "Private-Token: XXXXXXXXXXXXX" <URL>
インストールスクリプトがあると便利
Go言語で作成したバイナリを、直接ダウンロードしてもらうでも良いんですが、
ダウンロード用のシェルスクリプトを用意しておけば、
こんな感じで簡単にコマンドインストールして使ってもらえると思います。
curl https://gitlab.com/tyabuta/go-make2usage/raw/master/install.sh | bash
リポジトリ上に、curlコマンドでバイナリをダウンロードするシェルスクリプトを用意して、
そのシェルスクリプトを直接、bashに流し込んでもらえばインストールができる仕組みです。
e.g.)
install.sh
#!/usr/bin/env bash
repo=tyabuta/go-make2usage
binName=make2usage
if [ -f "$binName" ]; then
exit 0
fi
if [ "$(uname)" == 'Linux' ]; then
pathToBin=bin/linux64/$binName
elif [ "$(uname)" == 'Darwin' ]; then
pathToBin=bin/darwin64/$binName
else
pathToBin=bin/windows64/$binName
fi
url=https://gitlab.com/$repo/-/jobs/artifacts/master/raw/$pathToBin?job=compile
echo "Downloading $url"
curl -L -o $binName $url
chmod a+x $binName
echo "Installed $binName"
おしまい
僕の作ったMakefileからUsage生成してくれるコマンド(make2usage)も、
この方法でバイナリファイルだけ簡単にダウンロードできるようにしております。
https://gitlab.com/tyabuta/go-make2usage
よかったらお試しあれ〜。
今回思ったことは、curlって本当凄い!!
ヘッダ情報も付けれる、リダイレクトもできる
POSTリクエストもできるみたいだし。
使い方を覚えておいて損ないコマンドかな〜。