Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hpc-factory
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
rc
hpc-factory
Merge requests
!196
Feat gl cicd var scripts
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Feat gl cicd var scripts
atlurie/hpc-factory:feat-gl-cicd-var-scripts
into
main
Overview
0
Commits
18
Pipelines
0
Changes
3
Merged
Eesaan Atluri
requested to merge
atlurie/hpc-factory:feat-gl-cicd-var-scripts
into
main
4 months ago
Overview
0
Commits
18
Pipelines
0
Changes
3
Expand
Closes
#232 (closed)
Edited
4 months ago
by
Eesaan Atluri
1
0
Merge request reports
Viewing commit
fac0002a
Prev
Next
Show latest version
3 files
+
131
−
110
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
fac0002a
feat: Use gitlab module vs requests to modify cicd vars
· fac0002a
Eesaan Atluri
authored
4 months ago
scripts/gitlab-ci-vars-reader.py
+
48
−
46
Options
import
requests
import
json
import
argparse
import
json
import
gitlab
# Load configuration from JSON file
def
load_config
(
file_path
):
try
:
with
open
(
file_path
,
'
r
'
)
as
file
:
return
json
.
load
(
file
)
except
FileNotFoundError
:
print
(
f
"
Error: Configuration file
'
{
file_path
}
'
not found.
"
)
exit
(
1
)
# Function to fetch all CI/CD variables from a GitLab project
def
fetch_variables
(
config
):
gitlab_url
=
config
[
"
gitlab_url
"
]
project_id
=
config
[
"
src_project_id
"
]
private_token
=
config
[
"
private_token
"
]
def
fetch_variables
(
project
):
p_variables
=
list
(
project
.
variables
.
list
(
iterator
=
True
))
variables
=
[
var
.
asdict
()
for
var
in
p_variables
]
headers
=
{
"
PRIVATE-TOKEN
"
:
private_token
,
"
Content-Type
"
:
"
application/json
"
}
return
variables
url
=
f
"
{
gitlab_url
}
/api/v4/projects/
{
project_id
}
/variables?per_page=100
"
response
=
requests
.
get
(
url
,
headers
=
headers
)
if
response
.
status_code
==
200
:
variables
=
response
.
json
()
return
variables
else
:
print
(
f
"
Failed to fetch variables. Status code:
{
response
.
status_code
}
"
)
return
[]
def
fetch_sched_variables
(
sched_pipeline
):
variables
=
sched_pipeline
.
attributes
[
"
variables
"
]
return
variables
# Function to format the fetched variables for input into the config file
def
format_variables_for_input
(
variables
):
ci_variables
=
{
var
[
'
key
'
]:
var
[
'
value
'
]
for
var
in
variables
}
return
ci_variables
# Main function to load the config and fetch variables
def
main
():
# Setup argument parser
parser
=
argparse
.
ArgumentParser
(
description
=
"
GitLab CI/CD Variable
s R
eader
"
)
parser
=
argparse
.
ArgumentParser
(
description
=
"
GitLab CI/CD Variable
r
eader
"
)
parser
.
add_argument
(
"
--
auth_
config
"
,
"
--config
_file
"
,
type
=
str
,
default
=
"
auth-config.json
"
,
help
=
"
Path to the configuration file (default: config.json)
"
default
=
"
gitlab.ini
"
,
required
=
True
,
help
=
"
Path to the configuration file (default: gitlab.ini)
"
,
)
parser
.
add_argument
(
"
--var_file
"
,
type
=
str
,
default
=
"
ci-variables.json
"
,
help
=
"
Path to the CI vars file (default: ci-variables.json)
"
,
)
parser
.
add_argument
(
"
--project_id
"
,
type
=
int
,
default
=
""
,
required
=
True
,
help
=
"
Gitlab project ID to read variables from
"
,
)
parser
.
add_argument
(
"
--sched_pipeline_id
"
,
type
=
int
,
help
=
"
Gitlab project scheduled pipeline ID
"
,
)
# Parse the arguments
args
=
parser
.
parse_args
()
# Load the configuration
file
config
=
load_config
(
args
.
auth_config
)
gl
=
gitlab
.
Gitlab
.
from_config
(
"
uabrc
"
,
[
args
.
config_
file
])
project
=
gl
.
projects
.
get
(
args
.
project_id
)
# Fetch and print the variables
variables
=
fetch_variables
(
config
)
if
variables
:
print
(
"
ci_variables:
"
)
formatted_variables
=
format_variables_for_input
(
variables
)
# Print the formatted variables dictionary in JSON format
print
(
json
.
dumps
(
formatted_variables
,
indent
=
4
))
# Fetch project or sched pipeline variables
if
not
args
.
sched_pipeline_id
:
variables
=
fetch_variables
(
project
)
else
:
print
(
"
No variables found.
"
)
sched_pipeline
=
project
.
pipelineschedules
.
get
(
args
.
sched_pipeline_id
)
variables
=
fetch_sched_variables
(
sched_pipeline
)
try
:
with
open
(
args
.
var_file
,
"
w
"
)
as
file
:
json
.
dump
(
variables
,
file
,
indent
=
2
)
except
FileNotFoundError
:
print
(
f
"
Error: Writing File to
'
{
args
.
var_file
}
'"
)
exit
(
1
)
# Run the main function
if
__name__
==
"
__main__
"
:
main
()
Loading