Create a GitLab pipeline to push to Artifact Registry

Learn how to connect GitLab to Google Cloud and create a GitLab pipeline using runners on Compute Engine to push images to Artifact Registry.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the Compute Engine and Artifact Registry APIs:

    gcloud services enable compute.googleapis.com artifactregistry.googleapis.com
  7. Install the Google Cloud CLI.
  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  10. Make sure that billing is enabled for your Google Cloud project.

  11. Enable the Compute Engine and Artifact Registry APIs:

    gcloud services enable compute.googleapis.com artifactregistry.googleapis.com
  12. Set up the GitLab on Google Cloud integration by following the GitLab instructions in Google Cloud Workload Identity Federation and IAM policies.
  13. Create a standard mode Docker format Artifact Registry repository.
  14. Connect your Artifact Registry repository to your GitLab project by following the instructions in Set up the Google Artifact Registry registry in a GitLab project.

Clone your GitLab repository

  1. To clone your GitLab repository to your working environment using SSH or HTTPS, follow the instructions in Clone a Git repository to your local computer.

  2. If you are working in your local shell, install Terraform. Terraform is already installed in Cloud Shell.

Create a Dockerfile

  1. In your cloned repository, create a new file named Dockerfile.
  2. Copy and paste the following into your Dockerfile.

    # Dockerfile for test purposes. Generates a new random image in every build.
    FROM alpine:latest
    RUN dd if=/dev/urandom of=random bs=10 count=1
    
  3. Add your Dockerfile to git, commit, and push to your GitLab repository.

    git add Dockerfile
    git commit -m "add dockerfile"
    git push
    

    You are prompted to enter your username and personal access token.

The Dockerfile generates a new random image for every build, and is only for test purposes.

Enable continuous integration (CI) runners on Compute Engine

GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline. The GitLab on Google Cloud integration assists you in setting up an autoscaling fleet of runners on Compute Engine, with a runner manager that creates temporary runners to execute multiple jobs simultaneously.

To set up your autoscaling fleet of runners, follow the instructions in Set up GitLab Runner to execute your CI/CD jobs on Google Cloud. Select Google Cloud as the environment where you want your runners to execute your CI/CD jobs, and fill out the rest of the configuration details.

Once you have entered the details for your runners, you can follow the setup instructions to configure your Google Cloud project, install and register GitLab Runner, and apply the provided terraform in your working environment to apply the configuration.

Create a pipeline

Create a pipeline that builds your Docker image, pushes it to GitLab container registry, and copies the image to Artifact Registry.

  1. In your GitLab project, create a .gitlab-ci.yml file.

  2. To create a pipeline that builds your image, pushes it to GitLab container registry, and copies it to Artifact Registry, modify the contents of your .gitlab-ci.yml file to resemble the following.

    Before copying the example, replace the following:

    • LOCATION: the Google Cloud region where you created your Artifact Registry repository.
    • PROJECT: your Google Cloud project ID.
    • REPOSITORY: the repository ID of your Artifact Registry repository.
    stages:
      - build
      - deploy
    
    variables:
      GITLAB_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
    
    build-sample-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: LOCATION-docker.pkg.dev/PROJECT/REPOSITORY/image:v1.0.0
    

The pipeline builds the image docker:24.0.5 using docker-in-docker, stores it in the GitLab container registry, and then pushes it to your Artifact Registry repository with the version v1.0.0 using the Artifact Registry GitLab component.

View your artifacts

To view your artifact in GitLab:

  1. In your GitLab project, on the left sidebar, select Build > Artifacts.
  2. Click the name of the artifact to view the details of the build.

To view your artifact in Artifact Registry:

  1. Open the Repositories page in the Google Cloud console.

    Open the Repositories page

  2. Click the name of your linked repository.

  3. Click the name of the image to view the version name and tags.

  4. Click the name of the image version to view the version's build, pull, and manifest information.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, you can delete your Google Cloud project. If you want to keep your project, you can delete your Artifact Registry repository.

For information on GitLab pricing and project management, see the following resources:

Delete your Artifact Registry repository

If you want to keep your Google Cloud project and only delete the Artifact Registry repository resource, follow the steps in this section. If you want to delete your entire Google Cloud project, follow the steps in Delete your project.

Before you remove the repository, ensure that any images you want to keep are available in another location.

To delete the repository:

Console

  1. Open the Repositories page in the Google Cloud console.

    Open the Repositories page

  2. In the repository list, select the repository you want to delete.

  3. Click Delete.

gcloud

To delete your repository, run the following command:

gcloud artifacts repositories delete REPOSITORY \
    --location=LOCATION

Delete your Google Cloud project

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next