How To Create A GitLab Project Using API
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have a basic overview of GitLab and its components, we have also created our 1st GitLab Project.
https://cloudaffaire.com/what-is-gitlab/
In this blog post, we will discuss how to create a GitLab Project using API and its features with a hands-on demo. The demo is created using API as we are taking a programmatic approach to cover most of the demo. But we have also provided the console steps as well so that if you want to perform the demo from GitLab console, you can do the same.
In order to access GitLab through API, you will need one private token for authorization. In the 1st step, we will create a private token.
How To Create A Private Token In GitLab?
Step 1: Log in to your GitLab account and in the top right corner, click your account icon and then click on ‘Settings’.
Step 2: Navigate to ‘Access Tokens’. Provide a name and expiry date, scope and click ‘Create personal access token’.
Our private access token created successfully. Copy the access token and keep in a safe place.
Now we are ready for all the demo so lets get started.
What Is GitLab Project?
In GitLab, you can create projects for hosting your codebase, use it as an issue tracker, collaborate on code, and continuously build, test, and deploy your app with built-in GitLab CI/CD. Your projects can be available publicly, internally, or privately, at your choice and is controlled by project visibility. GitLab does not limit the number of private projects you create. In layman term a project is basically your git remote repository hosted in GitLab.
Project Visibility:
- private: Project access must be granted explicitly for each user.
- internal: The project can be cloned by any logged in user.
- public: The project can be accessed without any authentication.
How To Create And Update A GitLab Project Using API?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
##################### ## GitLab Projects ## ##################### ## Prerequisites: GitLab Access Token ## One CentOS system with internet access ## git, curl and jq package installed ## ------------------------------------- ## Create and update a project in GitLab ## ------------------------------------- ## Create a project in GitLab ## GitLab Console => Projects => Your Projects => New Project => PROJECT_ID=$(curl --silent --header "PRIVATE-TOKEN: -XPOST "https://gitlab.com/api/v4/projects?name= ## Get the project details ## You can not get all project details in one place from the console ## GitLab Console => Projects => Your Projects => curl --silent --header "PRIVATE-TOKEN: -XGET "https://gitlab.com/api/v4/projects/$PROJECT_ID" | jq '.' ## Edit your project and add a description to your project ## GitLab Console => Projects => Your Projects => New Project => ## => Settings => General => Project description curl -s -H "Content-Type: application/json" \ -H "PRIVATE-TOKEN: -XPUT --data '{"description":"Gitlab Project" }' \ https://gitlab.com/api/v4/projects/$PROJECT_ID | jq '.description' |
What Is GitLab Project Repository?
A repository is what you use to store your codebase in GitLab and change it with version control. A repository is part of a project, which has a lot of other features.
How To Create And Update A File Directory In GitLab Project Repository Using API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
## -------------------------------------------- ## Create an update a GitLab project repository ## -------------------------------------------- ## Create one file in your GitLab project repository ## GitLab Console => Projects => Your Projects => ## => Project Drop Down (+ sign) => New file curl -s --request POST --header "PRIVATE-TOKEN: --data '{"branch": "master", "author_email": "debjeettoni@gmail.com", "author_name": "Debjeet Bhowmik", "content": "file created through api", "commit_message": "myfile1.txt added"}' \ "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/files/myfile1%2Etxt" | jq '.' ## Update one file in your GitLab project repository ## GitLab Console => Projects => Your Projects => ## => curl -s --request PUT --header "PRIVATE-TOKEN: --data '{"branch": "master", "author_email": "debjeettoni@gmail.com", "author_name": "Debjeet Bhowmik", "content": "file updated through api", "commit_message": "myfile1.txt updated"}' \ "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/files/myfile1%2Etxt" | jq '.' ## Create one file inside a folder in your GitLab project repository ## GitLab Console => Projects => Your Projects => ## => Project Drop Down (+ sign) => New Directory curl -s --request POST --header "PRIVATE-TOKEN: --data '{"branch": "master", "author_email": "debjeettoni@gmail.com", "author_name": "Debjeet Bhowmik", "content": "file created through api", "commit_message": "myfile2.txt added inside mydir1"}' \ "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/files/mydir1%2Fmyfile2%2Etxt" | jq '.' ## List all files and directory in your GitLab project repository ## GitLab Console => Projects => Your Projects => curl --silent --header "PRIVATE-TOKEN: -XGET "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/tree" | jq '.' ## Delete one file from your GitLab project repository ## GitLab Console => Projects => Your Projects => ## => curl -s --request DELETE --header "PRIVATE-TOKEN: --data '{"branch": "master", "author_email": "debjeettoni@gmail.com", "author_name": "Debjeet Bhowmik", "commit_message": "myfile1.txt deleted"}' \ "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/files/myfile1%2Etxt" |
Commits:
Commits can be thought of as snapshots or milestones along the timeline of a Git project. Commits are created with the git commit command to capture the state of a project at that point in time.
How To Create, List and revert Commit In GitLab Project Repository:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
## ---------------------------------------------------------- ## Create, List and revert a GitLab project repository commit ## ---------------------------------------------------------- ## Create a new commit in GitLab project repository using api ## GitLab Console: NA curl --silent \ --request POST \ --form "branch=master" \ --form "commit_message=commit using api" \ --form "start_branch=master" \ --form "actions[][action]=create" \ --form "actions[][file_path]=mynewfile.txt" \ --form "actions[][content]=this file created through api" \ --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/commits" | jq '.' ## List all commit details in your GitLab project repository ## GitLab Console => Projects => Your Projects => ## => Left Navigation Pane => Repository => Commits curl -s --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/commits" | jq '.' ## List only commit messages in your GitLab project repository curl -s --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/commits" | jq '.[].message' ## Revert the latest commit in GitLab project repository LATEST_COMMIT_ID=$(curl -s --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/commits" | jq -r '.[0].id') && curl --request POST --header "PRIVATE-TOKEN: --form "branch=master" \ "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/commits/$LATEST_COMMIT_ID/revert" | jq '.' |
Branches:
A branch is a version of a project’s working tree. You create a branch for each set of related changes you make. This keeps each set of changes separate from each other, allowing changes to be made in parallel, without affecting each other. GitLab sets master as the default branch for your project. Branches can also be protected to prevent unauthorized access.
How To Create, List And Merge GitLab Project Repository Branch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
## --------------------------------------------------------- ## Create, List and merge a GitLab project repository branch ## --------------------------------------------------------- ## Create a new branch from master in GitLab project repository ## GitLab Console => Projects => Your Projects => ## => Project Drop Down (+ sign) => New Branch curl -s --request POST --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/branches?branch=mybranch&ref=master" | jq '.' ## List all branches of a GitLab project repository curl -s --request GET --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/branches" | jq -r '.[].name' ## Add one file to your new branch ## Create a new commit in GitLab project repository using api ## GitLab Console: NA curl --silent \ --request POST \ --form "branch=mybranch" \ --form "commit_message=myfile3 added" \ --form "start_branch=mybranch" \ --form "actions[][action]=create" \ --form "actions[][file_path]=myfile3.txt" \ --form "actions[][content]=this file created through api" \ --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/repository/commits" | jq '.' ## Create a merge request in your GitLab project repository curl -s --request POST --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/merge_requests?source_branch=mybranch&target_branch=master&title=api_mr_req" | jq '.' ## List all merge request in your GitLab project repository curl -s --request GET --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/merge_requests" | jq '.' ## Accept the merge request and delete source branch MR_IID=$(curl -s --request GET --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/merge_requests" | jq -r '.[].iid') && curl -v -s --request PUT --header "PRIVATE-TOKEN: "https://gitlab.com/api/v4/projects/$PROJECT_ID/merge_requests/$MR_IID/merge?should_remove_source_branch=false" | jq '.' ## Delete your project curl --silent --header "PRIVATE-TOKEN: -XDELETE "https://gitlab.com/api/v4/projects/$PROJECT_ID" | jq '.' |
Hope you enjoyed this article. In the next blog post, we will discuss how to create a GitLab project by importing from an external source.
To get more details on GitLab you can follow the below link.
https://docs.gitlab.com/ee/README.html
To Get more details on Git you can follow the below links.
https://cloudaffaire.com/category/devops/git/