在寫gitlab 自動化腳本構建的時候,會發現無權限問題:
The requested URL returned error: 403
此時,可以嘗試採用ssh_key的方式來解決這個問題。
ssh-keygen -t ed25519 -C "[email protected]"
將公鑰粘貼到gitlab的profile頁面中。
接着在gitlab 倉庫上設置好對應的變量,變量名爲SSH_PRIVATE_KEY
。 它的值,就是用你的gitlab 身份賬號生成的一個ssh 私有祕鑰。
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "設置git賬號"
- git config --global user.email "[email protected]"
- git config --global user.name "scq000"
- gitlab_hostname=$(echo "${CI_REPOSITORY_URL}" | sed -e 's|https\?://gitlab-ci-token:.*@||g' | sed -e 's|/.*||g')
- ssh-keyscan "${gitlab_hostname}" >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts