[Gitlab] Docker Gitlab으로 pages(mkdoc) 기능 사용해보기(with runner)
Pages Daemon에 Gitlab 프로젝트 내 소스코드를 runner를 이용하여 배포하고 Gitlab Nginx에서 Reverse Proxy를 구성해 Pages Daemon으로 Proxing하여 웹호스팅을 하는 것이 일련의 흐름이다.
이번에 구현해 볼 pages의 형태는 Pages가 Gitlab과 동일한 서버(or Container)에서 구동되며 nginx에서 Reverse Proxing을 통해 서비스 되는 형태이다.
Gitlab과 Pages는 동일 Container내에서, Runner는 별도의 Container에서 구동시켜 연동할 예정이다.
구축에 앞서 Pages에서 사용할 도메인이 필요한데 이때 Gitlab과 다른 하위(2차)도메인이 필요하다.
Gitlab : test-gitlab.test.com
Pages : test-gitlab-pages.test.com
또한 프로젝트의 Pages 도메인은 3차 도메인으로 생기게 되므로 *(와일드카드 or sharepoint) DNS 레코드 설정도 필요하다. 예를 들어 <프로젝트이름>.pages.test.com 이렇게 Pages 도메인이 생성 되므로 *.pages.test.com의 레코드 설정이 필요하다.
참고 : https://docs.gitlab.com/administration/pages/#prerequisites
Mkdocs 관련 참고 : https://gitlab.com/pages/mkdocs
Docker를 이용하여 Gitlab, Runner 실행
Docker-compose를 사용하며 https는 사용하지 않는다.
docker-compose.yml
services:
gitlab:
container_name : gitlab
image: 'gitlab/gitlab-ce:latest'
restart: always
hostname: 'test-gitlab'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://test-gitlab.test.com'
nginx['listen_port'] = 80
nginx['listen_https'] = false
gitlab_rails['initial_root_password'] = '1qaz@WSX'
gitlab_pages['enable'] = true
# pages
pages_external_url "http://test-gitlab-pages.test.com"
pages_nginx['ssl'] = false
pages_nginx['listen_https'] = false
gitlab_pages['listen_proxy'] = '0.0.0.0:8090'
gitlab_pages['internal_gitlab_server'] = 'http://localhost:8080'
GITLAB_TIMEZONE: Asia/Seoul
# Add any other gitlab.rb configuration here, each on its own line
ports:
- '80:80'
- '443:443'
- '2222:22'
volumes:
- '/srv/gitlab2/config:/etc/gitlab'
- '/srv/gitlab2/logs:/var/log/gitlab'
- '/srv/gitlab2/data:/var/opt/gitlab'
- '/srv/gitlab2/backups:/var/opt/gitlab/backups'
- '/srv/gitlab2/certs:/var/opt/gitlab/certs'
networks:
- gitlab
gitlab-runner:
container_name: gitlab-runner
image: 'gitlab/gitlab-runner:latest'
restart: always
depends_on:
- gitlab
volumes:
- '/srv/gitlab-runner/config:/etc/gitlab-runner'
- '/var/run/docker.sock:/var/run/docker.sock'
networks:
- gitlab
networks:
gitlab:
name: gitlab-network
Container 실행
docker-compose -f docker-compose.yml up -d
docker-compose -f docker-compose.yml ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
gitlab gitlab/gitlab-ce:latest "/assets/wrapper" gitlab 2 hours ago Up 2 hours (healthy) 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp, 0.0.0.0:2222->22/tcp, :::2222->22/tcp
gitlab-runner gitlab/gitlab-runner:latest "/usr/bin/dumb-init …" gitlab-runner 2 hours ago Up 2 hours
Gitlab Runner Register
모든 프로젝트에서 Share 되는 runner로 구성한다.
Runner Container 접속
docker exec -it gitlab-runner /bin/bash
Gitlab Runner Register
gitlab-runner register
Gitlab URL 입력 : test-gitlab.test.com
Runtime platform arch=amd64 os=linux pid=29 revision=ef334dcc version=17.10.1
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
Registration token 입력 (Shared)
Gitlab WEB GUI : Admin - CI/CD - Runners - Registration token 복사
Group이나 Project에 Runner를 할당 하려면 해당 group - CI/CD - Runner - Registration token 복사
Enter the registration token:
oMqXjzcznzhauAacgh7p
runner tag 입력 : test-runner
Enter a description for the runner:
[test-gitlab-runner]: <엔터>
Enter tags for the runner (comma-separated):
test-runner
Enter optional maintenance note for the runner:
<엔터>
executor 입력 : 여기서는 shell
Enter an executor: custom, ssh, docker-autoscaler, docker+machine, kubernetes, instance, shell, parallels, virtualbox, docker, docker-windows:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
Mkdocs 설치
설치 참조 : https://www.mkdocs.org/user-guide/installation/
apt update
apt install python3 python-pip
pip install mkdocs
참고: Mkdocs 설치 에러
pip install mkdocs
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.
See /usr/share/doc/python3.12/README.venv for more information.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking youion or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
위와 같은 에러가 발생하면
python3 -m pip config set global.break-system-packages true
runner는 gitlab-runner user로 실행되니 gitlab-runner 사용자로 스위치 한 후 같은 명령어를 실행한다.
Runner Start
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
gitlab-runner start
Gitlab 에서 Runner 확인
Gitlab WEB GUI : Admin - CI/CD - Runners
참고
Pipeline Job에서 아래와 같은 에러가 난다면
Runner 수정
Run untagged jobs 옵션 선택
Pages 프로젝트 생성
Pages Template을 통해 프로젝트를 생성한다.
Pipelines 확인 : Build - Pipelines
소스 코드가 수정되면 .gitlab-ci.yml 에 의해 배포된다.
Pages URL 확인 : Deploy - Pages
참조 : https://gitlab-docs.infograb.net/ee/administration/pages/source.html#dns-%EA%B5%AC%EC%84%B1