Tutorial: Create a GitLab pipeline to push to Google 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
-
To run the commands on this page, set up the
gcloud
CLI in one of the following development environments: -
Create or select a Google Cloud project.
NOTE: If you don't plan to keep the resources that you create in this procedure, then create a new Google Cloud project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.
To create a Google Cloud project, run the following command:
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. -
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Compute Engine and Artifact Registry APIs:
gcloud services enable compute.googleapis.com artifactregistry.googleapis.com
-
Set up the GitLab on Google Cloud integration by following the instructions in Google Cloud Workload Identity Federation and IAM policies.
-
Create a standard mode Docker format Artifact Registry repository.
-
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
-
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.
-
If you are working in your local shell, install Terraform. Terraform is already installed in Cloud Shell.
Create a Dockerfile
-
In your cloned repository, create a new file named
Dockerfile
. -
Copy and paste the following into your
Dockerfile
.# Dockerfile for test purposes. Generates a new random image in every build. FROM alpine:3.15.11 RUN dd if=/dev/urandom of=random bs=10 count=1
-
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 Google 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.
After 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 the GitLab container registry, and copies the image to Google Artifact Registry.
-
In your GitLab project, create a
.gitlab-ci.yml
file. -
To create a pipeline that builds your image, pushes it to the GitLab container registry, and copies it to Google Artifact Registry, modify the contents of your
.gitlab-ci.yml
file to resemble the following.In the example, replace the following:
-
LOCATION
: the Google Cloud region where you created your Google Artifact Registry repository. -
PROJECT
: your Google Cloud project ID. -
REPOSITORY
: the repository ID of your Google 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: {{ov}}LOCATION{{cv}}-docker.pkg.dev/{{ov}}PROJECT{{cv}}/{{ov}}REPOSITORY{{cv}}/image:v1.0.0
-
The pipeline uses Docker in Docker to build the image docker:24.0.5
, stores it
in the GitLab container registry, and then uses the
Google Artifact Registry GitLab component
to push it to your Google Artifact Registry
repository with the version v1.0.0
.
View your artifacts
To view your artifact in GitLab:
- In your GitLab project, on the left sidebar, select Build > Artifacts.
- Select the name of the artifact to view the details of the build.
To view your artifact in Google Artifact Registry:
- Open the Repositories page in the Google Cloud console.
- Select the name of your linked repository.
- Select the name of the image to view the version name and tags.
- Select 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 Google Artifact Registry repository.
For information on GitLab and Google Artifact Registry pricing and project management, see the following resources:
Delete your Google Artifact Registry repository
If you want to keep your Google Cloud project and only delete the Google 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 your repository, run the following command:
gcloud artifacts repositories delete REPOSITORY \
--location=LOCATION
Replace the following:
-
REPOSITORY
with your Google Artifact Registry repository ID -
LOCATION
with the location of your repository
Delete your Google Cloud project
Caution: Deleting a project has the following effects:
- Everything in the project is deleted. If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
- Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an appspot.com URL, delete selected resources inside the project instead of deleting the whole project.
If you plan to explore multiple architectures, tutorials, or quick start tutorials on Google Cloud, reusing projects can help you avoid exceeding project quota limits.
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then select Delete.
- In the dialog, type the project ID, and then select Shut down to delete the project.
Related topics
- Learn how to Optimize GitLab CI/CD configuration files.
- Read about how the GitLab on Google Cloud integration uses IAM with workload identity federation to control access to Google Cloud in Access control with {{iam_name}}.