日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

gitlab-ci详细说明

發(fā)布時(shí)間:2024/4/13 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 gitlab-ci详细说明 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 概念
    • Pipeline
    • Stages
    • Jobs
      • job分組
    • variables
      • 自定義變量
      • 預(yù)定義變量
      • 使用變量
  • 關(guān)鍵詞
    • 全局關(guān)鍵詞
      • before_script
      • after_script
      • include
    • Stage關(guān)鍵詞
    • Job關(guān)鍵詞
      • script
      • image
      • artifacts
      • tags
      • cache
      • stage
      • when
      • only/except
      • dependencies
      • extends
      • trigger
      • rules
      • allow_failure
      • coverage
      • pages
      • release
      • resource_group
      • retry
      • timeout
  • 參考
    • gitlabci變量

概念

從 Gitlab 8.0 開(kāi)始,Gitlab CI 就已經(jīng)集成在 Gitlab 中,我們只要在項(xiàng)目中添加一個(gè).gitlab-ci.yml文件,然后添加一個(gè)Runner,即可進(jìn)行持續(xù)集成。

Pipeline

一次 Pipeline 其實(shí)相當(dāng)于一次構(gòu)建任務(wù),里面可以包含很多個(gè)流程,如安裝依賴(lài)、運(yùn)行測(cè)試、編譯、部署測(cè)試服務(wù)器、部署生產(chǎn)服務(wù)器等流程。任何提交或者 Merge Request 的合并都可以觸發(fā) Pipeline 構(gòu)建。

Stages

Stages 表示一個(gè)構(gòu)建階段,也就是上面提到的一個(gè)流程。我們可以在一次 Pipeline 中定義多個(gè) Stages,這些 Stages 會(huì)有以下特點(diǎn):

  • 所有 Stages 會(huì)按照順序運(yùn)行,即當(dāng)一個(gè) Stage 完成后,下一個(gè) Stage 才會(huì)開(kāi)始
  • 只有當(dāng)所有 Stages 完成后,該構(gòu)建任務(wù) (Pipeline) 才會(huì)成功
  • 如果任何一個(gè) Stage 失敗,那么后面的 Stages 不會(huì)執(zhí)行,該構(gòu)建任務(wù) (Pipeline) 失敗

Jobs

Jobs 表示構(gòu)建工作,表示某個(gè) Stage 里面執(zhí)行的工作。我們可以在 Stages 里面定義多個(gè) Jobs,這些 Jobs 會(huì)有以下特點(diǎn):

  • 相同 Stage 中的 Jobs 會(huì)并行執(zhí)行
  • 相同 Stage 中的 Jobs 都執(zhí)行成功時(shí),該 Stage 才會(huì)成功
  • 如果任何一個(gè) Job 失敗,那么該 Stage 失敗,即該構(gòu)建任務(wù) (Pipeline) 失敗

job分組

可以把job分組,通過(guò) 以下方式,job名稱(chēng)為build ruby,有3步。

build ruby 1/3:stage: buildscript:- echo "ruby1"build ruby 2/3:stage: buildscript:- echo "ruby2"build ruby 3/3:stage: buildscript:- echo "ruby3"

variables

變量包括自定義變量和CI/CD預(yù)定義的變量。
參考:https://docs.gitlab.com/ee/ci/variables/#variables

自定義變量

自定義變量可以通過(guò)以下語(yǔ)句定義:

variables:TEST: "HELLO WORLD"

預(yù)定義變量

There are also Kubernetes-specific deployment variables.

VariableGitLabRunnerDescription
CHAT_CHANNEL10.6allThe Source chat channel that triggered the ChatOps command.
CHAT_INPUT10.6allThe additional arguments passed with the ChatOps command.
CIall0.4Available for all jobs executed in CI/CD. true when available.
CI_API_V4_URL11.7allThe GitLab API v4 root URL.
CI_BUILDS_DIRall11.10The top-level directory where builds are executed.
CI_COMMIT_BEFORE_SHA11.2allThe previous latest commit present on a branch. Is always 0000000000000000000000000000000000000000 in pipelines for merge requests.
CI_COMMIT_BRANCH12.60.5The commit branch name. Available in branch pipelines, including pipelines for the default branch. Not available in merge request pipelines or tag pipelines.
CI_COMMIT_DESCRIPTION10.8allThe description of the commit. If the title is shorter than 100 characters, the message without the first line.
CI_COMMIT_MESSAGE10.8all提交message。
CI_COMMIT_REF_NAME9.0allbranch or tag名稱(chēng)
CI_COMMIT_REF_PROTECTED11.11alltrue if the job is running for a protected reference.
CI_COMMIT_REF_SLUG9.0allCI_COMMIT_REF_NAME in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.
CI_COMMIT_SHA9.0all提交version
CI_COMMIT_SHORT_SHA11.7allCI_COMMIT_SHA 前8字符
CI_COMMIT_TAG9.00.5The commit tag name. Available only in pipelines for tags.
CI_COMMIT_TIMESTAMP13.4allThe timestamp of the commit in the ISO 8601 format.
CI_COMMIT_TITLE10.8allThe title of the commit. The full first line of the message.
CI_CONCURRENT_IDall11.10The unique ID of build execution in a single executor.
CI_CONCURRENT_PROJECT_IDall11.10The unique ID of build execution in a single executor and project.
CI_CONFIG_PATH9.40.5The path to the CI/CD configuration file. Defaults to .gitlab-ci.yml.
CI_DEBUG_TRACEall1.7true if debug logging (tracing) is enabled.
CI_DEFAULT_BRANCH12.4allThe name of the project’s default branch.
CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX13.7allThe image prefix for pulling images through the Dependency Proxy.
CI_DEPENDENCY_PROXY_PASSWORD13.7allThe password to pull images through the Dependency Proxy.
CI_DEPENDENCY_PROXY_SERVER13.7allThe server for logging in to the Dependency Proxy. This is equivalent to $CI_SERVER_HOST:$CI_SERVER_PORT.
CI_DEPENDENCY_PROXY_USER13.7allThe username to pull images through the Dependency Proxy.
CI_DEPLOY_FREEZE13.2allOnly available if the pipeline runs during a deploy freeze window. true when available.
CI_DEPLOY_PASSWORD10.8allThe authentication password of the GitLab Deploy Token, if the project has one.
CI_DEPLOY_USER10.8allThe authentication username of the GitLab Deploy Token, if the project has one.
CI_DISPOSABLE_ENVIRONMENTall10.1Only available if the job is executed in a disposable environment (something that is created only for this job and disposed of/destroyed after the execution - all executors except shell and ssh). true when available.
CI_ENVIRONMENT_NAME8.15allThe name of the environment for this job. Available if environment:name is set.
CI_ENVIRONMENT_SLUG8.15allThe simplified version of the environment name, suitable for inclusion in DNS, URLs, Kubernetes labels, and so on. Available if environment:name is set.
CI_ENVIRONMENT_URL9.3allThe URL of the environment for this job. Available if environment:url is set.
CI_HAS_OPEN_REQUIREMENTS13.1allOnly available if the pipeline’s project has an open requirement. true when available.
CI_JOB_ID9.0alljob id
CI_JOB_IMAGE12.912.9運(yùn)行job的容器的image
CI_JOB_JWT12.10allA RS256 JSON web token to authenticate with third party systems that support JWT authentication, for example HashiCorp’s Vault.
CI_JOB_MANUAL8.12alljob是否是人工執(zhí)行的
CI_JOB_NAME8.12alljob名稱(chēng)
CI_JOB_STAGE9.00.5The name of the job’s stage.
CI_JOB_STATUSall13.5The status of the job as each runner stage is executed. Use with after_script. Can be success, failed, or canceled.
CI_JOB_TOKEN9.01.2A token to authenticate with certain API endpoints. The token is valid as long as the job is running.
CI_JOB_URL11.10.5The job details URL.
CI_JOB_STARTED_AT13.10allThe UTC datetime when a job started, in ISO 8601 format.
CI_KUBERNETES_ACTIVE13.0allOnly available if the pipeline has a Kubernetes cluster available for deployments. true when available.
CI_NODE_INDEX11.5allThe index of the job in the job set. Only available if the job uses parallel.
CI_NODE_TOTAL11.5allThe total number of instances of this job running in parallel. Set to 1 if the job does not use parallel.
CI_OPEN_MERGE_REQUESTS13.8allA comma-separated list of up to four merge requests that use the current branch and project as the merge request source. Only available in branch and merge request pipelines if the branch has an associated merge request. For example, gitlab-org/gitlab!333,gitlab-org/gitlab-foss!11.
CI_PAGES_DOMAIN11.8allThe configured domain that hosts GitLab Pages.
CI_PAGES_URL11.8allThe URL for a GitLab Pages site. Always a subdomain of CI_PAGES_DOMAIN.
CI_PIPELINE_ID8.10all流水線(xiàn)ID,gitlab實(shí)例內(nèi)唯一
CI_PIPELINE_IID11.0all流水線(xiàn)ID,項(xiàng)目?jī)?nèi)唯一
CI_PIPELINE_SOURCE10.0allHow the pipeline was triggered. Can be push, web, schedule, api, external, chat, webide, merge_request_event, external_pull_request_event, parent_pipeline, trigger, or pipeline.
CI_PIPELINE_TRIGGEREDallalltrue if the job was triggered.
CI_PIPELINE_URL11.10.5The URL for the pipeline details.
CI_PIPELINE_CREATED_AT13.10allThe UTC datetime when the pipeline was created, in ISO 8601 format.
CI_PROJECT_CONFIG_PATH13.8all(Deprecated) The CI configuration path for the project. Deprecated in GitLab 13.10. Removal planned for GitLab 14.0.
CI_PROJECT_DIRallallThe full path the repository is cloned to, and where the job runs from. If the GitLab Runner builds_dir parameter is set, this variable is set relative to the value of builds_dir. For more information, see the Advanced GitLab Runner configuration.
CI_PROJECT_IDallallThe ID of the current project. This ID is unique across all projects on the GitLab instance.
CI_PROJECT_NAME8.100.5The name of the directory for the project. For example if the project URL is gitlab.example.com/group-name/project-1, CI_PROJECT_NAME is project-1.
CI_PROJECT_NAMESPACE8.100.5The project namespace (username or group name) of the job.
CI_PROJECT_PATH_SLUG9.3all$CI_PROJECT_PATH in lowercase with characters that are not a-z or 0-9 replaced with -. Use in URLs and domain names.
CI_PROJECT_PATH8.100.5The project namespace with the project name included.
CI_PROJECT_REPOSITORY_LANGUAGES12.3allA comma-separated, lowercase list of the languages used in the repository. For example ruby,javascript,html,css.
CI_PROJECT_ROOT_NAMESPACE13.20.5The root project namespace (username or group name) of the job. For example, if CI_PROJECT_NAMESPACE is root-group/child-group/grandchild-group, CI_PROJECT_ROOT_NAMESPACE is root-group.
CI_PROJECT_TITLE12.4allThe human-readable project name as displayed in the GitLab web interface.
CI_PROJECT_URL8.100.5The HTTP(S) address of the project.
CI_PROJECT_VISIBILITY10.3allThe project visibility. Can be internal, private, or public.
CI_REGISTRY_IMAGE8.100.5The address of the project’s Container Registry. Only available if the Container Registry is enabled for the project.
CI_REGISTRY_PASSWORD9.0allThe password to push containers to the project’s GitLab Container Registry. Only available if the Container Registry is enabled for the project.
CI_REGISTRY_USER9.0allThe username to push containers to the project’s GitLab Container Registry. Only available if the Container Registry is enabled for the project.
CI_REGISTRY8.100.5The address of the GitLab Container Registry. Only available if the Container Registry is enabled for the project. This variable includes a :port value if one is specified in the registry configuration.
CI_REPOSITORY_URL9.0allThe URL to clone the Git repository.
CI_RUNNER_DESCRIPTION8.100.5The description of the runner.
CI_RUNNER_EXECUTABLE_ARCHall10.6The OS/architecture of the GitLab Runner executable. Might not be the same as the environment of the executor.
CI_RUNNER_ID8.100.5The unique ID of the runner being used.
CI_RUNNER_REVISIONall10.6The revision of the runner running the job.
CI_RUNNER_SHORT_TOKENall12.3First eight characters of the runner’s token used to authenticate new job requests. Used as the runner’s unique ID.
CI_RUNNER_TAGS8.100.5A comma-separated list of the runner tags.
CI_RUNNER_VERSIONall10.6The version of the GitLab Runner running the job.
CI_SERVER_HOST12.1allThe host of the GitLab instance URL, without protocol or port. For example gitlab.example.com.
CI_SERVER_NAMEallallThe name of CI/CD server that coordinates jobs.
CI_SERVER_PORT12.8allThe port of the GitLab instance URL, without host or protocol. For example 8080.
CI_SERVER_PROTOCOL12.8allThe protocol of the GitLab instance URL, without host or port. For example https.
CI_SERVER_REVISIONallallGitLab revision that schedules jobs.
CI_SERVER_URL12.7allThe base URL of the GitLab instance, including protocol and port. For example https://gitlab.example.com:8080.
CI_SERVER_VERSION_MAJOR11.4allThe major version of the GitLab instance. For example, if the GitLab version is 13.6.1, the CI_SERVER_VERSION_MAJOR is 13.
CI_SERVER_VERSION_MINOR11.4allThe minor version of the GitLab instance. For example, if the GitLab version is 13.6.1, the CI_SERVER_VERSION_MINOR is 6.
CI_SERVER_VERSION_PATCH11.4allThe patch version of the GitLab instance. For example, if the GitLab version is 13.6.1, the CI_SERVER_VERSION_PATCH is 1.
CI_SERVER_VERSIONallallThe full version of the GitLab instance.
CI_SERVERallallAvailable for all jobs executed in CI/CD. yes when available.
CI_SHARED_ENVIRONMENTall10.1Only available if the job is executed in a shared environment (something that is persisted across CI/CD invocations, like the shell or ssh executor). true when available.
GITLAB_CIallallAvailable for all jobs executed in CI/CD. true when available.
GITLAB_FEATURES10.6allThe comma-separated list of licensed features available for the GitLab instance and license.
GITLAB_USER_EMAIL8.12allThe email of the user who started the job.
GITLAB_USER_ID8.12allThe ID of the user who started the job.
GITLAB_USER_LOGIN10.0allThe username of the user who started the job.
GITLAB_USER_NAME10.0allThe name of the user who started the job.
TRIGGER_PAYLOAD13.9allThe webhook payload. Only available when a pipeline is triggered with a webhook.

使用變量

ShellUsage
bash/sh$variable,${variable}
PowerShell$env:variable (primary) or $variable
Windows Batch%variable%, or !variable! for delayed expansion, which can be used for variables that contain white spaces or newlines.

變量可以通過(guò)依賴(lài)job 來(lái)繼承。

關(guān)鍵詞

全局關(guān)鍵詞

before_script

before_script 關(guān)鍵詞是用于在每個(gè)任務(wù)之前執(zhí)行的腳本,但是會(huì)在artifacts恢復(fù)之后執(zhí)行。你可以這樣定義一個(gè)全局的before_script,

default:before_script:- echo "Execute this script in all jobs that don't already have a before_script section."

也可以在一個(gè)任務(wù)中中單獨(dú)定義

job:before_script:- echo "Execute this script instead of the global before_script."script:- echo "This script executes after the job's `before_script`"

任務(wù)中的before_script會(huì)覆蓋全局的before_script

after_script

after_script與before_script類(lèi)似,用于定義多行腳本,會(huì)在任務(wù)執(zhí)行完成后執(zhí)行,即使任務(wù)失敗也會(huì)被執(zhí)行。如果任務(wù)被取消或者超時(shí),after_script就不會(huì)被執(zhí)行了,可以定義全局的,也可以定義局部的

default:after_script:- echo "Execute this script in all jobs that don't already have an after_script section."job1:script:- echo "This script executes first. When it completes, the global after_script executes."

include

使用include可以導(dǎo)入一個(gè)或多個(gè)額外的yaml文件到CI/CD配置里,這就可以將一個(gè)很長(zhǎng)的流水線(xiàn),分隔出來(lái)。使用include來(lái)引入。
也可以將幾個(gè)流水線(xiàn)中相同的配置,提取出來(lái),公用。引入的文件擴(kuò)展名 必須是.yaml或者.yml兩種,其他的不行。

include 關(guān)鍵詞下,有四個(gè)可選性:

  • local, 引入一個(gè)當(dāng)前項(xiàng)目的文件
  • file, 引入一個(gè)不同項(xiàng)目的文件
  • remote, 引入一個(gè)公網(wǎng)文件,
  • template, 引入一個(gè)由GitLab提供的模板
include:- local: '/templates/.gitlab-ci-template.yml' --- include:- project: 'my-group/my-project' #另外一個(gè)項(xiàng)目file: '/templates/.gitlab-ci-template.yml'ref: master #master分支 --- include:- remote: 'https://gitlab.com/awesome-project/raw/master/.gitlab-ci-template.yml'

Stage關(guān)鍵詞

Job關(guān)鍵詞

script

任務(wù)要執(zhí)行的shell腳本內(nèi)容,內(nèi)容會(huì)被runner執(zhí)行,在這里,你不需要使用git clone …克隆當(dāng)前的項(xiàng)目,來(lái)進(jìn)行操作,因?yàn)樵诹魉€(xiàn)中,每一個(gè)的job的執(zhí)行都會(huì)將項(xiàng)目下載,恢復(fù)緩存這些流程,不需要你再使用腳本恢復(fù)。你只需要在這里寫(xiě)你的項(xiàng)目安裝,編譯執(zhí)行,如
npm install 另外值得一提的是,腳本的工作目錄就是當(dāng)前項(xiàng)目的根目錄,所有可以就像在本地開(kāi)發(fā)一樣。此外script可以是單行或者多行

單行腳本:

job:script: npm install

多行腳本:

job:script:- npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/- npm install --registry=http://registry.npm.taobao.org

***script***是一個(gè)job的必填內(nèi)容,不可或缺。一個(gè)job最少有二個(gè)屬性,一個(gè)是 job name, 任務(wù)名稱(chēng), 一個(gè)就是script。

?

image

指定一個(gè)基礎(chǔ)Docker鏡像作為基礎(chǔ)運(yùn)行環(huán)境,經(jīng)常用到的鏡像有node, nginx ,docker

job:image: node:latestscript: npm install

image的作用就是給當(dāng)前任務(wù)或者當(dāng)前流水線(xiàn)設(shè)置一個(gè)基礎(chǔ)環(huán)境,有可能是nodejs,也有可能是java, go, php,可以設(shè)置當(dāng)前流水線(xiàn)的,也可以設(shè)置當(dāng)前任務(wù)的。

artifacts

制品,一個(gè)成品,作用是將流水線(xiàn)過(guò)程中的一些文件,文件夾,打包壓縮,提供一個(gè)外鏈供人下載,另外還能在后續(xù)的job中緩存。
比如我們構(gòu)建一個(gè)前端項(xiàng)目后將dist目錄做成一個(gè)壓縮包,

build:script:- npm run buildartifacts:paths:- dist/

后面的任務(wù)可以使用生成的制品。

paths 是用來(lái)指定將哪些文件,目錄放入制品中, 也可以使用exclude關(guān)鍵詞,將那些目錄,文件排除,支持正則表達(dá)式。
此外還有以下幾個(gè)屬性:

  • paths :文件路徑
  • exclude: 排除的文件
  • name :制品名稱(chēng)
  • expose_as: 在UI頁(yè)面導(dǎo)出的名稱(chēng)
  • untracked :布爾類(lèi)型,是否將git忽略的文件加到制品中
  • when :何時(shí)上傳制品,可選值:on_success;on_failure;always
  • expire_in: 過(guò)期時(shí)間默認(rèn)30天
  • reports: 收集測(cè)試報(bào)告
build:script:- npm run buildartifacts:paths:- dist/exclude:- binaries/**/*.oexpose_as: 'artifact 1'name: "$CI_COMMIT_REF_NAME"untracked: falseexpire_in: 2 hrs 20 minwhen: on_failure

tags

tags 關(guān)鍵詞是用于指定Runner。tags的取值范圍是在該項(xiàng)目可見(jiàn)的runner tags中,可以在Setting =>CI/CD => Runner 中查看的到。不設(shè)置則默認(rèn)使用公有Runner去執(zhí)行流水線(xiàn)。每個(gè)任務(wù)可以指定一個(gè)Runner,可以指定多個(gè)標(biāo)簽,但runner卻只能一個(gè)。以一個(gè)為準(zhǔn)。tags是在注冊(cè)Runner時(shí)配置的,后續(xù)也可以更改。

install:tags:- hello-vue- dockerscript:- npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/- npm install --registry=http://registry.npm.taobao.org

cache

緩存是將當(dāng)前工作環(huán)境目錄中的一些文件,一些文件夾存儲(chǔ)起來(lái),用于在各個(gè)任務(wù)初始化的時(shí)候恢復(fù)。避免多個(gè)下載同樣的包,能夠大大優(yōu)化流水線(xiàn)效率。在前端項(xiàng)目中,我們經(jīng)常把node_modules緩存起來(lái),這樣一條流水線(xiàn)都可以使用這些下載好的包。在java項(xiàng)目中經(jīng)常把maven下載的包緩存起來(lái)。以備后用
cache下的參數(shù)有

  • paths:當(dāng)前工作環(huán)境下的目錄
  • key:存儲(chǔ)的key,key不變不會(huì)重新生成緩存,
  • prefix:使用一些文件制作成文件hash值,當(dāng)做key的一部分,
  • untracked :是否緩存git忽略的文件
  • when :定義何時(shí)存儲(chǔ)緩存,值 on_success;on_failure;always
  • policy:緩存是否要在job完成后重新上傳

緩存可以設(shè)置流水線(xiàn)全局,也可以在job中設(shè)置

cache:key: hello-vue-cachepaths:- node_modules##### cache:key:files:- Gemfile.lock- package.jsonpaths:- vendor/ruby- node_modules

stage

stage 是階段的意思,用于歸檔一部分的job,按照定義的stage順序來(lái)執(zhí)行。默認(rèn)的stage有build,test,deploy, 此外還有兩個(gè)特殊的.pre 和 .post。以下執(zhí)行順序: job_1,job_0。

stages:- build- test- deployjob_0:stage: testscript: echo 'tets'job_1:stage: buildscript: echo 'build'

when

when關(guān)鍵字是實(shí)現(xiàn)在發(fā)生故障或盡管發(fā)生故障時(shí)仍能運(yùn)行的作業(yè)。比如你要在任務(wù)失敗后需要觸發(fā)一個(gè)job,
或者你需要手動(dòng)執(zhí)行任務(wù),或者當(dāng)你一個(gè)任務(wù)執(zhí)行成功后,執(zhí)行另一個(gè)任務(wù).

  • on_success 所有任務(wù)執(zhí)行成功后
  • on_failure 當(dāng)至少一個(gè)任務(wù)失敗后
  • always 執(zhí)行作業(yè),而不考慮作業(yè)在早期階段的狀態(tài)。
  • manual 手動(dòng)執(zhí)行任務(wù)
  • delayed 延遲執(zhí)行任務(wù)
  • never
    在rules中不排除執(zhí)行的任務(wù)
    在workflow:rules不允許的流水線(xiàn)

only/except

only/except 是規(guī)定當(dāng)前job的可見(jiàn)狀態(tài),一個(gè)項(xiàng)目有很多分支,tag,我們的流水線(xiàn),為了對(duì)特定的分支,特定的tag執(zhí)行不同的job,這里就要使用only和except

在任務(wù)上加上這一段代碼,就表明當(dāng)前任務(wù)只有在master分支可以運(yùn)行

only:- master

也可以根據(jù)當(dāng)前的代碼變動(dòng)是合并,還是推送,還是使用API來(lái)觸發(fā)的。如果一個(gè)任務(wù)沒(méi)有only屬性,那默認(rèn)就是

only: ['branches', 'tags'] 操作分支或者tags都會(huì)觸發(fā)流水線(xiàn)。

dependencies

dependencies關(guān)鍵詞是定義特定的job運(yùn)行規(guī)則。默認(rèn)artifacts是從當(dāng)前階段產(chǎn)生,在后續(xù)的階段都會(huì)被下載,但我們可以使用dependencies關(guān)鍵詞來(lái)控制artifacts從哪里下載

build:osx:stage: buildscript: make build:osxartifacts:paths:- binaries/build:linux:stage: buildscript: make build:linuxartifacts:paths:- binaries/test:osx:stage: testscript: make test:osxdependencies:- build:osx #依賴(lài)test:linux:stage: testscript: make test:linuxdependencies:- build:linux #依賴(lài)deploy:stage: deployscript: make deploy

任務(wù)test:osx 依賴(lài)build:osx。任務(wù)test:linux 依賴(lài) build:linux。這樣配置以后 任務(wù)test:linux 就不用等任務(wù)build:osx 執(zhí)行完成在執(zhí)行了,只需要等待任務(wù)build:linux完成

extends

這個(gè)關(guān)鍵詞可以使一個(gè)任務(wù)繼承另一個(gè)任務(wù)。

.tests:script: rake teststage: testonly:refs:- branchesrspec:extends: .testsscript: rake rspeconly:variables:- $RSPEC

任務(wù)rspec 繼承了.tests任務(wù),在流水線(xiàn)中.tests是一個(gè)隱藏的任務(wù),在流水線(xiàn)中,以點(diǎn)(.) 開(kāi)頭的任務(wù)名,都是隱藏的任務(wù)。不會(huì)被執(zhí)行。 被rspec 繼承后,相同的key會(huì)以rspec為準(zhǔn),rspec沒(méi)有的,而.tests有的,則合并到rspec中。
合并后的結(jié)果是:

rspec:script: rake rspecstage: testonly:refs:- branchesvariables:- $RSPEC

使用這一個(gè)手段,可以寫(xiě)一個(gè)模板,只要稍微改改就能后使用。非常適合大批量編寫(xiě)流水線(xiàn)。

trigger

trigger 是應(yīng)對(duì)那些更加復(fù)雜的CI/CD流程,如多流水線(xiàn),父子流水線(xiàn)。使用它可以定義一個(gè)下游的流水線(xiàn),配置了trigger的任務(wù)是不能跑腳本的,就是說(shuō)不能定義script, before_script, 和 after_script.

#流水線(xiàn)執(zhí)行完test任務(wù)后就會(huì)去執(zhí)行my/deployment項(xiàng)目的流水線(xiàn) rspec:stage: testscript: bundle exec rspecstaging:stage: deploytrigger: my/deployment --- #配置下游流水線(xiàn)式也可以執(zhí)行分支 rspec:stage: testscript: bundle exec rspecstaging:stage: deploytrigger:project: my/deploymentbranch: stablez

rules

rules是用于規(guī)定任務(wù)的執(zhí)行規(guī)則,使用一個(gè)表達(dá)式,來(lái)規(guī)范那些任務(wù)執(zhí)行,那些任務(wù)不執(zhí)行.還可以在任務(wù)成功,或者失敗后,觸發(fā)另一個(gè)任務(wù)。

docker build:script: docker build -t my-image:$CI_COMMIT_REF_SLUG .rules: #如果當(dāng)前的分支是master分支則任務(wù)延遲3個(gè)小時(shí)執(zhí)行,并且允許失敗。- if: '$CI_COMMIT_BRANCH == "master"'when: delayedstart_in: '3 hours'allow_failure: true

rules的下面可選屬性

  • if:使用if表達(dá)式 添加或移除一個(gè)任務(wù), 類(lèi)似 only:variables.
  • changes:根據(jù)某些個(gè)文件是否改變來(lái)追加或移除一些任務(wù)。類(lèi)似 only:changes.
  • exists :根據(jù)是否存在特定文件來(lái)追加或移除一些任務(wù)

allow_failure

allow_failure是一個(gè)布爾類(lèi)型, true或false, 默認(rèn)為false,表示當(dāng)前任務(wù)是否允許失敗。如果一個(gè)任務(wù)設(shè)置了allow_failure: true,并且這個(gè)任務(wù)報(bào)錯(cuò)了,那么它將會(huì)顯示黃色警告。但有種情況任務(wù)失敗了也會(huì)停止的, 那就是任務(wù)設(shè)置了when: manual,即手動(dòng)操作的任務(wù)。手動(dòng)啟動(dòng)的任務(wù),報(bào)錯(cuò)了就會(huì)停止,不會(huì)繼續(xù)執(zhí)行后續(xù)任務(wù),除非在rule設(shè)置報(bào)錯(cuò)的處理邏輯。

coverage

coverage 是用于獲取項(xiàng)目的代碼覆蓋率,這個(gè)配置項(xiàng)的值只能是一個(gè)正則表達(dá)式,官方有提供一些,在CI/CD的General pipelines里
覆蓋率可以添加到項(xiàng)目的readme上。

pages

pages是一項(xiàng)特殊的工作,用于將靜態(tài)內(nèi)容上傳到GitLab,可用于為您的網(wǎng)站提供服務(wù),其實(shí)就是可以托管你的網(wǎng)站。它具有特殊的語(yǔ)法,因此必須滿(mǎn)足以下兩個(gè)要求:

  • 任何靜態(tài)內(nèi)容都必須放在public/目錄下。
  • 制品artifacts必須是目錄public/,就是編譯后的文件必須存放在public中

下面的示例將所有文件從項(xiàng)目的根目錄移至public/目錄。這里必須先創(chuàng)建一個(gè).public 目錄,防止根目錄下已經(jīng)存在public了,導(dǎo)致循環(huán)復(fù)制。

pages:stage: deployscript:- mkdir .public- cp -r * .public- mv .public publicartifacts:paths:- publiconly:- master

release

release關(guān)鍵詞是用于創(chuàng)建一個(gè)release,即創(chuàng)建一個(gè)發(fā)布,可以配置這些內(nèi)容

  • tag_name: tag 名稱(chēng)
  • description: 描述
  • name :名稱(chēng)
  • ref :提交的hash值
  • milestones: 要關(guān)聯(lián)的里程碑
  • released_at: 創(chuàng)建時(shí)間

resource_group

有時(shí)在環(huán)境中同時(shí)運(yùn)行多個(gè)作業(yè)或流水線(xiàn)時(shí)可能會(huì)導(dǎo)致在部署過(guò)程中出錯(cuò)。為了避免這些錯(cuò)誤,resource_group可以使用該屬性來(lái)確保運(yùn)行程序不會(huì)同時(shí)運(yùn)行某些任務(wù)。資源組的行為類(lèi)似于其他編程語(yǔ)言中的信號(hào)燈。

當(dāng)一個(gè)任務(wù)設(shè)置了resource_group , 同一項(xiàng)目不同管道之間任務(wù)的運(yùn)行是互斥的。如果屬于同一資源組的多個(gè)任務(wù)同時(shí)進(jìn)入隊(duì)列,則運(yùn)行程序僅選擇其中一個(gè)作業(yè)。其他作業(yè)將等到 resource_group釋放。

deploy-to-production:script: deployresource_group: production

在這種情況下,兩個(gè)deploy-to-production單獨(dú)流水線(xiàn)中的兩個(gè)作業(yè)永遠(yuǎn)無(wú)法同時(shí)運(yùn)行。最后的結(jié)果及時(shí)你可以確保永遠(yuǎn)不會(huì)在生產(chǎn)環(huán)境中發(fā)生并發(fā)部署。

您可以為每個(gè)環(huán)境定義多個(gè)資源組。例如,當(dāng)部署到物理設(shè)備時(shí),您可能有多個(gè)物理設(shè)備。可以將每個(gè)設(shè)備部署到,但是在任何給定時(shí)間每個(gè)設(shè)備只能部署一個(gè)。

resource_group值只能包含字母,數(shù)字,-, _, /, $, {, }, .,和空格。它不能以開(kāi)頭或結(jié)尾/。

retry

retry可以設(shè)置一個(gè)任務(wù)的重試次數(shù),值的類(lèi)型是數(shù)字 最大是2,如果設(shè)置2,就表明該任務(wù)最多可以執(zhí)行3次,其中包括2次重試。對(duì)于網(wǎng)絡(luò)不穩(wěn)定的部署,非常有用。

test:script: rspecretry: 2

timeout

timeout是用于設(shè)置一個(gè)任務(wù)的超時(shí)時(shí)間

build:script: build.shtimeout: 3 hours 30 minutestest:script: rspectimeout: 3h 30m

參考

原文鏈接:https://blog.csdn.net/github_35631540/article/details/111029151

job keywords:https://docs.gitlab.com/ee/ci/yaml/#job-keywords

rules:https://docs.gitlab.com/ee/ci/yaml/#rules

https://docs.gitlab.com/11.11/ee/ci/yaml/README.html#when

變量:https://docs.gitlab.com/ee/ci/variables/README.html

yaml:http://192.168.1.X/help/ci/yaml/README.md

gitlabci變量

declare -x CI="true" declare -x CI_BUILDS_DIR="/builds" declare -x CI_BUILD_BEFORE_SHA="fc8c91ed7cb8f5eefe540786d48f9babd0b48155" declare -x CI_BUILD_ID="49" declare -x CI_BUILD_NAME="mvn_build_job" declare -x CI_BUILD_REF="e880f7c22eca83ac59eb263af097758a7a7f4f4a" declare -x CI_BUILD_REF_NAME="master" declare -x CI_BUILD_REF_SLUG="master" declare -x CI_BUILD_STAGE="package" declare -x CI_BUILD_TOKEN="xxxxxxxxxxxxxxxxxxxx" declare -x CI_COMMIT_DESCRIPTION="" declare -x CI_COMMIT_MESSAGE="更新 .gitlab-ci.yml" declare -x CI_COMMIT_REF_NAME="master" declare -x CI_COMMIT_REF_SLUG="master" declare -x CI_COMMIT_SHA="e880f7c22eca83ac59eb263af097758a7a7f4f4a" declare -x CI_COMMIT_TITLE="更新 .gitlab-ci.yml" declare -x CI_CONCURRENT_ID="1" declare -x CI_CONCURRENT_PROJECT_ID="1" declare -x CI_CONFIG_PATH=".gitlab-ci.yml" declare -x CI_DISPOSABLE_ENVIRONMENT="true" declare -x CI_JOB_ID="49" declare -x CI_JOB_IMAGE="XXXXX/maven:3.6.3-openjdk-8" declare -x CI_JOB_NAME="mvn_build_job" declare -x CI_JOB_STAGE="package" declare -x CI_JOB_STATUS="running" declare -x CI_JOB_TOKEN="xxxxxxxxxxxxxxxxxxxx" declare -x CI_JOB_URL="http://xxxxx/base/demo-sample/-/jobs/49" declare -x CI_PIPELINE_ID="22" declare -x CI_PIPELINE_IID="11" declare -x CI_PIPELINE_SOURCE="push" declare -x CI_PIPELINE_URL="http://xxxxx/base/demo-sample/pipelines/22" declare -x CI_PROJECT_DIR="/builds/base/demo-sample" declare -x CI_PROJECT_ID="226" declare -x CI_PROJECT_NAME="demo-sample" declare -x CI_PROJECT_NAMESPACE="base" declare -x CI_PROJECT_PATH="base/demo-sample" declare -x CI_PROJECT_PATH_SLUG="base-demo-sample" declare -x CI_PROJECT_URL="http://xxxxx/base/demo-sample" declare -x CI_PROJECT_VISIBILITY="private" declare -x CI_REGISTRY_PASSWORD="xxxxxxxxxxxxxxxxxxxx" declare -x CI_REGISTRY_USER="gitlab-ci-token" declare -x CI_REPOSITORY_URL="http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@xxxxx/base/demo-sample.git" declare -x CI_RUNNER_DESCRIPTION="gitlab-runner-gitlab-runner-79fdddc45c-tznlg" declare -x CI_RUNNER_EXECUTABLE_ARCH="linux/amd64" declare -x CI_RUNNER_ID="4" declare -x CI_RUNNER_REVISION="54944146" declare -x CI_RUNNER_SHORT_TOKEN="32d85b65" declare -x CI_RUNNER_TAGS="" declare -x CI_RUNNER_VERSION="13.10.0" declare -x CI_SERVER="yes" declare -x CI_SERVER_NAME="GitLab" declare -x CI_SERVER_REVISION="63daf37" declare -x CI_SERVER_VERSION="11.1.4" declare -x DOCKER_TAG="1.0.0-RELEASE" declare -x FF_CMD_DISABLE_DELAYED_ERROR_LEVEL_EXPANSION="false" declare -x FF_DISABLE_UMASK_FOR_DOCKER_EXECUTOR="false" declare -x FF_ENABLE_BASH_EXIT_CODE_CHECK="false" declare -x FF_GITLAB_REGISTRY_HELPER_IMAGE="false" declare -x FF_NETWORK_PER_BUILD="false" declare -x FF_RESET_HELPER_IMAGE_ENTRYPOINT="true" declare -x FF_SHELL_EXECUTOR_USE_LEGACY_PROCESS_KILL="false" declare -x FF_SKIP_NOOP_BUILD_STAGES="true" declare -x FF_USE_DIRECT_DOWNLOAD="true" declare -x FF_USE_FASTZIP="false" declare -x FF_USE_GO_CLOUD_WITH_CACHE_ARCHIVER="true" declare -x FF_USE_LEGACY_KUBERNETES_EXECUTION_STRATEGY="true" declare -x FF_USE_WINDOWS_LEGACY_PROCESS_STRATEGY="true" declare -x GITLAB_CI="true" declare -x GITLAB_FEATURES="" declare -x GITLAB_USER_EMAIL="xxxx@jurxxxx.com.cn" declare -x GITLAB_USER_ID="59" declare -x GITLAB_USER_LOGIN="lixx.jurxxx" declare -x GITLAB_USER_NAME="xxxx" declare -x HOME="/root" declare -x HOSTNAME="runner-32d85b65-project-226-concurrent-1f625l" declare -x JAVA_HOME="/usr/local/openjdk-8" declare -x JAVA_VERSION="8u275" declare -x KUBERNETES_PORT="tcp://10.233.0.1:443" declare -x KUBERNETES_PORT_443_TCP="tcp://10.233.0.1:443" declare -x KUBERNETES_PORT_443_TCP_ADDR="10.233.0.1" declare -x KUBERNETES_PORT_443_TCP_PORT="443" declare -x KUBERNETES_PORT_443_TCP_PROTO="tcp" declare -x KUBERNETES_SERVICE_HOST="10.233.0.1" declare -x KUBERNETES_SERVICE_PORT="443" declare -x KUBERNETES_SERVICE_PORT_HTTPS="443" declare -x LANG="C.UTF-8" declare -x MAVEN_CONFIG="/root/.m2" declare -x MAVEN_HOME="/usr/share/maven" declare -x MINIO_PORT="tcp://10.233.36.231:9000" declare -x MINIO_PORT_9000_TCP="tcp://10.233.36.231:9000" declare -x MINIO_PORT_9000_TCP_ADDR="10.233.36.231" declare -x MINIO_PORT_9000_TCP_PORT="9000" declare -x MINIO_PORT_9000_TCP_PROTO="tcp" declare -x MINIO_SERVICE_HOST="10.233.36.231" declare -x MINIO_SERVICE_PORT="9000" declare -x MINIO_SERVICE_PORT_9000_TCP="9000" declare -x OLDPWD="/" declare -x PATH="/usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" declare -x PWD="/builds/base/sample" declare -x SHLVL="1"

總結(jié)

以上是生活随笔為你收集整理的gitlab-ci详细说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。