GitLab_CI_-_Training_notes/README.md
2026-08-07 12:57:32 +03:00

180 lines
4.6 KiB
Markdown

# GitLab CI setups
Since not all of us can get access to gitlab freely, because of many reasons, like money or political situation, to train this skill you should install your own local instance of gitlab first.
To do so you can use your own lab-server(local or vps), I preferred to use local Ubuntu lab on my PC via VirtualBox.
### Recomended resources for GitLab
- 4-8 Gib of RAM (the more the better)
- 2 CPU cores (min)
- 30 Gib free space (I gave to Lab about 65 Gibs)
## Instalation
**Make sure you have Docker installed on the server !!!**
`https://docs.docker.com/engine/install/ubuntu/`
**Make sure you've done port forwarding !!!**
`Machine -> Network -> Port Forwarding -> + ADD NEW -> Name: Gitlab; Protocol: TCP; Host port: 85; Guest port: 85 -> OK`
Install GitLab:
```bash
sudo docker run --detach \
--hostname gitlab.local \
--publish 443:443 \
--publish 85:80 \
--publish 2223:22 \
--name gitlab \
--restart always \
--volume /srv/gitlab/config:/etc/gitlab \
--volume /srv/gitlab/logs:/var/log/gitlab \
--volume /srv/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
```
Check if port 85 is being listened
`sudo ss -tulpn | grep :85`
***Wait for a while till GitLab complete installation***
Then get new password
```bash
sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
```
**Copy this password and save - we'll need it very soon**
## Enter the system
- `In browser:` http://localhost:85
- `login:` root
- `password:` password you got
## Next step is sync
**First of all we need to create Personal Access Token AND SAVE IT**
- `Your avatar(click) -> Edit Profile -> (Left sidebar) Access -> Personal access tokens -> Generate token -> Legacy token (more than enough)`
I'm gonna use IDE of JetBrains(Win10):
- `File -> Settings -> Version Control -> GitLab`
- Server: http://localhost:85
- Token: token you've just got
## CI/CD Setups
Since we using self-hosted GitLab instance, let's install GitLab Runner
```bash
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
```
```bash
# make it executable
sudo chmod +x /usr/local/bin/gitlab-runner
```
```bash
#create user for runner
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
```
```bash
# set it as service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
```
### Check server ip
```
hostname -I
```
## Get registration token
1. open project
2. Settings -> CI/CD -> runners -> New project runner
3. copy token
4. remember gitlab url(which one you use to open gitlab in browser or use IP)
#### In runner(Gitlab UI) allow untagged jobs
1. Project -> Setting -> CI/CD -> Runners
2. Find `ubuntu-runner`, click Edit
3. Set mark in checkbox
4. Save changes
## Register runner
```
sudo gitlab-runner register
```
#### There will be questions
```plain
Enter the GitLab instance URL: http://10.0.2.15:85 <-- insert yours IP!
Enter the registration token: [вставь токен]
Enter a description: ubuntu-runner
Enter tags: docker
Enter optional maintenance note:
Registering runner... succeeded
Enter an executor: docker
Enter the default Docker image: docker:latest
```
## Setup config (URGENT)
```
sudo nano /etc/gitlab-runner/config.toml
```
Set it like this
```toml
concurrent = 1
check_interval = 0
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "ubuntu-runner"
url = "http://10.0.2.15:85" # <-- IP instead of localhost!
token = "glrt-..." # <-- your token - do not touch it!
executor = "docker"
clone_url = "http://10.0.2.15:85" # <-- add this line!
[runners.docker]
image = "docker:latest"
privileged = true # <-- needs for Docker-in-Docker
extra_hosts = ["gitlab.local:10.0.2.15"] # <-- if you use gitlab.local
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
```
**Change 10.0.2.15 to your IP from hostname -I**
### Restart runner
```
sudo gitlab-runner restart
sudo gitlab-runner verify
```
***Respond should be:***
`verifying runner ... is valid runner=...`
## Test pipeline
1. in project root(IDE) create `.gitlab-ci.yml`
2. Put this code:
```yaml
hello:
stage: test
image: alpine:latest
script:
- echo "Runner works!"
- echo "GitLab CI is alive!"
```
**Then push**
```
git add .gtlab-ci.yml
git commit -m "test runner"
git push origin main
```
**After that go to Ci/CD -> Pipelines -> check if green pipeline appeared**
### Check if all works fine
```
sudo gitlab-runner list
sudo journalctl -u gitlab-runner -f
```