Using custom Git commands

Manually clone Git repositories

NOTE
Manually running Git commands is an advanced technique. For most use cases you should use the native Git checkout offered by Codefresh.

For complex cloning, you can still use custom clone commands in a freestyle step. In this case, you lose the native Codefresh integration such as Git authentication and automatic workdir setup. Use custom clone commands only as a last resort.

Cloning with the Git executable

It is very easy to run custom Git commands in a freestyle step. Pass any parameters to the Git clone step as you would pass them on your local workstation.

version: '1.0'
steps:
  myCustomClone:
    title: Performing swallow clone
    image: alpine/git:latest
    commands:
      - rm -rf ruby-on-rails-sample-app
      - git clone --depth 1 https://github.com/codefresh-contrib/ruby-on-rails-sample-app.git
  PrintFileList:
    title: 'Listing files'
    image: alpine:latest
    working_directory: './ruby-on-rails-sample-app'
    commands:
      - 'ls -l'     

Notice the rm command before the clone step. This makes sure that every time the pipeline runs, the git clone step is implemented in an empty directory. Otherwise the git clone command will fail (Git will refuse to clone on an existing directory).

You can enter your own Git username/password or reuse the credentials from the Codefresh integration.

Manually running Git commands

Once you understand that you can manually run Git commands in Codefresh pipelines, it is easy to see that any Git workflow is possible. Here is an example where an application is packaged in a Docker container, after merging master to a specific branch.

version: '1.0'
steps:
  myCustomClone:
    title: Performing swallow clone
    image: alpine/git:latest
    commands:
      - rm -rf example_nodejs_postgres
      - git clone https://github.com/kostis-codefresh/example_nodejs_postgres
      - cd example_nodejs_postgres
      - git checkout experiment1
      - git merge master
      - git status
  myDockerImage:
    title: 'BuildingDockerImage'
    type: build
    dockerfile: Dockerfile
    working_directory: './example_nodejs_postgres'
    image_name: my-app-image
    tag: from-master-branch      

If there are any errors with the merge, the pipeline fails automatically. Codefresh automatically stops any pipeline that shows an error in a step.

Other forms of cloning

There is nothing special about running Git it in a freestyle step. In fact, you can check out code with any other command that you would run locally in your terminal.

Here is an example with Golang.

version: '1.0'
steps:
  myCustomClone:
    title: Download example
    image: golang:1.11-alpine
    commands:
      - apk add --no-cache git
      - go get github.com/golang/example/hello

If you run this pipeline you will see git used as part of the go get mechanism.

For more examples, such as using SSH keys and working with Git submodules, see the Git-clone step.

CI/CD pipeline examples
Native Git checkout
Native Git integration
Steps in pipelines