# Hands-On - Some use cases with Gitlab CI/CD These exercises will make you realize some interesting use cases with Gitlab CI/CD (I hope). ## Creating your repo If you don't have a repo with a source code created, feel free to access https://labrei.dsce.fee.unicamp.br:6498/ninalg/gitlab-training/ You can clone this repository, or you can fork it in the gitlab web. ## Adding the .gitlab-ci.yml Using the gitlab Web IDE or the common way to interact with gitlab (git add, commit, push), create the .gitlab-ci.yml in your repository using the code below: ```yaml= default: image: "python:3.8" variables: GIT_SSL_NO_VERIFY: "true" stages: - build - test - deploy build: stage: build tags: - carlos script: - echo "this is building" - python3.8 setup.py develop unittest: tags: - carlos stage: test script: - python3.8 -m unittest discover -s app_labrei/tests/unit/ integration: tags: - alexandre stage: test script: - python3.8 setup.py develop - python3.8 -m unittest discover -s app_labrei/tests/integration/ linting: stage: test tags: - alexandre script: - pip install flake8 - flake8 --ignore E305 --exclude .git,__pycache__ --max-line-length=90 app_labrei/tests/integration/ dependencies: [] pages: when: always stage: deploy tags: - carlos script: - pip install sphinx sphinx-rtd-theme - cd docs/ && make html && cd - - mkdir .public - cp -R docs/build/html .public - mv .public public artifacts: expire_in: 1 day paths: - public only: - master ``` Check if the gitlab pipeline run ok. ## Adding the Dockerfile In the root dir of your repository, create a file name Dockerfile and fill it with the content below. Commit and push. ```dockerfile= FROM python:3.8 RUN mkdir /app/ COPY Dockerfile /app/ COPY README.md /app/ COPY setup.py /app/ RUN ls /app/ RUN mkdir /app/app_labrei/ RUN ls /app/app_labrei/ COPY ./app_labrei/* /app/app_labrei/ WORKDIR /app RUN python3.8 setup.py install ``` ## Adding the Dockerfile build job to CI/CD In the added .gitlab-ci.yml add the following code. It will trigger the execution of a job to run the construction of your docker image using the previously provided Dockerfile. ```yaml= build_image: stage: build tags: - davi image: docker:19.03.12 stage: build script: - echo "this is building docker" - docker build -t app_labrei:latest . ``` ## Pushing the image to a registry In the added .gitlab-ci.yml add the following code. It will trigger the execution of a job to build the docker image of your code, login in the configured docker registry of your choice and push the image to it. To login in docker registry, tag and push the image, you need to configure some variables in your repository. There are many predefined variables in gitlab CI/CD that you can use to tag your image. Check them at https://docs.gitlab.com/ee/ci/variables/predefined_variables.html. But mostly important, you need to configure extra variables in the CI/CD part of your repository. You must add variables for: 1. CI_REGISTRY_USERNAME: The username that you use to login in gitlab 2. CI_REGISTRY_PASSWD: The username password that you use to login in gitlab 3. CI_REGISTRY: The registry address, i.e., the place where the image is going to be stored. In our case, define this value as 10.0.1.16:5000 ```yaml= release: stage: deploy tags: - davi image: docker:19.03.12 variables: IMAGE_TAG: $CI_REGISTRY/$CI_REGISTRY_USERNAME/$CI_PROJECT_NAME:$CI_COMMIT_REF_SLUG script: - docker login -u $CI_REGISTRY_USERNAME -p $CI_REGISTRY_PASSWD $CI_REGISTRY - docker build -t $IMAGE_TAG . - docker push $IMAGE_TAG ``` ## Playing with the .gitlab-ci.yml https://docs.gitlab.com/ee/ci/merge_request_pipelines/ https://docs.gitlab.com/ee/ci/yaml/README.html#onlyexcept-basic https://docs.gitlab.com/ee/ci/yaml/README.html#when # Final Remarks ## How to change the current folder to a new repository ### Option 1 ```bash= git remote set-url origin new-repository-url git remove -v git push -u origin master ``` ### Option 2 Clone the new repository in another folder, copy the content of the old folder to it, then: ```bash= git add -A git commit -a -m "initial commit" git push ```