Prerequisites
This article assumes you are already familiar with version control tool called git
To learn more about git you can check this https://mohsensy.github.io/developer-tools/2017/08/25/introduction-to-git-and-version-control.html
Introduction
When working on any software project it is very important to be able to share code with
other developers in your team and keep track of all changes on your code also many
other things, you can track changes to your code locally using a git repository but to
share code with others you must create a repository on one of the public git servers
this includes https://github.com, https://bitbucket.org
https://gitlab.com/users/sign_in
In this article we will use gitlab to host our own git repository and share it with
other developers in the team, we will also learn basic manual code deployment to a
server that we have.
Hint: This article assumes you have access to a server of your choice.
Create a new repository on gitlab
After you login to gitlab you can see your dashboard which contains the projects
that you have access to them, if this is the first time you login to gitlab your
dashboard will be empty, choose New project as seen here
Choose a project name, description and visibility then click Create project.
What is visibility??
There are three types of project visibility described as follows:
- Private: This means that the project will be private and only people whose you
give them access can see the project when exploring gitlab or using the project’s URL.
- Internal: Internal projects can be accessed by any gitlab authenticated user.
- Public: These projects can be accessed by anyone even if he is not authenticated to gitlab
or not a gitlab user even.
You can create as many projects as you want from any type.
The following screen shot shows the page for the new repository.
The page has instructions for cloning the repository, creating a new repository in an
existing folder and pushing it to gitlab and pushing an existing local repository to
gitlab, we will take the third option and assume you already have a local repository
and want to share it with others.
Navigate to the folder where your local repository exists and execute the following
commands
git remote add origin git@gitlab.com:mohsen47/test.git git push -u origin --all
The first command is used to create a remote or we can say an alias to the URL of
gitlab repository so we can use it every time we need to push or pull code from it
and not write the whole URL every time.
The second one pushes your git commits on all branches to the remote repository,
if you refresh your new repository page you will see the code you just pushed or the
activity tab that shows you already pushed code, this depends on your personal settings.
Give access to your friends
Now after you created your first gitlab repository ans pushed your local repository
to it, now is the time to share this repository with your friends, we chose to make
the repository private so no other people can access it at all, if they search on gitlab
the repository will not be visible in the search results, if someone gets the URL to
your repository and try to navigate to it they will get nothing because the repository is
private.
To give access to others to your repository select settings –> Members from the
left hand side panel, as shown in the following screenshot.
Now you are in the page to choose your friends and give them access to your repository,
you will have to give three kinds of information, first the member to invite, permission and
expiration date, we will explain each one below.
- Members to invite: Your friends who will work with you on your project need to be
gitlab users too, search for them using their unique gitlab user name or email address.
- Permissions: Each user you give him access to your account will have a specific access
level permissions, there are three kinds of permissions:
- Guest: These users have access to project issues, wiki and jobs, give this
permission to users who do not need access to your code and can only view
issues and build jobs.
- Reporter: These users have the permissions for Guests plus the ability to create
issues, download source code and see merge requests, these users will help you
to organize your work using gitlab issues and will not be able to change your code.
- Developer: These users have the same permissions as Reporter plus the abilty to push
to non-protected branches, create merge requests, create wikis, milestones and
environments, these are your core developers who will write the code for you and
fix bugs or any issues.
- Maintainer: These users have the permissions of developers plus the ability to push
to protected branches, change many settings and runners and use gitlab pages.
- Owner: This is you the creator of the project, it cannot be assigned to anyone else
and you have absolute control even you can delete the repository.
- Access expiration date: You can set a date where this user will automatically lose
access to the repository if you want to give temporal access to your repository
for this user, if you do not specify an expiration date the user will never be
expired and their access will continue until you explicitly disable it.
Download the repository to your friend’s workstation
Now after you have given access to your friend it is time to download the repository
to their workstation so they can start working on it.
Open the project’s main page and select clone from the right of the screen, you can clone
with SSH which needs to setup a SSH key pair on your account or with HTTPS which only
needs your username and password let us use HTTPS for now, copy the HTTPS url and
execute git clone <url> to download the repository on your local machine, this
creates a new local repository with git init and a remote called `origin` that points
to your gitlab repository.
Deploy code to your server
Now your code is on gitlab, you can share it with your friends and can both push and pull
it from and to gitlab, you still need to run your code on your server and show your work
to others either your friends so they can make sure it is running correctly also to your
users, after all your code will not always live on your local workstation it must be deployed
to a server so others can use your app.
In this section we will describe how to manually deploy your code to your own server
and in a future article we will automate all this using gitlab CI and ansible, stay tuned.
To achieve the tasks here you need to login to your Linux server using putty or any SSH
client you prefer.
- Clone the repository from gitlab to your server
git clone https://gitlab.com/mohsen47/test.git
Now you have the code on your sever, you can serve the application to your users
using a web server for example, this depends on your programming language and framework
used to create the app.
- If you update your repository on gitlab all you need to do is just login again to your
server and execute this command to update the code on your server.
git pull origin master
This will update the code on your server assuming your updated code exists on your master
branch, you can create multiple local repositories for different branches of your gitlab
repository or you can checkout different branches in your current local repository.
Conclusion
In this introductory article we learned basics of gitlab usage, however we only scratched
the surface of gitlab there are so many many great features in gitlab that we will learn
about them in future articles, we also learned how to share code with other developers and
give them access to our repository and finally manual code deployment to our own server.
We will build upon these skills in future articles so please make sure to read it and
understand it, if you have any questions please leave a comment below.