Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hpc-factory
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
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
Eesaan Atluri
hpc-factory
Commits
cd1cbb44
Commit
cd1cbb44
authored
1 month ago
by
Eesaan Atluri
Browse files
Options
Downloads
Patches
Plain Diff
feat: Use gitlab module vs requests to modify cicd vars
parent
222da9a3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scripts/gitlab-ci-vars-updater.py
+36
-61
36 additions, 61 deletions
scripts/gitlab-ci-vars-updater.py
with
36 additions
and
61 deletions
scripts/gitlab-ci-vars-updater.py
+
36
−
61
View file @
cd1cbb44
import
requests
import
json
import
argparse
import
argparse
import
json
import
gitlab
# Load configuration from JSON file
def
load_file
(
file_path
):
def
load_config
(
file_path
):
try
:
try
:
with
open
(
file_path
,
'
r
'
)
as
file
:
with
open
(
file_path
,
"
r
"
)
as
file
:
return
json
.
load
(
file
)
return
json
.
load
(
file
)
except
FileNotFoundError
:
except
FileNotFoundError
:
print
(
f
"
Error: Configuration file
'
{
file_path
}
'
not found.
"
)
print
(
f
"
Error: Configuration file
'
{
file_path
}
'
not found.
"
)
exit
(
1
)
exit
(
1
)
# Function to create or update a GitLab CI/CD variable
def
create_or_update_variable
(
config
,
var_name
,
var_value
):
gitlab_url
=
config
[
"
gitlab_url
"
]
project_id
=
config
[
"
dest_project_id
"
]
private_token
=
config
[
"
private_token
"
]
headers
=
{
"
PRIVATE-TOKEN
"
:
private_token
,
"
Content-Type
"
:
"
application/json
"
}
url
=
f
"
{
gitlab_url
}
/api/v4/projects/
{
project_id
}
/variables/
{
var_name
}
"
# Check if the variable already exists
# Function to create or update a GitLab CI/CD variable
response
=
requests
.
get
(
url
,
headers
=
headers
)
def
create_or_update_variable
(
project
,
var_dict
):
# Check if the variable exists in the project
if
response
.
status_code
==
404
:
p_variable
=
project
.
variables
.
get
(
var_dict
[
"
key
"
]).
asdict
()
print
(
f
"
Variable
'
{
var_name
}
'
does not exist. Creating new variable...
"
)
if
p_variable
:
create_url
=
f
"
{
gitlab_url
}
/api/v4/projects/
{
project_id
}
/variables
"
# Check if the attributes are the same
data
=
{
for
k
,
v
in
var_dict
.
items
():
"
key
"
:
var_name
,
if
p_variable
[
k
]
!=
v
:
"
value
"
:
var_value
,
# If not update the value in the project
"
variable_type
"
:
"
env_var
"
,
# Can be 'file' for file variables
print
(
f
"
Updating key
{
k
}
value from:
{
p_variable
[
k
]
}
->
{
v
}
"
)
"
protected
"
:
False
,
# Set to True if you want the variable protected
project
.
variables
.
update
(
k
,
{
"
value
"
:
v
})
"
masked
"
:
False
# Set to True if you want the variable masked
# Create variable if it doesn't exist in the project
}
create_response
=
requests
.
post
(
create_url
,
headers
=
headers
,
json
=
data
)
if
create_response
.
status_code
==
201
:
print
(
f
"
Variable
'
{
var_name
}
'
created successfully.
"
)
else
:
print
(
f
"
Failed to create variable
'
{
var_name
}
'
. Status code:
{
create_response
.
status_code
}
"
)
elif
response
.
status_code
==
200
:
print
(
f
"
Variable
'
{
var_name
}
'
already exists. Updating variable...
"
)
data
=
{
"
value
"
:
var_value
}
update_response
=
requests
.
put
(
url
,
headers
=
headers
,
json
=
data
)
if
update_response
.
status_code
==
200
:
print
(
f
"
Variable
'
{
var_name
}
'
updated successfully.
"
)
else
:
print
(
f
"
Failed to update variable
'
{
var_name
}
'
. Status code:
{
update_response
.
status_code
}
"
)
else
:
else
:
print
(
f
"
Error checking variable
'
{
var_name
}
'
. Status code:
{
response
.
status_code
}
"
)
for
k
,
v
in
var_dict
.
items
():
print
(
f
"
Creating key
{
k
}
: with value ->
{
v
}
"
)
project
.
variables
.
create
({
"
key
"
:
k
,
"
value
"
:
v
})
# Main function to load the auth config and apply variables
def
main
():
def
main
():
# Setup argument parser
# Setup argument parser
parser
=
argparse
.
ArgumentParser
(
description
=
"
GitLab CI/CD Variables Updater
"
)
parser
=
argparse
.
ArgumentParser
(
description
=
"
GitLab CI/CD Variables Updater
"
)
parser
.
add_argument
(
parser
.
add_argument
(
"
--
auth_
config
"
,
"
--config
_file
"
,
type
=
str
,
type
=
str
,
default
=
"
auth-config.json
"
,
default
=
"
gitlab.ini
"
,
help
=
"
Path to the configuration file (default:
config.json
)
"
help
=
"
Path to the configuration file (default:
gitlab.ini
)
"
,
)
)
parser
.
add_argument
(
parser
.
add_argument
(
"
--var
s
"
,
"
--var
_file
"
,
type
=
str
,
type
=
str
,
default
=
"
ci-variables.json
"
,
default
=
"
ci-variables.json
"
,
help
=
"
Path to the
configuration
file (default: c
onfig
.json)
"
help
=
"
Path to the
CI vars
file (default: c
i-variables
.json)
"
,
)
)
parser
.
add_argument
(
"
--project_id
"
,
type
=
int
,
help
=
"
Gitlab project ID
"
)
# Parse the arguments
# Parse the arguments
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
# Load the configuration file
gl
=
gitlab
.
Gitlab
.
from_config
(
"
uabrc
"
,
[
args
.
config_file
])
config
=
load_config
(
args
.
auth_config
)
project
=
gl
.
projects
.
get
(
args
.
project_id
)
vars_dict
=
load_config
(
args
.
vars
)
# Load the CI vars file
var_list
=
load_file
(
args
.
var_file
)
ci_variables
=
vars_dict
[
"
ci_variables
"
]
# Create or update all variables
for
var_dict
in
var_list
:
create_or_update_variable
(
project
,
var_dict
)
# Loop through and create/update all variables
for
var_name
,
var_value
in
ci_variables
.
items
():
create_or_update_variable
(
config
,
var_name
,
var_value
)
# Run the main function
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
main
()
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment