问题
I've got a gitlab running on server. For now, I've also got just a list of users and emails needed to be add into gitlab. Is there a way to do this automatically? (i.e. by script/service)
回答1:
You can use the GitLab API to create users in a script. Recent versions of curl can url-encode POST data for you. Otherwise spaces will have to be %20
and --data
instead of --data-urlencode
.
curl --header "PRIVATE-TOKEN: QVy1PB7sTxfy4pqfZM1U" --data-urlencode "email=jon@doe.com&password=defaultpassword&username=jdoe&name=Jon Doe" "http://example.com/api/v3/users"
Curl is the path of least resistance with your preferred shell language (in my case Bash). If you don't want to use curl to script creating your users there are numerous libraries for different languages to interact with the GitLab API. Take your pick. Popular libraries include:
- Java - java-gitlab-api
- PHP - php-gitlab-api
- Python - pyapi-gitlab, python-gitlab, or python-gitlab3
- Ruby - Gitlab wrapper
I'm sure there are many more API libs for other languages just google for your preferred language and include GitLab API as part of the query.
回答2:
If it is possible (not tested yet), it could have been done through the gitlab-shell library, which exposes the API of gitlab.
But adding a user isn't part of its API yet (it only add ssh keys).
Issue 1942 does mention there is an API in gitlab itself.
You can take example on tester like spec/features/gitlab_flavored_markdown_spec.rb:
require 'spec_helper'
describe "GitLab Flavored Markdown" do
let(:project) { create(:project_with_code) }
let(:issue) { create(:issue, project: project) }
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
let(:fred) do
u = create(:user, name: "fred")
project.team << [u, :master]
u
end
回答3:
cli-gitlab installed through npm is working pretty well for me for both LDAP (AD) and local users. It saves the token of the 'admin' user which makes it a lot easier to script/automate things.
来源:https://stackoverflow.com/questions/19863786/is-there-a-way-to-add-users-automatically-into-gitlab