GitLab CI
GitLab CI/CD deployment
Run Docker instance
GitLab CI use Docker container to run jobs. To deploy on your server, you have to create a SSH key for GitLab CI.
You can create a local Docker alpine:latest to test CI.
docker pull alpine:latestRun with interactive mode
docker run -it alpine:latest shInit SSH
command -v ssh-agent >/dev/null || ( apk add --update openssh )
eval $(ssh-agent -s)Create SSH key
ssh-keygen -t ed25519Now you can add the public key to your server ~/.ssh/authorized_keys file.
cat ~/.ssh/id_ed25519.pubAnd private key to GitLab CI/CD Variables.
cat ~/.ssh/id_ed25519Test CI
Set variables:
SSH_IP=xxx.xxx.xxx.xxx
SSH_USER=linux_user_on_your_server
SSH_PRIVATE_KEY=`cat ~/.ssh/id_ed25519`Setup SSH
echo -e "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add - > /dev/null
mkdir -p ~/.ssh
chmod 700 ~/.ssh
ssh-keyscan 168.119.97.151 >> ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hostsTest SSH
ssh -${SSH_USER}@${SSH_IP}If it works, you can exit.
exitVariables
You can find variables in Settings > CI/CD > Variables.
INFO
You have three ways to set variables:
- Local variables are available only for this pipeline.
- Project variables are available only for this project.
- Group variables are available for all projects in this group.
Local variables / CI/CD variables
You can define local variables in your .gitlab-ci.yml file.
variables:
NODE_VERSION: 18.17.0And use it in your jobs.
script:
- echo $NODE_VERSIONAnd some predefined variables are available in GitLab CI/CD, like $CI_PROJECT_NAME. You can find a list here: https://docs.gitlab.com/ee/ci/variables/.
Project variables
You can define project variables in Settings > CI/CD > Variables.
In https://gitlab.com/kiwilan/memorandum, variables are available into https://gitlab.com/kiwilan/memorandum/-/settings/ci_cd.
Group variables
You can define group variables in Settings > CI/CD > Variables. Group variables are available for all projects in this group.
In https://gitlab.com/kiwilan, variables are available into https://gitlab.com/groups/kiwilan/-/settings/ci_cd.

Pipeline
Now you can create a .gitlab-ci.yml file at the root of your project.
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/
stages:
- deploy
variables:
NODE_VERSION: 18.17.0
deploy-job:
stage: deploy
image: alpine:latest
before_script:
- "command -v ssh-agent >/dev/null || ( apk add --update openssh )"
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan $SSH_IP >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
script:
- ssh $SSH_USER@$SSH_IP "
. ~/.zshrc &&
cd ~/www/$CI_PROJECT_NAME &&
git pull &&
~/.nvm/versions/node/v$NODE_VERSION/bin/pnpm i &&
~/.nvm/versions/node/v$NODE_VERSION/bin/pnpm generate &&
notifier '$CI_PROJECT_TITLE deployed'"
only:
- main$CI_PROJECT_NAMEand$CI_PROJECT_TITLEare predefined variables from GitLab CI.$NODE_VERSIONis a custom variable for this project only, defined invariables:section.$SSH_PRIVATE_KEY,$SSH_IPand$SSH_USERare custom variables, defined inSettings > CI/CD > Variables.. ~/.zshrcallow to load zsh config file.~/.nvm/versions/node/v$NODE_VERSION/bin/pnpmis the path to pnpm binary.notifieris a custom script withgoto send notification to Discord, you can find it here notifier
Now you can install your own runner on your server with this guide.