skip to Main Content

I am currently experimenting GitLab CI/CD on a laravel / docker compose project to enhance my deployment and integration experience, but i can’t manage to find a right way to do it. Since we are a very small team and we all work with normalized docker compose environments for our project, we disregard the creation of a global developement environment to only use local ones. My needs are a pipeline that :

  • runs tests when commits are made on any branch excepted main
  • runs tests and deployment when main is getting changes (only by merge requests)

I would like to achieve an architecture similar to this (without the deploy review part) : GitLabCI/CD schema demo

I created a gitlab runner instance in a local linux server (that has docker and composer installed) to run the pipeline. Everything goes well when i run the pipeline for the main branch : it runs tests and deploys the containers with the docker compose file in the build directory generated by gitlab runner, but when i execute the pipeline for any other branch, it runs the tests and still sends the code to the server anyway so it gets non production code that its not supposed to.

Here’s my gitlab-ci.yml file :

stages:
  - build
  - test
  - deploy_production

cache:
  paths:
    - vendor/

build_composer:
  stage: build
  script:
    - composer install -n
  artifacts:
    paths:
      - ./vendor
  only:
    - main

pest_test:
  stage: test
  script:
    - ./vendor/bin/pest

larastan_test:
  stage: test
  script:
    - ./vendor/bin/phpstan analyse --memory-limit=2G --no-progress

deploy_production:
    stage: deploy_production
    script:
      - docker composee up -d
    rules:
      - if: $CI_COMMIT_BRANCH == "main"
        when: on_success

I don’t know if i’m supposed to create another runner on a dev environment and make it run the test stage for every commits thanks to the tags, or if i should use a runner in a docker environment to send code in SSH instead of using build directories generated by gitlab runner.

Is it even relevant to run test on every commit when it is done when some code is merged in main ?

Thanks in advance for your help.

2

Answers


  1. It looks like you are trying to install the application directly onto the GitLab runner. When a pipeline is triggered, the runner downloads the code from the triggering branch/merge request, based on the Git Strategy, so that it can be built and tested. The best practice here would be to install the application on a different server, so the source code is not clobbered by the next running pipeline.

    In general, it is best to build and test code from feature branches, so that the main branch remains stable. A final round of tests can be ran using a Merge Request Pipeline, to ensure that main receives working code.

    Login or Signup to reply.
  2. you need to define rules for your app to be pushed to server in case when the merge request is merged

    rules:
    - if: $CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push"
      when: always
    - when: never
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search