Use script in GitLab

Hi everyone !

I currently work in a web agency that uses a personal framework.
The latter has several files that are reused for each new site that we develop.
My question is the following :
Can we have a directory on GitLab which would be “parent” of several other directories?

Here’s what I’d like (if possible):
I want to create a new site, I clone the “framework” directory with the ssh command.
After this clone, a script would make me composer install and npm install.

I hope I was clear, thank you in advance for your answers.

PS : We work with php

Hi,

When you create a new project, you can use ‘import project’ option. And you specify your project template and i think that is ok.

1 Like

Hi,
Thank you for your reply.

I noted well to import a project, but how to run npm install and composer install during an SSH clone?

the first part was to clone your repo, ok.

The second is to build your application. To build your project, you can create a .gitlab-ci.yml file and create a job. Have you already create a GitLab CI pipeline ?

1 Like

Hi,

No I never did that.
I looked at a lot of things about pipelines on internet but I couldn’t find what I want, we want to start working with gitlab so we start from the beginning…

To generate pipeline, you can start to create a .gitlab-ci.yml.

In your case, you tell us you want run one npm command. So you can have one stage, which contains one job based on the node image, and put your commnd in the script tag.

You can have documentation on GitLab CI/CD Examples | GitLab

1 Like

I tested creating the .gitlab-ci.yaml file

image: tetraweb/php
hidden:
key:
files:
- package.json
paths:
- node_modules/

build:
stage:build
script:
- npm install

When I run the commit, the pipeline is in “passed” state but the node_modules folder with dependencies is not created…

I don’t have to do it right…

(You can use preformatted text to easier paste code :smile: )

You node modules directory was created, but not visible. If you need it, you can use artifacts. If you want some details I create a serie of Cheatsheets : GitLab Cheatsheet Series' Articles - DEV Community

I think this work :

image: tetraweb/php

build:
  stage: build
  script:
    - npm install
  artifacts:
    paths:
      - node_modules/

1 Like

I tried that indeed but suddenly I would like the artifact to be uploaded directly to the server…

Here is where I am currently (it’s progressing :yum:)

image: tetraweb/php

before_script:
  - apt-get update
  - apt-get install zip unzip
  - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  - php composer-setup.php
  - php -r "unlink('composer-setup.php');"
  - php composer.phar install
  - npm install
  - npm run deploy
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - mkdir -p ~/.ssh
  - eval $(ssh-agent -s)
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

stage_deploy:
  artifacts:
    paths:
      - build/
  script:
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - ssh -p22 server_user@server_host "mkdir httpdocs/_tmp"
    - scp -P22 -r build/* server_user@server_host:httpdocs/_tmp
    - ssh -p22 server_user@server_host "mv httpdocs/live httpdocs/_old && mv httpdocs/_tmp httpdocs/live"
    - ssh -p22 server_user@server_host "rm -rf httpdocs/_old"

The pipeline runs fine until “ssh add” and gives me an error

$ ssh-add <(echo "$SSH_PRIVATE_KEY")
Enter passphrase for /dev/fd/63: 
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 1

I don’t understand why…
obviously I replaced server_user@server_host with my informations

PS: I just saw that you were from Nantes (me too) sorry for my English :grin:

mmmh i think that you can split your pipeline in more jobs :

  • a before_script job to install zip unzip. Is there any image which already make this ?
  • a deploy job to run you deploy command
  • a install job where you get the result of the previous job (with artifact) and execute your install. Do you know ansible? it’s easier to make this operation :slight_smile:

Oh great you come from Nantes too !

I started from the documentation you gave me to make this script so I used the tetraweb/php image which seems to meet what I want.

Infact what I don’t understand is how to push the node_modules folder, because with artifact I can download it via gitlab but it doesn’t appear on my server (local or prod)

No I don’t know ansible but i will find out about it.

(Yes I work in Carquefou :wink: ).

Thanks again for all your answers.

With artifact, you can use node_modules folder in your next jobs.

No worry for Ansible, with your script will be ok. Before your first ssh command, you can verify your repository build and node_modules that they are really presents.

For you script, have you declare SSH_PRIVATE_KEY in yours variables ? In General > CICD > Variables ?

Ok I understand for the artifact but I also need my node_modules library for the rest of my project that’s why I would like to upload it directly…

Yes I have configured the variable in gitLab

Have you try to execute this command line in a terminal ?
ssh-add <(echo "$SSH_PRIVATE_KEY")

For your node_modules, when you said you need this directory, is in your repository ? IS your project is public ? it will be easier to help you if I can accès to the project :smile:

You may utilise the ‘import project’ option when creating a new project. And you specify your project template, which I believe is acceptable.