Openshift: Using BuildConfig with a private git repo with a Dockerfile

Let's look at how to use BuildConfig with a private git repo with a Dockerfile. I'll be using gitlab private repo as an example.

Let's say I have a DeploymentConfig (see below) that's pulling an from dockerhub, but I want to use an image built from my repo.

repo url: https://my-gitlab-repo.com/jefferyb/name-of-the-repo

Basic-auth secret

First, we need to create a basic-auth secret for your private git repo. The recommended way is to use Tokens.

To create deploy tokens, go to your git project, in my case, https://my-gitlab-repo.com/jefferyb/name-of-the-repo, and then go to settings -> repository -> Deploy Tokens -> Name: openshift and check the read_repository box under Scopes and then click the green button Create deploy token and you should get something like:

Your New Deploy Token

gitlab+deploy-token-3
Use this username as a login.

vM_BsuuCMR3uefxnAkzS
Use this token as a password. Make sure you save it - you won't be able to access it again.

On your terminal, change to or create your project:

# Create a new project
$ oc new-project <name-of-project>

# Or change to your project
$ oc project <name-of-project>

# Create a secret from your New Deploy Token so that it can access your private repo
$ oc create secret generic gitlab-auth --type=kubernetes.io/basic-auth --from-literal=username='gitlab+deploy-token-3' --from-literal=password='vM_BsuuCMR3uefxnAkzS'

The DeploymentConfig

Let's use this for our example

- kind: DeploymentConfig
  apiVersion: v1
  metadata:
    name: ${APPLICATION_NAME}
    labels:
      app: ${APPLICATION_NAME}
  spec:
    replicas: 1
    template:
      metadata:
        labels:
          app: ${APPLICATION_NAME}
          deploymentconfig: ${APPLICATION_NAME}
      spec:
        containers:
          - name: ${APPLICATION_NAME}
            image: image-name:latest
    triggers:
    - type: ConfigChange

ImageStream

We'll start with ImageStream

- kind: ImageStream
  apiVersion: v1
  metadata:
    name: ${APPLICATION_NAME}
    labels:
      app: ${APPLICATION_NAME}
    annotations:
      description: ImageStream for ${APPLICATION_NAME}
  spec:
    lookupPolicy:
      local: true
    tags:
      - name: latest
        from:
          kind: ImageStreamTag
          name: ${APPLICATION_NAME}:latest

BuildConfig

Next, create our BuildConfig

- apiVersion: v1
  kind: BuildConfig
  metadata:
    name: ${APPLICATION_NAME}
    labels:
      app: ${APPLICATION_NAME}
  spec:
    successfulBuildsHistoryLimit: 3
    failedBuildsHistoryLimit: 3
    nodeSelector: null
    triggers:
      - type: ConfigChange
      - type: ImageChange
        imageChange: {}
    source:
      type: Git
      git:
        # Make sure you have the .git at the end
        uri: https://my-gitlab-repo.com/jefferyb/name-of-the-repo.git
      # Your loggin auth credentials
      sourceSecret:
        name: gitlab-auth
    strategy:
      type: Docker
      dockerStrategy:
        dockerfilePath: Dockerfile
    output:
      to:
        kind: ImageStreamTag
        name: ${APPLICATION_NAME}:latest
    postCommit: {}
    resources: {}
    runPolicy: Serial

Updated DeploymentConfig

Now, let's update our DeploymentConfig

- kind: DeploymentConfig
  apiVersion: v1
  metadata:
    name: ${APPLICATION_NAME}
    labels:
      app: ${APPLICATION_NAME}
  spec:
    replicas: 1
    template:
      metadata:
        labels:
          app: ${APPLICATION_NAME}
          deploymentconfig: ${APPLICATION_NAME}
      spec:
        containers:
          - name: ${APPLICATION_NAME}
            # Update image name
            image: ${APPLICATION_NAME}:latest
    triggers:
      - type: ConfigChange
      # add the ImageChange trigger
      - type: ImageChange
        imageChangeParams:
          automatic: true
          containerNames:
            - ${APPLICATION_NAME}
          from:
            kind: ImageStreamTag
            name: ${APPLICATION_NAME}:latest

All together

Here's what it would look like all together (You can save it as buildconf-example.yaml)

apiVersion: v1
kind: Template
parameters:
  - name: APPLICATION_NAME
    value: buildconf-example
  - name: GIT_URI
    # Make sure you have the .git at the end
    value: "https://my-gitlab-repo.com/jefferyb/name-of-the-repo.git"

objects:
- kind: ImageStream
  apiVersion: v1
  metadata:
    name: ${APPLICATION_NAME}
    labels:
      app: ${APPLICATION_NAME}
    annotations:
      description: ImageStream for ${APPLICATION_NAME}
  spec:
    lookupPolicy:
      local: true
    tags:
      - name: latest
        from:
          kind: ImageStreamTag
          name: ${APPLICATION_NAME}:latest

- apiVersion: v1
  kind: BuildConfig
  metadata:
    name: ${APPLICATION_NAME}
    labels:
      app: ${APPLICATION_NAME}
  spec:
    successfulBuildsHistoryLimit: 3
    failedBuildsHistoryLimit: 3
    nodeSelector: null
    triggers:
      - type: ConfigChange
      - type: ImageChange
        imageChange: {}
    source:
      type: Git
      git:
        # Make sure you have the .git at the end
        uri: ${GIT_URI}
      # Your loggin auth credentials
      sourceSecret:
        name: gitlab-auth
    strategy:
      type: Docker
      dockerStrategy:
        dockerfilePath: Dockerfile
    output:
      to:
        kind: ImageStreamTag
        name: ${APPLICATION_NAME}:latest
    postCommit: {}
    resources: {}
    runPolicy: Serial

- kind: DeploymentConfig
  apiVersion: v1
  metadata:
    name: ${APPLICATION_NAME}
    labels:
      app: ${APPLICATION_NAME}
  spec:
    replicas: 1
    template:
      metadata:
        labels:
          app: ${APPLICATION_NAME}
          deploymentconfig: ${APPLICATION_NAME}
      spec:
        containers:
          - name: ${APPLICATION_NAME}
            # Update image name
            image: ${APPLICATION_NAME}:latest
    triggers:
      - type: ConfigChange
      # add the ImageChange trigger
      - type: ImageChange
        imageChangeParams:
          automatic: true
          containerNames:
            - ${APPLICATION_NAME}
          from:
            kind: ImageStreamTag
            name: ${APPLICATION_NAME}:latest

You should be able to run with:

$ oc process -f buildconf-example.yaml | oc apply -f -

Or you can use -p to provide the GIT_URI param from the command line, like:

$ oc process -f buildconf-example.yaml -p GIT_URI='https://my-gitlab-repo.com/jefferyb/my-other-repo.git' | oc apply -f -

imagestream.image.openshift.io "buildconf-example" created
buildconfig.build.openshift.io "buildconf-example" created
deploymentconfig.apps.openshift.io "buildconf-example" created

and you should have your app deployed...

Start new build/update your app

To start a new build or update your app after committing to your repo, you can run:

$ oc start-build ${APPLICATION_NAME} --follow in our case, it would be

$ oc start-build buildconf-example --follow

This will start a new build, push the new image to our ImageStream, which will trigger a new deployment of our application.

More info

For more info, please check https://docs.okd.io/latest/dev_guide/builds/index.html

Comments:

For comments, please visit https://plus.google.com/u/0/+JefferyBagirimvano/posts/4CMq3k2jTK1