Ray Lee | 李宗叡
Learn or Die
Published in
Jul 9, 2024

# 前言

紀錄如何使用 GitLab 的 secure file

# 上傳

上傳沒什麼特別,單純的把機敏檔案上傳即可,上傳後不管是誰都無法直接的瀏覽檔案,除非把它下載下來,所以下載的權限必須控管

# 下載

要下載 secure file 會需要 access token,這個在 登入 -> Preference -> Access Token 這邊來申請,由於下載 secure file 屬於呼叫 API,因此 Access Token 的 scope 是 API

取得 token 之後,便可以用以下的 curl 來下載 secure file

curl --header "PRIVATE-TOKEN: $ACCESS_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/secure_files" > files.json
  • $ACCESS_TOKEN:我們取得 Access Token 之後,可以將它放在 CI/CD 當中的 variable,這樣就可以在 CI/CD 流程中使用到它
  • $CI_PROJECT_ID:GitLab 預設的 variable,可以取得當前 project id

files.json 的內容範例如下

[{"id":123,"name":"secureFileName1","checksum":"fjeafoieawj","checksum_algorithm":"sha256","created_at":"2023-12-18T07:45:08.203Z","expires_at":null,"metadata":null,"file_extension":null}]

array 當中的每一個 object 都代表一個 secure file,我們需要取得 需要的 secure file ID 來做進一步的下載,因此使用 jq command 來進一步取得 ID

FILE_ID=$(cat files.json | jq '.[] | select(.name=="secureFileName1").id')

最後,直接下載該 secure file,$FILE_ID 就是上面取得的 ID

curl --header "PRIVATE-TOKEN: $ACCESS_TOKEN" https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/secure_files/$FILE_ID/download --output env_decrypt_key

--

--

Ray Lee | 李宗叡
Learn or Die

It's Ray. I do both backend and frontend, but more focus on backend. I like coding, and would like to see the whole picture of a product.