溫馨提示×

GitLab與Ubuntu如何集成

小樊
37
2025-10-10 08:29:07
欄目: 智能運維

Integrating GitLab with Ubuntu: A Step-by-Step Guide
GitLab is a powerful web-based Git repository manager that integrates seamlessly with Ubuntu, enabling version control, CI/CD, issue tracking, and collaboration. Below is a structured guide to installing, configuring, and optimizing GitLab on Ubuntu.


1. Prerequisites

Before installing GitLab, ensure your Ubuntu system meets the following requirements:

  • Ubuntu Version: Ubuntu 22.04 LTS (recommended for long-term support) or later.
  • System Resources: At least 4GB RAM (2GB minimum, but 4GB+ is recommended for production), 2 CPU cores, and 25GB+ storage.
  • Dependencies: Update your system and install essential packages:
    sudo apt update && sudo apt upgrade -y
    sudo apt install -y curl openssh-server ca-certificates tzdata postfix
    
    During postfix installation, select Internet Site and enter your domain/server name (e.g., gitlab.example.com).

2. Install GitLab Community Edition (CE)

GitLab CE is the free, open-source version suitable for most teams. Follow these steps:

a. Add GitLab’s Official Repository

GitLab provides a Debian repository to simplify installation and updates. Run the following commands to add the repository:

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

This script adds GitLab’s GPG key and configures the repository.

b. Install GitLab CE

Specify your server’s external URL (domain or IP address) during installation. Replace <YOUR_DOMAIN_OR_IP> with your actual value (e.g., http://192.168.1.100):

sudo EXTERNAL_URL="<YOUR_DOMAIN_OR_IP>" apt install gitlab-ce -y

The -y flag auto-confirms installation prompts.


3. Configure GitLab

After installation, configure GitLab to suit your environment:

a. Modify External URL (Optional)

If you need to change the external URL later, edit the GitLab configuration file:

sudo nano /etc/gitlab/gitlab.rb

Find the line external_url '<YOUR_DOMAIN_OR_IP>' and update it. Save the file and exit (Ctrl+O, Enter, Ctrl+X).

b. Reconfigure GitLab

Apply configuration changes by running:

sudo gitlab-ctl reconfigure

This command updates GitLab’s settings, restarts services, and ensures consistency.

c. Restart GitLab

To apply changes immediately, restart the GitLab service:

sudo gitlab-ctl restart

This ensures all components (web server, database, etc.) are running with the new configuration.


4. Access GitLab

Once configured, access GitLab via your browser:

  • Open a browser and navigate to http://<YOUR_DOMAIN_OR_IP>.
  • The first time you log in, use the default administrator credentials:
    • Username: root
    • Password: Retrieved from /etc/gitlab/initial_root_password (run cat /etc/gitlab/initial_root_password to view; delete this file after use for security).

5. Post-Installation Setup

Optimize GitLab for security and usability:

a. Change the Root Password

Log in as root and immediately change the password. GitLab will prompt you to set a new one during the first login.

b. Configure HTTPS (Recommended)

Use Let’s Encrypt to enable free SSL/TLS encryption. Install Certbot and run:

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d <YOUR_DOMAIN>

Follow the prompts to complete the setup. This automatically configures HTTPS for your GitLab instance.

c. Configure Firewall

Allow HTTP (80), HTTPS (443), and SSH (22) ports to enable external access:

sudo ufw allow http
sudo ufw allow https
sudo ufw allow ssh
sudo ufw enable

Verify the rules with sudo ufw status.

d. Set Up SSH Keys

To push/pull code securely, add your SSH key to GitLab:

  1. On your local machine, generate an SSH key (if you don’t have one):
    ssh-keygen -t rsa -C "your_email@example.com"
    
  2. Copy the public key (~/.ssh/id_rsa.pub) to your clipboard.
  3. In GitLab, go to User Profile > Settings > SSH Keys and paste the key.
  4. Test the connection:
    ssh -T git@<YOUR_DOMAIN>
    
    You should see a welcome message (e.g., “Welcome to GitLab, @your_username!”).

6. Optional: Advanced Configuration

For production environments, consider these enhancements:

a. Backups

Schedule regular backups to protect your data. Run the following command to create a manual backup:

sudo gitlab-rake gitlab:backup:create

Backups are stored in /var/opt/gitlab/backups by default. To automate backups, add a cron job (e.g., daily at 2 AM):

sudo crontab -e

Add this line:

0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1

b. GitLab CI/CD

Enable automated build/test/deploy pipelines by creating a .gitlab-ci.yml file in your project root. Example:

stages:
  - test
  - deploy

test_job:
  stage: test
  script:
    - echo "Running tests..."
    - npm test  # Replace with your test command

deploy_job:
  stage: deploy
  script:
    - echo "Deploying to production..."
    - scp -r ./ user@server:/var/www/html  # Replace with your deploy command
  only:
    - main  # Run only on the main branch

Commit and push the file to trigger the pipeline.


By following these steps, you can successfully integrate GitLab with Ubuntu, creating a robust platform for source code management and team collaboration. For further customization, refer to GitLab’s official documentation.

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女