Need help with pipeline only uploading to Google on main branch merges

Problem to solve

I am using gitlabs hosted service at gitlab.com. I have a repository which is being built into an image and then uploaded to the Google Artifact Registry where I then run the service in a container in Google Cloud Run.

I am trying to design the pipeline so that the build and upload only happens on merges to main. So I can work on and commit a branch, but the image won’t be built and sent to Google until I merge with main.

Steps to reproduce

I am able to add the rule that it runs only if there is a commit to the main branch. But it doesn’t seem to apply to the Google integration. So if I create a new branch and commit, the build doesn’t happen, but it still tries to upload to the registry.

Configuration

This is my current pipeline configuration.

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

stages:
  - build
  - deploy

variables:
  GITLAB_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

build-docker-image:
  image: docker:24.0.5
  stage: build
  services:
    - docker:24.0.5-dind
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $GITLAB_IMAGE .
    - docker push $GITLAB_IMAGE

include:
  - component: gitlab.com/google-gitlab-components/artifact-registry/upload-artifact-registry@0.1.0
    inputs:
      stage: deploy
      source: $GITLAB_IMAGE
      target: us-west1-docker.pkg.dev/myapplication/myapplication/image:v1.0.0

Versions

Please select whether options apply, and add the version information.

  • Self-managed
  • GitLab.com SaaS
  • Dedicated
  • Self-hosted Runners

I found the answer, I needed to add the following.

upload-artifact-registry:
rules:
- if: ‘$CI_COMMIT_BRANCH == “main”’