GitLab CI and CD - rishavry/WorksPresentation GitHub Wiki

  • CI/CD is a continuous method of software development, where you continuously build, test, deploy, and monitor iterative code changes. GitLab is a popular tool for this.

  • In my Megagram project, I would use GitLab for the smooth deployment of all my frontends and backends; specifically, all my frontends and backends will have to pass tests, and if they do, then the production-code gets updated in a timely fashion.

  • Here is what I would do to use GitLab for CI/CD- I would add a file called .gitlab-ci.yml(which will be executed by the 'GitLab runner' anytime a git commit occurs in the remote-repo) with content similar to the following-

    variables:
        IMAGE_NAME: docker-username/docker-registry #this will be the name of the image you would 
        IMAGE_TAG: insert-image-tag-here
    
    
    stages: #the stages will be executed in order from top to bottom, and if any stage fails, the remaining stages
        # will not take place; furthermore, the git push will fail if any stage fails, and all jobs that are
        # in the same stage will be executed concurrently
        - test  #testing code of current-state of repository (CI)
        - build #building docker-image of current-state of repository
        - deploy #deploying docker-image of current-state of repository (CD)
    
    
    run_tests:  #job 1, which will be taking place in a Docker container
        image: insert-image-here:insert-tag-here 
        before_script:
            - apt-get update && apt-get install make
        script:
            - make test
    
    
    build_image: #job 2, which will be taking place in another Docker container
        stage: build
        image: docker:28.2.2
        services:
            - docker:28.2.2-dind
        variables:
            DOCKER_TLS_CERTDIR = "/certs"
        before_script:
            - docker login -u $REGISTRY_USER -p $REGISTRY_PASS #$REGISTRY_USER and $REGISTRY_PASS are secure secret variables whose values were declared in the GitLab website
        script:
            - docker build -t $IMAGE_NAME:$IMAGE_TAG . #the repository 
            - docker push $IMAGE_NAME:$IMAGE_TAG   
    
    
    deploy_image: #job 3, which will be taking place in another Docker container
        stage: deploy
        script:
            - #ssh into the master-node of the Kubernetes-cluster, delete the current deployment of the docker-image that represented the state of the 
            # repository in the previous commit. then deploy the newly-pushed docker-image that reflects the current state of the repository
⚠️ **GitHub.com Fallback** ⚠️