How to obtain GitLab Personal Access Token from command line

If you have control over how the Docker container is deployed then save this file locally:

# Inspired by https://gitlab.com/gitlab-org/gitlab/-/blob/master/db/fixtures/development/25_api_personal_access_token.rb
# frozen_string_literal: true

puts "=======================================".color(:red)
puts "---------------------------------------".color(:red)
puts "Creating api access token for root user".color(:red)
puts "---------------------------------------".color(:red)
puts "=======================================".color(:red)

token = PersonalAccessToken.new
token.user_id = User.find_by(username: 'root').id
token.name = 'api-token-for-testing'
token.scopes = ["api"]
token.set_token('ypCa3Dzb23o5nvsixwPA')
token.save

print 'OK'

and modify your docker-compose.yml to include

        image: gitlab/gitlab-ee
        volumes:
          - ./25_api_personal_access_token.rb:/opt/gitlab/embedded/service/gitlab-rails/ee/db/fixtures/production/25_api_personal_access_token.rb

This works for me. Once the GitLab container is initialized and I can see the login page I continue by using a Python library to import my test data and run a bunch of integration tests against the running GitLab instance.

Note 1: I am using GitLab EE above but you can explore /opt/gitlab/embedded/service/gitlab-rails/ for more fixtures directories and place this initialization somewhere else.

Note 2: The original file from the gitlab repository didn't work for me b/c it is designed to be used in development mode and the require failed for me. Also the token_digest is hard-coded and won't match the salt with your GitLab will generate!


Workaround based on HTML parsing: https://github.com/vitalyisaev2/gitlab_token