如何为复刻的GitLab CE项目配置云端Ubuntu服务器持续部署(CD)?
Got it, let's walk through setting up a continuous deployment pipeline for your forked GitLab CE repo to a cloud Ubuntu server. I’ve tackled this exact scenario a few times, so here’s a practical, step-by-step breakdown that should work for your use case:
First, let’s get the basics sorted:
- Server Setup: Ensure your Ubuntu cloud server has the necessary ports open (80, 443, 22 for GitLab and SSH access). Create a dedicated deploy user (e.g.,
gitlab-deploy) with sudo access—never use root for deployments. Set up passwordless SSH access from your CI/CD runner to this user (generate an SSH key pair, add the public key to~gitlab-deploy/.ssh/authorized_keyson the server). - Repo Setup: Make sure your forked GitLab CE repo is hosted on a platform that supports CI/CD (GitLab.com, GitHub, or your own GitLab instance) and you have admin rights to configure pipeline variables.
I recommend two low-friction options, depending on your hosting platform:
- GitLab CI/CD (if hosted on GitLab): Built-in, no extra infrastructure needed—perfect for quick setup.
- GitHub Actions (if hosted on GitHub): Just as straightforward, with a huge library of pre-built actions.
- Jenkins (for custom workflows): Only use this if you need super complex, on-premise-compliant pipelines—it’s more maintenance-heavy.
We’ll focus on GitLab CI/CD first, since it’s native to GitLab CE, then cover the GitHub alternative.
Step 1: Add CI/CD Variables
In your repo’s settings, go to CI/CD > Variables and add these sensitive values (mark them as "protected" and "masked"):
SSH_PRIVATE_KEY: The private key for yourgitlab-deployuser.SERVER_IP: Your cloud Ubuntu server’s public IP.
Step 2: Write the .gitlab-ci.yml File
Drop this file in your repo root—tweak it to match your specific code changes:
stages: - build - deploy # Stage 1: Compile GitLab assets and dependencies build_gitlab: stage: build image: ruby:2.7 # Match the Ruby version your GitLab CE branch uses before_script: - apt-get update && apt-get install -y build-essential libyaml-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev zlib1g-dev - gem install bundler - bundle install --without development test # Skip dev/test dependencies for speed script: - bundle exec rake gitlab:assets:compile RAILS_ENV=production # Compile frontend assets # Add any custom build commands here if your changes require extra compilation artifacts: paths: - public/ # Compiled static assets - app/ # Modified Ruby code (adjust this based on your changes) expire_in: 1 week # Clean up old artifacts automatically # Stage 2: Deploy to the Ubuntu server deploy_to_server: stage: deploy image: alpine:latest # Lightweight image for SSH/rsync before_script: - apk add --no-cache openssh-client rsync - mkdir -p ~/.ssh - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa # Critical for SSH security - ssh-keyscan -H "$SERVER_IP" >> ~/.ssh/known_hosts # Avoid host key prompts script: # 1. Stop GitLab to prevent file conflicts during deployment - ssh gitlab-deploy@"$SERVER_IP" "sudo gitlab-ctl stop" # 2. Sync compiled assets and modified code to the server - rsync -avz --exclude='.git' ./public/ gitlab-deploy@"$SERVER_IP":/opt/gitlab/embedded/service/gitlab-rails/public/ - rsync -avz --exclude='.git' ./app/ gitlab-deploy@"$SERVER_IP":/opt/gitlab/embedded/service/gitlab-rails/app/ # 3. Reconfigure and restart GitLab to apply changes - ssh gitlab-deploy@"$SERVER_IP" "sudo gitlab-ctl reconfigure && sudo gitlab-ctl start" # 4. Verify deployment success - ssh gitlab-deploy@"$SERVER_IP" "sudo gitlab-ctl status" only: - main # Only trigger deployment when pushing to your main branch
If your repo is on GitHub, create .github/workflows/deploy.yml instead:
name: Deploy Forked GitLab CE to Ubuntu Server on: push: branches: [ main ] jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: ruby-version: '2.7' bundler-cache: true # Cache dependencies to speed up builds - name: Compile GitLab assets run: | sudo apt-get install -y build-essential libyaml-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev zlib1g-dev bundle exec rake gitlab:assets:compile RAILS_ENV=production - name: Deploy to Ubuntu server uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.SERVER_IP }} username: gitlab-deploy key: ${{ secrets.SSH_PRIVATE_KEY }} script: | sudo gitlab-ctl stop rsync -avz --exclude='.git' $GITHUB_WORKSPACE/public/ /opt/gitlab/embedded/service/gitlab-rails/public/ rsync -avz --exclude='.git' $GITHUB_WORKSPACE/app/ /opt/gitlab/embedded/service/gitlab-rails/app/ sudo gitlab-ctl reconfigure && sudo gitlab-ctl start sudo gitlab-ctl status
- Backup First: Always run
sudo gitlab-backup createon the server before deploying—this saves you if something breaks during sync. - Version Matching: Ensure the GitLab CE version in your fork matches the one installed on the server. Mismatched versions will cause weird bugs.
- Incremental Sync: Use
rsyncinstead of full copies—it only transfers changed files, cutting down deployment time. - Monitor Logs: After deployment, check GitLab logs with
sudo gitlab-ctl tailto catch any errors early. - Rollback Plan: Keep a copy of the previous working version on the server, or use the backup to roll back if deployment fails.
内容的提问来源于stack exchange,提问作者Scott Ge

