Gitlab 简明教程
GitLab CI - Advanced usage of CI
Environments and Deployments
环境用于测试、构建和部署 CI(持续集成)作业,并使用 GitLab 控制软件的持续部署。GitLab CI 能够跟踪您的项目部署,您还将了解服务器上正在部署的内容。
Environments are used for testing, building and deploying the CI (Continuous Integration) jobs and control the Continuous Deployment of software with the GitLab. GitLab CI is capable of tracking your project deployments and also you will come to know what is being deployed on your server.
环境的名称可以使用 environment:name 字符串来定义,并包含以下内容 −
The name of an environment could be defined by using environment:name string and contain the following −
-
letters
-
digits
-
spaces
-
-
-
_
-
/
-
$
-
{
-
}
Using SSH keys with GitLab CI/CD
您可以设置 SSH(安全外壳或安全套接字外壳)密钥,以便在计算机和 GitLab 之间提供可靠连接。当 −
You can set the SSH (Secure Shell or Secure Socket Shell) keys to provide a reliable connection between the computer and GitLab. The SSH keys can be used with GitLab CI/CD when −
-
You need to checkout internal sub modules.
-
You need to download private packages using package manager.
-
You need to install an application to your own server.
-
You execute the SSH commands to remote server from build environment.
-
You need to rsync files to a remote server from the build environment.
SSH 密钥设置在 GitLab SSH Key Setup 章节中进行了解释。
The SSH key setup is explained in the GitLab SSH Key Setup chapter.
Artifacts
工件用于在成功后将文件和目录列表附加到作业。工件包含以下类型 −
Artifacts are used to attach the list of files and directories to the job after success. The artifacts contain following types −
-
artifacts:name − This directive is used to specify the name of created artifacts archive. It provides unique name for created artifacts archive which is helpful when you are downloading the archive from GitLab.
-
artifacts:when − This directive is used to upload artifacts when there is a job failure. It contains the following values: on_success − It is used to upload the artifacts when there is a job success.on_failure − It is used to upload the artifacts when the job fails.always − It is used to upload the artifacts regardless of job status.
-
artifacts:expire_in − It defines that how long artifacts should live before they expire and therefore deleted, since they are uploaded and stored on GitLab
Triggering Pipelines
触发器可以强制特定分支或标记使用 API 调用重新构建,并且带有旧标签的触发器将可以访问当前项目。
Triggers can force a specific branch or tag to get rebuilt with an API call and triggers with the legacy label will have access to the current project.
新触发器可以按以下步骤添加 −
The new trigger can be added as shown in the below steps −
Step 1 − 登录到 GitLab 帐号并转到您的专案 −
Step 1 − Login to your GitLab account and go to your project −

Step 2 − 在“设置”选项卡下单击 CI/CD 选项,然后展开“管道触发器”选项 −
Step 2 − Click on the CI/CD option under Settings tab and expand the Pipeline triggers option −

输入触发器的说明并点击“添加触发器”按钮。
Enter the description for the trigger and click on the Add Trigger button.
Step 3 − 其次,它将在创建触发器后显示成功消息 −
Step 3 − Next, it will display the success message after creating the trigger −

Step 4 − 现在转到“设置”选项卡下的 CI/CD 选项,然后展开“管道触发器”选项。您将看到新创建的触发器以及令牌,如下图所示 −
Step 4 − Now go to CI/CD option under Settings tab and expand the Pipeline triggers option. You will see the newly created trigger along with the token as shown in the image below −

Pipeline Schedules
您可以使用管道计划在特定时间间隔内运行管道。要创建管道计划,请使用以下步骤 −
You can run the pipeline by using the pipeline schedules at specific intervals. To create pipeline schedule, use the below steps −

Step 2 − 点击 CI/CD 选项卡下的“计划”选项,然后点击“新建计划”按钮 −
Step 2 − Click on the Schedules option under CI/CD tab and click on the New schedule button −

Step 3 − 其次,它将打开“计划新管道”屏幕,填写字段并点击“保存管道计划”按钮 −
Step 3 − Next, it will open the Scheduling new pipeline screen, fill up the fields and click on the Save pipeline schedule button −

Step 4 − 现在,您将看到计划运行的管道 −
Step 4 − Now, you will see the pipeline which is scheduled to run −

Connecting GitLab with a Kubernetes Cluster
可以使用 Kubernetes 集群以简单的方式查看和部署应用、运行管道等。您可以通过将 GitLab 账户与 Google Kubernetes Engine (GKE) 关联来为项目创建新集群。
The Kubernetes cluster can be used to review and deploy the applications, running the pipeline etc in an easy method. You can create a new cluster to your project by associating your GitLab account with the Google Kubernetes Engine (GKE).
新 Kubernetes 集群的创建如下所示 −
The new Kubernetes cluster can be created as shown in the below steps −
Step 1 − 登录到 GitLab 帐号并转到您的专案 −
Step 1 − Login to your GitLab account and go to your project −

Step 2 − 点击 CI/CD 选项卡下的“Kubernetes”选项 −
Step 2 − Click on the Kubernetes option under CI/CD tab −

Step 3 − 其次,点击“添加 Kubernetes 集群”按钮 −
Step 3 − Next, click on Add Kubernetes cluster button −

Step 4 − 点击“在 GKE 上创建”按钮在 Google Kubernetes Engine 上创建新 Kubernetes 集群 −
Step 4 − Click on Create on GKE button to create a new Kubernetes cluster on Google Kubernetes Engine −

Step 5 − 如果您有 Google 账户,那么使用该账户注册以输入 Kubernetes 集群的详细信息,否则创建新 Google 账户 −
Step 5 − If you have a Google account, then sign with that account to enter the details for Kubernetes cluster or else create a new Google account −

Step 6 − 现在输入 Kubernetes 集群的各个字段的值 −
Step 6 − Now enter the values in the fields for your Kubernetes cluster −

Step 7 − 在各个字段输入值之前,您需要在 Google Cloud Platform 控制台中创建的项目的 ID 来托管 Kubernetes 集群。要创建 ID,点击上个图片中突出显示的“查看您的项目”链接。它将打开以下屏幕,然后点击“我的项目”菜单,然后点击加号 (+) 图标创建新项目 −
Step 7 − Before adding values in the fields, you need ID of the project which is created in the Google Cloud Platform console to host the Kubernetes cluster. To create ID, click on the See your projects link which is highlighted in the previous image. It will open the below screen, then click on My Project menu and click on the plus (+) icon to create a new project −

Step 8 − 现在输入项目名称并点击“创建”按钮 −
Step 8 − Now enter the project name and click on the Create button −

Step 9 − 您将获得托管 Kubernetes 集群的项目的 ID −
Step 9 − You will get the ID of the project which will host the Kubernetes cluster −

Step 10 − 填写 Kubernetes 集群各个字段的值以及 Google Cloud Platform 项目 ID,然后点击“创建 Kubernetes 集群”按钮 −
Step 10 − Enter the values in the fields for your Kubernetes cluster along with the Google Cloud Platform project ID and click on the Create Kubernetes cluster button −
