1

I'm currently working on a little Angular Web project. And I found this great tool named Gitlab CI.

I read the docs and setup a node docker to build the webapp. Then I want to upload the builded app with ftp to my server. And this is where my trouble starts.

First here ist my gitlab-ci.yml

image: node:7.5.0
cache:
  key: "$CI_BUILD_REF_NAME"
  untracked: true
  paths:
    - node_modules/
    - dist/

stages:
  - build
# - test
  - deploy
  - cleanup
# - deployProd


runBuild:
  before_script:
   - npm install -g angular-cli
   - npm install
  stage: build
  script:
    - ng build --target=production --environment=test
  except:
    - tags

runProdBuild:
  before_script:
    - npm install -g angular-cli
    - npm install
  stage: build
  script:
    - ng build --target=production --environment=prod
  only:
    - tags



runDeployTest:
  before_script:
    - apt-get install ftp
  variables:
    DATABASE: ""
    URL: "http://test.domain.de"
  stage: deploy
  environment:
      name: Entwicklungssystem
      url: https://test.domain.de
  artifacts:
    name: "$CI_BUILD_NAME/$CI_BUILD_REF_NAME"
    paths:
    - dist/
    expire_in: 2d
  except:
      - tags
  script:
    - echo '<?php ini_set("max_execution_time", 300);  function rrmdir($dir) {    if (is_dir($dir))    {        $objects = scandir($dir);        foreach ($objects as $object)       {            if ($object != "." && $object != "..")            {                if (is_dir($dir."/".$object))                {                    rrmdir($dir."/".$object);      }        else          {  echo "unlink :".$dir."/".$object;      unlink($dir."/".$object);     } }     }     rmdir($dir);     } } rrmdir(__DIR__."."); ?>' > delete.php
    - lftp -d -c "set ftp:ssl-allow no; open -u $ftp_user,$ftp_password $ftp_server; cd $ftp_path; put -O . delete.php"
    - wget "$URL/delete.php"
    - cd ./dist
    - zip -r install.zip .
    - lftp -d -c "set ftp:ssl-allow no; open -u $ftp_user,$ftp_password $ftp_server; cd $ftp_path; put -O . install.zip"
    - echo "<?php   \$dateiname = __DIR__.'/install.zip';   \$ofolder = str_replace('/public','',__DIR__);  exec('unzip '.\$dateiname.' -d '.\$ofolder.' 2>&1', \$out);   print(implode('<br>', \$out)); unlink(\$dateiname); unlink('entpacker.php'); unlink(__DIR__.'/../delete.php'); unlink(__DIR__.'/../delete.php.1'); ?>" > entpacker.php
    - lftp -d -c "set ftp:ssl-allow no; open -u $ftp_user,$ftp_password $ftp_server; cd $ftp_path; put -O . entpacker.php"
    # Install
    - wget $URL/entpacker.php

runDeployProd:
  before_script:
    - apt-get install ftp
  variables:
    DATABASE: ""
    URL: "http://test.domain.de"
  stage: deploy
  environment:
    name: Produktivsystem
    url: https://prod.domain.de
  artifacts:
    name: "$CI_BUILD_NAME/$CI_BUILD_REF_NAME"
    paths:
      - dist/
    expire_in: 2d
  script:
    - echo '<?php ini_set("max_execution_time", 300);  function rrmdir($dir) {    if (is_dir($dir))    {        $objects = scandir($dir);        foreach ($objects as $object)       {            if ($object != "." && $object != "..")            {                if (is_dir($dir."/".$object))                {                    rrmdir($dir."/".$object);      }        else          {  echo "unlink :".$dir."/".$object;      unlink($dir."/".$object);     } }     }     rmdir($dir);     } } rrmdir(__DIR__."."); ?>' > delete.php
    - lftp -d -c "set ftp:ssl-allow no; open -u $ftp_user,$ftp_password $ftp_server; cd $ftp_path; put -O . delete.php"
    - wget "$URL/delete.php"
    - cd ./dist
    - zip -r install.zip .
    - lftp -d -c "set ftp:ssl-allow no; open -u $ftp_user,$ftp_password $ftp_server; cd $ftp_path; put -O . install.zip"
    - echo "<?php   \$dateiname = __DIR__.'/install.zip';   \$ofolder = str_replace('/public','',__DIR__);  exec('unzip '.\$dateiname.' -d '.\$ofolder.' 2>&1', \$out);   print(implode('<br>', \$out)); unlink(\$dateiname); unlink('entpacker.php'); unlink(__DIR__.'/../delete.php'); unlink(__DIR__.'/../delete.php.1'); ?>" > entpacker.php
    - lftp -d -c "set ftp:ssl-allow no; open -u $ftp_user,$ftp_password $ftp_server; cd $ftp_path; put -O . entpacker.php"
    # Install
    - wget $URL/entpacker.php
  only:
      - tags


cleanup:
  stage: cleanup
  script:
  - rm -rf ./dist
  - rm -rf ./node_modules
  when: manual

So it works fine until I want to install ftp to the docker image.

My question is now: Is it possible to install ftp to the image?

Or is there a other way to handle things like this? I can't use ssh because there is no ssh access to the webspace.

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
dominic
  • 385
  • 1
  • 6
  • 23
  • 1
    I'd suggest you create your own image where you add all you need (`npm install -g angular-cli`, `apt-get` install of other programs, etc.). It will speed up the jobs and make them more resilient to failures or unexpected results. `rsync` would be a better option than FTP transfer. However, even that would have one flaw: **what if something goes wrong in the middle of the transfer?** On a production server it would be a problem, on a dev server it would be a nuisance. Explore other deployment strategies. – tmt Feb 09 '17 at 10:55
  • as I sad I didn't have ssh access to the webspace. So I think rsync won't work. The webpage isn't this critical that it has to run and I deploy to productive only at manual tags. So I could check for this site after deployment. I tried to create a own Dockerimage but I don't get it to work. – dominic Feb 09 '17 at 11:36
  • My bad. I missed the part about lack of SSH. Re-consider the rest though. – tmt Feb 09 '17 at 11:42
  • i was wondering if using a docker node image with lftp in would solve this problem. like this one https://github.com/pionl/docker-node-with-lftp. not sure if it is up to date with current node though – MitchBroadhead Mar 07 '20 at 11:01

2 Answers2

2

I got a solution. As suggested I tried to create a own docker image. There I noticed that I can't install lftp too. So at creating an docker image you have to run apt-get update first.

So I tried this inside my script, and it worked.

So you need to run apt-get update first, then install any package you want.

dominic
  • 385
  • 1
  • 6
  • 23
1

Use lftp instead of ftp

runDeployProd:
  before_script:
    - apt-get install lftp

https://forum.gitlab.com/t/deploy-via-ftp-via-ci/2631/2

Kohei Mikami
  • 2,850
  • 24
  • 21
  • This gives the following error '$ apt-get install lftp Reading package lists... Building dependency tree... Reading state information... E: Unable to locate package lftp'. So maybe the node image can't install this ? – dominic Feb 09 '17 at 10:40
  • 1
    hmm... I'm not sure the script is running on the same docker container. If it doesn't work, I suggest trying to create a new docker image with `ftp` or `lftp`. Use that image instead of `node:7.5.0`. – Kohei Mikami Feb 09 '17 at 14:17