Checking out Git repositories

Using the Codefresh native GIT integration

Codefresh has native support for GIT repositories and Git triggers. First you need to setup a GIT integration (Your administrator might also have done this for you already).

GIT integrations

GIT integrations

You can add new integration for any cloud provider or even on-premise ones. By default you will also have a provider setup if you used one for Codefresh signup (Github, Gitlab or Bitbucket).

For each Git Integration, make sure that you note down its name, as you will use in your pipeline inside a git-clone step.

Cloning a specific repository

The simplest way to clone using your git provider is by specifying the exact repository details. Here is a pipeline that clones a git repository and creates a Docker image from a Dockerfile:

codefresh.yml

version: '1.0'
steps:
  main_clone:
    title: 'Cloning main repository...'
    type: git-clone
    repo: kostis-codefresh/example_nodejs_postgres
    revision: master
    git: github-1
  myDockerImage:
    title: 'Building My Docker Image'
    type: build
    dockerfile: Dockerfile
    image_name: my-app-image
    tag: from-master-branch

This syntax is very simple to use, but has the disadvantage that ties your pipeline to a specific repository. This makes the pipeline impossible to re-use among different micro-services (that are built in a similar manner).

The proper way to use git-clone steps is to make them trigger specific. Instead of hard-coding the git repository that is checked-out, it is best to checkout the same one that triggered the pipeline. This is what you want in most scenarios anyway.

This can be achieved by using Codefresh variables to refer to the trigger. Here is the same pipeline as before, written in a generic way:

codefresh.yml

version: '1.0'
steps:
  main_clone:
    title: 'Cloning main repository...'
    type: git-clone
    repo: '${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}'
    revision: '${{CF_REVISION}}'
    git: github-1
  myDockerImage:
    title: 'Building My Docker Image'
    type: build
    dockerfile: Dockerfile
    image_name: my-app-image
    tag: ${{CF_BRANCH_TAG_NORMALIZED}}

The big advantage of this pipeline is that it can be reused for ALL your projects that follow the same pattern of having a Dockerfile in the root of the git repository.

Reusing a pipeline between microservices

Reusing a pipeline between microservices

Thus you can have a single pipeline and when you want to enable it for a new micro-service you can simply add a new git trigger for it.

You still run the pipeline manually if you wish. In this case you will be asked which trigger you want to “simulate” so that the variable pipelines are correctly replaced by Codefresh.

Simulating a GIT trigger

Simulating a GIT trigger

This is the recommended way of creating re-usable pipelines in Codefresh.

Working inside the cloned directory

Normally each pipeline step in Codefresh can be named as you want. Specifically for the git-clone step however the name main_clone is special.

If you name your clone step as main_clone the Codefresh will automatically change the working directory for all the next pipelines steps, to be the same as the project that was just checked out.

Checkout structure

Checkout structure

This is probably what you want anyway, so make sure that you name your git-clone steps as main_clone. If you use any other name, then the working folder will be the parent of the checked-out project which is the shared Codefresh volume at /codefresh/volume.

Cloning multiple repositories

You can use as many clone steps as you want and at any position in the pipeline. They don’t have to be the first step.

Here is an example where two repositories are checked out and two docker images are then built.

codefresh.yml

version: '1.0'
steps:
  checkoutApp1:
    title: 'Cloning first repository...'
    type: git-clone
    repo: kostis-codefresh/example_nodejs_postgres
    revision: experiment1
    git: github-1
  myFirstDockerImage:
    title: 'BuildingDockerImage'
    type: build
    dockerfile: Dockerfile
    image_name: my-nodejs-image
    tag: from-develop-branch
    working_directory: './example_nodejs_postgres'
  checkoutApp2:
    title: 'Cloning second repository...'
    type: git-clone
    repo: kostis-codefresh/trivial-go-web
    revision: master
    git: github-1
  mySecondDockerImage:
    title: 'BuildingDockerImage'
    type: build
    dockerfile: Dockerfile
    working_directory: './trivial-go-web'
    image_name: my-app-image
    tag: from-master-branch         

Notice that in this case the git-clone steps are not named main_clone and therefore we specify exactly what is the working directory for each one.