Skip to content

Instantly share code, notes, and snippets.

@devhammed
Last active February 1, 2024 17:35
Show Gist options
  • Save devhammed/0de6d79c1fbefeccfed8ab2362874a3e to your computer and use it in GitHub Desktop.
Save devhammed/0de6d79c1fbefeccfed8ab2362874a3e to your computer and use it in GitHub Desktop.
GitLab Deploys Pipeline for Laravel (with environments and rollback support)
stages:
- deploy
production-deploy:
stage: deploy
environment: production
only:
- /^v.*$/
script:
- which ssh-agent || (apt-get update -y && apt-get install openssh-client -y)
- eval "$(ssh-agent -s)"
- echo "$SSH_PRIVATE_KEY" | tr -d "\r" | ssh-add -
- ssh -o "StrictHostKeyChecking=no" "$SSH_USER@$SSH_HOST" "
cd '$PROJECT_PATH' &&
php8.2 artisan down &&
git fetch --all --tags --prune &&
git checkout '$CI_COMMIT_REF_NAME' &&
php8.2 composer.phar install --no-ansi --no-dev --no-interaction --no-plugins --no-progress --no-scripts --optimize-autoloader &&
php8.2 artisan optimize:clear &&
php8.2 artisan schedule:interrupt &&
php8.2 artisan schedule:clear-cache &&
php8.2 artisan migrate --force &&
php8.2 artisan route:cache &&
php8.2 artisan view:cache &&
php8.2 artisan config:cache &&
npm ci &&
npm run build &&
php8.2 artisan up"

Instructions

Start by creating .gitlab-ci.yml file and pasting the contents provided in it, you can customize the deployment commands as needed.

Generate SSH Key Pair

On the server, generate a new SSH key pair using the ssh-keygen command:

ssh-keygen -t id_ed25519

Copy the Public Key

After generating the SSH key pair, copy the contents of the public key (usually found in ~/.ssh/id_ed25519.pub). You can use the cat command to display the contents:

cat ~/.ssh/id_ed25519.pub

Add Public Key to authorized_keys

Now, you need to add the public key to the authorized_keys file in the .ssh directory of your user's home directory. If the file doesn't exist, create it.

nano ~/.ssh/authorized_keys

Paste the contents of the public key into this file and save it.

Set Correct Permissions

Ensure that the permissions for the .ssh directory and the authorized_keys file are set correctly:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Add Variables to GitLab

First obtain the private key contents using:

cat ~/.ssh/id_ed25519

then go to your GitLab project's settings and navigate to CI/CD > Variables.

Then click on "Add variable" button and set the key to SSH_PRIVATE_KEY and paste the contents of your SSH private key into the value field.

The variable value must end in a newline (LF character). To add a newline, press Enter or Return at the end of the last line of the SSH key before saving it in the CI/CD settings.

Now also add other required environment variables as needed e.g SSH_HOST, SSH_USER & PROJECT_PATH.

Setup Tag Protection

You must have at least the Maintainer role for the project.

This is very important if you selected "Protected" when creating the variables above else they won't be available for use.

  • In your project, Select Settings > Repository.
  • Expand Protected tags
  • Click on Add tag
  • Select Tag and type v* then select Create wildcard v* in the options
  • Select Allowed to create and choose the role level you want to permit deployments for.
  • Then click Protect

Add Deploy Key

You must have at least the Maintainer role for the project.

  • In your project, Select Settings > Repository.
  • Expand Deploy keys.
  • Click on Add new key.
  • Use the server IP as the Title
  • Use the public key contents you copied above as the Key.
  • Then click on Add Key button to save.

Deploying

To deploy a new version of your project to production or whatever environment you decided to setup, use the following tagging command:

git tag -a 'v1.0.3' -m 'Add KYC Verification Support'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment