Skip to content

Instantly share code, notes, and snippets.

@Albinzr
Forked from charleslouis/.gitlab-ci-npm.yml
Created January 4, 2019 20:39
Show Gist options
  • Save Albinzr/7baf481c796887c80854b4d5e207a017 to your computer and use it in GitHub Desktop.
Save Albinzr/7baf481c796887c80854b4d5e207a017 to your computer and use it in GitHub Desktop.
deploy-gitlab-2-linode.md , gitlab, deploy, PROJ_OWNER , DEPLOY_DIR, .gitlab-ci.yml
deploy-prod:
variables:
GIT_STRATEGY: none
stage: deploy
only:
- master
script:
- sudo -u $PROJ_OWNER git --git-dir=$DEPLOY_DIR/.git --work-tree=$DEPLOY_DIR fetch origin $CI_COMMIT_REF_NAME
- sudo -u $PROJ_OWNER git --git-dir=$DEPLOY_DIR/.git --work-tree=$DEPLOY_DIR reset --hard FETCH_HEAD
- sudo -H -u $PROJ_OWNER chmod u+x $DEPLOY_DIR/nodescripts.sh
- sudo -H -u $PROJ_OWNER $DEPLOY_DIR/nodescripts.sh
stages:
- deploy
deploy_dev:
stage: deploy
script:
- echo "Deploy to staging server"
environment:
name: staging
url: $STAGING_URL
only:
- master
script:
- sudo -u $PROJ_OWNER git --git-dir=$DEPLOY_DIR_DEV/.git --work-tree=$DEPLOY_DIR_DEV fetch origin $CI_COMMIT_REF_NAME
- sudo -u $PROJ_OWNER git --git-dir=$DEPLOY_DIR_DEV/.git --work-tree=$DEPLOY_DIR_DEV reset --hard FETCH_HEAD
deploy_prod:
stage: deploy
script:
- echo "Deploy to production server"
environment:
name: production
url: $PROD_URL
only:
- master
when: manual
script:
- sudo -u $PROJ_OWNER git --git-dir=$DEPLOY_DIR/.git --work-tree=$DEPLOY_DIR fetch origin $CI_COMMIT_REF_NAME
- sudo -u $PROJ_OWNER git --git-dir=$DEPLOY_DIR/.git --work-tree=$DEPLOY_DIR reset --hard FETCH_HEAD

In your server

This is considering using linode with a froxlor install - so it user / user repo / domain are already created) :

edit /etc/sudoers

*vim /etc/sudoers

  • add a line gitlab-runner ALL=(myuser:group) NOPASSWD:ALL
  • change myuser:group accordingly
  • then :wq!

go to linode user folder

  • cd /var/customers/webs/myuser
  • mkdir .ssh if not done
  • chown -R myuser:mysusergroup .ssh
  • sudo -u myuser ssh-keygen -t rsa
  • Store it in /var/customers/webs/myuser/.ssh
  • ! use NO PASSWORD - as gitlab deploy won't be able to write it :)
  • Copy public key ---> cat .ssh/id_rsa.pub , highlight and copy (unless you have xclip and X11 forwarding is enabled)

Go to gitlab > Settings-> CI/CD -> Secret keys

go to theme/repo folder

  • cd /var/customers/webs/myuser/dev.myuser.servername.io/wp-content/themes/
  • sudo -u myuser git clone gitlabRepo@gitlab
  • ! use ssh - not https
  • ! make sure git clone doesn't prompt for a password

In gitlab :

copy public key

In gitlab > Project > Settings->Repositories->Deploy Keys

  • Copy public key from server/xyz/myuser/.ssh/id_rsa.pub
  • Paste it in a new Deploy key - use myuser as user name

In Project > Settings > CI/CD > Runners

  • disable SharedRunner
  • enable the Specific Runner

In Settings-> CI/CD -> Secret Variables

  • Add a variable DEPLOY_DIR (ex: /var/customers/webs/user/usersite-prod.com/wp-content/themes/usertheme - or any theme/repo folder)
  • Add a variable DEPLOY_DIR_DEV (ex: /var/customers/webs/user/usersite-dev.com/wp-content/themes/usertheme - or any theme/repo folder)
  • Add variable PROJ_OWNER (ex: user - same as ssh user / project linode user)
  • Add variable PROD_URL and STAGING_URL (https://mysite.com)

In Project > click CI Configuration

Add .gitlab-ci.yml with the following : Click edit to get the clean code of this gist OR BETTER : use one of the joined file with extended .gitlab-ci.yml depending on your needs (one gives staging and prod, the other provides a build with npm and yarn). Adjust accoprdingly. If you use gitlab-ci-npm.yml, you need to add also the file nodescript.sh.

  • Then commit

In local

  • change something (ex: README.md, or add a file)
  • commit
  • push to master

In Gitlab

  • Check if job is in gitlab > Pipelines > Jobs
  • If not, it is possible to run it again or check what is going on

ISSUES

IF sudo: command not found

DO THIS

Enable runners ;)

IF

DONT DO THIS

  • run getent passwd myuser
  • it should issue something like myuser:x:10009:10009:Froxlor Customer:/var/customers/webs/myuser/:/bin/false
  • then vim /etc/passwd
  • paste myuser:x:10009:10009:Froxlor Customer:/var/customers/webs/myuser/:/bin/false
  • you might also sudo service nginx restart if something goes wrong

DO THAT

For Wordpress

If you use Sage

  • edit vim ./resources/assets/config.json
  • change devUrl and publicPath
  • create nodescript.sh (or nodescript-dev.sh and nodescript-prod.sh if needed) / these scripts will be ran with CI based on gitlab-ci.yml

Create DB

Connect DB

Install theme

#!/bin/bash
cd /var/customers/webs/user-folder/site.bluecraft.io
yarn install
# npm install
# gulp build
# npm build
# npm run-script build
yarn build
cp -r src/app dist/
npm uninstall
rm -r node_modules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment