Using the gitlab-python package, I’d like to extract lines from all Dockerfiles. Using my code below, I am able to get project names and url to the repo I want but how can I ensure there is a Dockerfile and read the contents of the Dockerfile.
JavaScript
x
25
25
1
import gitlab
2
import json
3
from pprint import pprint
4
import requests
5
import urllib.request
6
7
8
# private token authentication
9
gl = gitlab.Gitlab('<path_to_gitlab_repo>', private_token=<token_here>)
10
11
gl.auth()
12
13
# list all projects
14
projects = gl.projects.list()
15
for project in projects:
16
# print(project) # prints all the meta data for the project
17
print("Project: ", project.name)
18
print("Gitlab URL: ", project.http_url_to_repo)
19
# print("Branches: ", project.repo_branches)
20
pprint(project.repository_tree(all=True))
21
f = urllib.request.urlopen(project.http_url_to_repo)
22
myfile = f.read()
23
print(myfile)
24
print("nn")
25
The output I get now is :
JavaScript
1
7
1
Gitlab URL: <path_to_gitlab_repo>
2
[{'id': '0c4a64925f5c129d33557',
3
'mode': '1044',
4
'name': 'README.md',
5
'path': 'README.md',
6
'type': 'blob'}]
7
Advertisement
Answer
You can use the project.files.get()
method (see documentation) to get the Dockerfile of the project.
You can then print the content of the Dockerfile/do whatever you want to do with it like this:
JavaScript
1
32
32
1
import gitlab
2
import base64
3
4
5
# private token authentication
6
gl = gitlab.Gitlab(<gitlab-url>, private_token=<private-token>)
7
8
gl.auth()
9
10
# list all projects
11
projects = gl.projects.list(all=True)
12
for project in projects:
13
# print(project) # prints all the meta data for the project
14
# print("Project: ", project.name)
15
# print("Gitlab URL: ", project.http_url_to_repo)
16
17
# Skip projects without branches
18
if len(project.branches.list()) == 0:
19
continue
20
21
branch = project.branches.list()[0].name
22
23
try:
24
f = project.files.get(file_path='Dockerfile', ref=branch)
25
except gitlab.exceptions.GitlabGetError:
26
# Skip projects without Dockerfile
27
continue
28
29
file_content = base64.b64decode(f.content).decode("utf-8")
30
print(file_content.replace('\n', 'n'))
31
32
You might have to adjust the branch name in case there are multiple branches.