Cloud-builders: Branch name containing slash

Created on 17 Sep 2018  路  5Comments  路  Source: GoogleCloudPlatform/cloud-builders

Hi,

I am using git branch naming patterns such as:

bugfix/fix-bug-whatever
feature/implement-whatever

I thought this is/was quite widespread, as it allows you to easily organize branches.

I setup cloud build triggers to automatically build Docker images upon repo changes. The images that are being built are named "gcr.io/PROJECT_ID/$REPO_NAME:$BRANCH_NAME"

This works fine for branches such as "master" or "develop", however fails for branches which contain a forward slash, such as bugfix/fix-bug-whatever.

invalid build: invalid image name "gcr.io/$PROJECT_ID/$REPO_NAME:$BRANCH_NAME

I understand this is a Docker limitation regarding image naming, however I cannot come up with a way to have Google Cloud Build modify the image name in case the branch name contains a forward slash.

I'm sorry if this isn't the proper place to ask, in which case I would appreciate it if I could be pointed into the right direction. Google searches with keywords "google cloud build image slash" show results rather poorly related to the issue I am having.

Most helpful comment

did anyone found the solution to this?

All 5 comments

Hrm. I can only offer up an ugly solution that involves bash and hacky substitutions. Here's an example where I replace all the hyphens in $BUILD_ID with "OHIO":

#cloudbuild.yaml
steps:
- name: "gcr.io/cloud-builders/docker"
  entrypoint: "/bin/bash"
  # Replace all hyphens in the build ID with OHIO.
  args: ["-c", "_tmpvar=`echo $BUILD_ID | sed -r 's,-,OHIO,g'`; echo $_MOD_BUILD_ID"]
substitutions:
    # Ideally, I should be able to call $_tmpvar=`echo $BUILD_ID | sed command` above,
    # but Cloud Build will try and parse anything prefixed with $ as a substitution,
    # so we do this hack.
    _MOD_BUILD_ID: $_tmpvar 
# Output
starting build "30254fb5-1e22-42fa-890b-a17841738c27"
FETCHSOURCE
BUILD
Already have image (with digest): gcr.io/cloud-builders/docker
30254fb5OHIO1e22OHIO42faOHIO890bOHIOa17841738c27
PUSH
DONE

You can probably do something similar with $BRANCH_NAME. I haven't tested this:

#cloudbuild.yaml
steps:
- name: "gcr.io/cloud-builders/docker"
  entrypoint: "/bin/bash"
  # Replace any forward slashes in the branch name with a hyphen.
  args: ["-c", "_tmpvar=`echo $BRANCH_NAME | sed -r 's,/,-,g'`; docker push gcr.io/$PROJECT_ID/$REPO_NAME:$_MOD_BRANCH_NAME"]
substitutions:
    _MOD_BRANCH_NAME: $_tmpvar 

did anyone found the solution to this?

Anyone know how to escape slashes in the branch name?

In case anyone is still looking for this:

steps:
  - name: "gcr.io/cloud-builders/docker"
    entrypoint: "bash"
    args: [ "-c", "_TMP_VAR=`echo $BRANCH_NAME | sed -r 's,/,-,g'`; docker build -t gcr.io/$PROJECT_ID/$REPO_NAME:$_NEW_BRANCH_NAME ."]
substitutions:
  _NEW_BRANCH_NAME: $_TMP_VAR
images:
  - "gcr.io/$PROJECT_ID/$REPO_NAME"

How to let this works with kaniko?

ERROR: build step 1 "gcr.io/kaniko-project/executor:v0.18.0" failed: starting step container failed: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown
Was this page helpful?
0 / 5 - 0 ratings

Related issues

drgomesp picture drgomesp  路  8Comments

ewhauser picture ewhauser  路  4Comments

leighmcculloch picture leighmcculloch  路  5Comments

govindrai picture govindrai  路  5Comments

namtzigla picture namtzigla  路  3Comments