Search code examples
shellgithubgitlabdevopscicd

Automate gitlab project creation and push to repository


Since I have more than 100 projects to be created in Gitlab, is there a way that creation till push to repository can be automated. Create project->Clone the repo.->Push modified files. This flow needs to be automated. Please provide me reference incase of no specifics.


Solution

  • Projects don't have to be created in advance. You can just push to any namespace to create projects.

    git remote add origin ssh://git@gitlab.example.com/mynamespace/my-newproject.git
    git push -u origin --all
    

    You'll see the server respond back with a message like this:

    remote: The private project mynamespace/my-newprojct was successfully created.
    

    If you wanted a script to create a project from scratch, you might do something like this:

    #!/usr/bin/env bash
    # create-project.sh
    gitlab_host="gitlab.example.com"  # replace with your host
    project_path=$1
    project_name="$(basename "${project_path}")"
    mkdir "$project_name"
    pushd "$project_name"
    echo "# ${project_name}" > README.md
    # Put any additional project file creation steps here
    git init
    git checkout -b main
    git add .
    git commit -m "initial commit"
    git remote add origin "ssh://git@${gitlab_host}/${project_path}.git"
    git push -u origin main
    popd
    

    Usage:

    ./create-project.sh mynamespace/my-newproject
    

    This will create a directory my-newproject, setup repo files, remote, and push it to GitLab creating the project.