gitlab-ci详细说明
文章目錄
- 概念
- Pipeline
- Stages
- Jobs
- job分組
- variables
- 自定義變量
- 預定義變量
- 使用變量
- 關鍵詞
- 全局關鍵詞
- before_script
- after_script
- include
- Stage關鍵詞
- Job關鍵詞
- 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 開始,Gitlab CI 就已經集成在 Gitlab 中,我們只要在項目中添加一個.gitlab-ci.yml文件,然后添加一個Runner,即可進行持續集成。
Pipeline
一次 Pipeline 其實相當于一次構建任務,里面可以包含很多個流程,如安裝依賴、運行測試、編譯、部署測試服務器、部署生產服務器等流程。任何提交或者 Merge Request 的合并都可以觸發 Pipeline 構建。
Stages
Stages 表示一個構建階段,也就是上面提到的一個流程。我們可以在一次 Pipeline 中定義多個 Stages,這些 Stages 會有以下特點:
- 所有 Stages 會按照順序運行,即當一個 Stage 完成后,下一個 Stage 才會開始
- 只有當所有 Stages 完成后,該構建任務 (Pipeline) 才會成功
- 如果任何一個 Stage 失敗,那么后面的 Stages 不會執行,該構建任務 (Pipeline) 失敗
Jobs
Jobs 表示構建工作,表示某個 Stage 里面執行的工作。我們可以在 Stages 里面定義多個 Jobs,這些 Jobs 會有以下特點:
- 相同 Stage 中的 Jobs 會并行執行
- 相同 Stage 中的 Jobs 都執行成功時,該 Stage 才會成功
- 如果任何一個 Job 失敗,那么該 Stage 失敗,即該構建任務 (Pipeline) 失敗
job分組
可以把job分組,通過 以下方式,job名稱為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預定義的變量。
參考:https://docs.gitlab.com/ee/ci/variables/#variables
自定義變量
自定義變量可以通過以下語句定義:
variables:TEST: "HELLO WORLD"預定義變量
There are also Kubernetes-specific deployment variables.
| CHAT_CHANNEL | 10.6 | all | The Source chat channel that triggered the ChatOps command. |
| CHAT_INPUT | 10.6 | all | The additional arguments passed with the ChatOps command. |
| CI | all | 0.4 | Available for all jobs executed in CI/CD. true when available. |
| CI_API_V4_URL | 11.7 | all | The GitLab API v4 root URL. |
| CI_BUILDS_DIR | all | 11.10 | The top-level directory where builds are executed. |
| CI_COMMIT_BEFORE_SHA | 11.2 | all | The previous latest commit present on a branch. Is always 0000000000000000000000000000000000000000 in pipelines for merge requests. |
| CI_COMMIT_BRANCH | 12.6 | 0.5 | The commit branch name. Available in branch pipelines, including pipelines for the default branch. Not available in merge request pipelines or tag pipelines. |
| CI_COMMIT_DESCRIPTION | 10.8 | all | The description of the commit. If the title is shorter than 100 characters, the message without the first line. |
| CI_COMMIT_MESSAGE | 10.8 | all | 提交message。 |
| CI_COMMIT_REF_NAME | 9.0 | all | branch or tag名稱 |
| CI_COMMIT_REF_PROTECTED | 11.11 | all | true if the job is running for a protected reference. |
| CI_COMMIT_REF_SLUG | 9.0 | all | CI_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_SHA | 9.0 | all | 提交version |
| CI_COMMIT_SHORT_SHA | 11.7 | all | CI_COMMIT_SHA 前8字符 |
| CI_COMMIT_TAG | 9.0 | 0.5 | The commit tag name. Available only in pipelines for tags. |
| CI_COMMIT_TIMESTAMP | 13.4 | all | The timestamp of the commit in the ISO 8601 format. |
| CI_COMMIT_TITLE | 10.8 | all | The title of the commit. The full first line of the message. |
| CI_CONCURRENT_ID | all | 11.10 | The unique ID of build execution in a single executor. |
| CI_CONCURRENT_PROJECT_ID | all | 11.10 | The unique ID of build execution in a single executor and project. |
| CI_CONFIG_PATH | 9.4 | 0.5 | The path to the CI/CD configuration file. Defaults to .gitlab-ci.yml. |
| CI_DEBUG_TRACE | all | 1.7 | true if debug logging (tracing) is enabled. |
| CI_DEFAULT_BRANCH | 12.4 | all | The name of the project’s default branch. |
| CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX | 13.7 | all | The image prefix for pulling images through the Dependency Proxy. |
| CI_DEPENDENCY_PROXY_PASSWORD | 13.7 | all | The password to pull images through the Dependency Proxy. |
| CI_DEPENDENCY_PROXY_SERVER | 13.7 | all | The server for logging in to the Dependency Proxy. This is equivalent to $CI_SERVER_HOST:$CI_SERVER_PORT. |
| CI_DEPENDENCY_PROXY_USER | 13.7 | all | The username to pull images through the Dependency Proxy. |
| CI_DEPLOY_FREEZE | 13.2 | all | Only available if the pipeline runs during a deploy freeze window. true when available. |
| CI_DEPLOY_PASSWORD | 10.8 | all | The authentication password of the GitLab Deploy Token, if the project has one. |
| CI_DEPLOY_USER | 10.8 | all | The authentication username of the GitLab Deploy Token, if the project has one. |
| CI_DISPOSABLE_ENVIRONMENT | all | 10.1 | Only 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_NAME | 8.15 | all | The name of the environment for this job. Available if environment:name is set. |
| CI_ENVIRONMENT_SLUG | 8.15 | all | The 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_URL | 9.3 | all | The URL of the environment for this job. Available if environment:url is set. |
| CI_HAS_OPEN_REQUIREMENTS | 13.1 | all | Only available if the pipeline’s project has an open requirement. true when available. |
| CI_JOB_ID | 9.0 | all | job id |
| CI_JOB_IMAGE | 12.9 | 12.9 | 運行job的容器的image |
| CI_JOB_JWT | 12.10 | all | A RS256 JSON web token to authenticate with third party systems that support JWT authentication, for example HashiCorp’s Vault. |
| CI_JOB_MANUAL | 8.12 | all | job是否是人工執行的 |
| CI_JOB_NAME | 8.12 | all | job名稱 |
| CI_JOB_STAGE | 9.0 | 0.5 | The name of the job’s stage. |
| CI_JOB_STATUS | all | 13.5 | The status of the job as each runner stage is executed. Use with after_script. Can be success, failed, or canceled. |
| CI_JOB_TOKEN | 9.0 | 1.2 | A token to authenticate with certain API endpoints. The token is valid as long as the job is running. |
| CI_JOB_URL | 11.1 | 0.5 | The job details URL. |
| CI_JOB_STARTED_AT | 13.10 | all | The UTC datetime when a job started, in ISO 8601 format. |
| CI_KUBERNETES_ACTIVE | 13.0 | all | Only available if the pipeline has a Kubernetes cluster available for deployments. true when available. |
| CI_NODE_INDEX | 11.5 | all | The index of the job in the job set. Only available if the job uses parallel. |
| CI_NODE_TOTAL | 11.5 | all | The total number of instances of this job running in parallel. Set to 1 if the job does not use parallel. |
| CI_OPEN_MERGE_REQUESTS | 13.8 | all | A 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_DOMAIN | 11.8 | all | The configured domain that hosts GitLab Pages. |
| CI_PAGES_URL | 11.8 | all | The URL for a GitLab Pages site. Always a subdomain of CI_PAGES_DOMAIN. |
| CI_PIPELINE_ID | 8.10 | all | 流水線ID,gitlab實例內唯一 |
| CI_PIPELINE_IID | 11.0 | all | 流水線ID,項目內唯一 |
| CI_PIPELINE_SOURCE | 10.0 | all | How 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_TRIGGERED | all | all | true if the job was triggered. |
| CI_PIPELINE_URL | 11.1 | 0.5 | The URL for the pipeline details. |
| CI_PIPELINE_CREATED_AT | 13.10 | all | The UTC datetime when the pipeline was created, in ISO 8601 format. |
| CI_PROJECT_CONFIG_PATH | 13.8 | all | (Deprecated) The CI configuration path for the project. Deprecated in GitLab 13.10. Removal planned for GitLab 14.0. |
| CI_PROJECT_DIR | all | all | The 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_ID | all | all | The ID of the current project. This ID is unique across all projects on the GitLab instance. |
| CI_PROJECT_NAME | 8.10 | 0.5 | The 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_NAMESPACE | 8.10 | 0.5 | The project namespace (username or group name) of the job. |
| CI_PROJECT_PATH_SLUG | 9.3 | all | $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_PATH | 8.10 | 0.5 | The project namespace with the project name included. |
| CI_PROJECT_REPOSITORY_LANGUAGES | 12.3 | all | A comma-separated, lowercase list of the languages used in the repository. For example ruby,javascript,html,css. |
| CI_PROJECT_ROOT_NAMESPACE | 13.2 | 0.5 | The 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_TITLE | 12.4 | all | The human-readable project name as displayed in the GitLab web interface. |
| CI_PROJECT_URL | 8.10 | 0.5 | The HTTP(S) address of the project. |
| CI_PROJECT_VISIBILITY | 10.3 | all | The project visibility. Can be internal, private, or public. |
| CI_REGISTRY_IMAGE | 8.10 | 0.5 | The address of the project’s Container Registry. Only available if the Container Registry is enabled for the project. |
| CI_REGISTRY_PASSWORD | 9.0 | all | The password to push containers to the project’s GitLab Container Registry. Only available if the Container Registry is enabled for the project. |
| CI_REGISTRY_USER | 9.0 | all | The username to push containers to the project’s GitLab Container Registry. Only available if the Container Registry is enabled for the project. |
| CI_REGISTRY | 8.10 | 0.5 | The 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_URL | 9.0 | all | The URL to clone the Git repository. |
| CI_RUNNER_DESCRIPTION | 8.10 | 0.5 | The description of the runner. |
| CI_RUNNER_EXECUTABLE_ARCH | all | 10.6 | The OS/architecture of the GitLab Runner executable. Might not be the same as the environment of the executor. |
| CI_RUNNER_ID | 8.10 | 0.5 | The unique ID of the runner being used. |
| CI_RUNNER_REVISION | all | 10.6 | The revision of the runner running the job. |
| CI_RUNNER_SHORT_TOKEN | all | 12.3 | First eight characters of the runner’s token used to authenticate new job requests. Used as the runner’s unique ID. |
| CI_RUNNER_TAGS | 8.10 | 0.5 | A comma-separated list of the runner tags. |
| CI_RUNNER_VERSION | all | 10.6 | The version of the GitLab Runner running the job. |
| CI_SERVER_HOST | 12.1 | all | The host of the GitLab instance URL, without protocol or port. For example gitlab.example.com. |
| CI_SERVER_NAME | all | all | The name of CI/CD server that coordinates jobs. |
| CI_SERVER_PORT | 12.8 | all | The port of the GitLab instance URL, without host or protocol. For example 8080. |
| CI_SERVER_PROTOCOL | 12.8 | all | The protocol of the GitLab instance URL, without host or port. For example https. |
| CI_SERVER_REVISION | all | all | GitLab revision that schedules jobs. |
| CI_SERVER_URL | 12.7 | all | The base URL of the GitLab instance, including protocol and port. For example https://gitlab.example.com:8080. |
| CI_SERVER_VERSION_MAJOR | 11.4 | all | The 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_MINOR | 11.4 | all | The 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_PATCH | 11.4 | all | The 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_VERSION | all | all | The full version of the GitLab instance. |
| CI_SERVER | all | all | Available for all jobs executed in CI/CD. yes when available. |
| CI_SHARED_ENVIRONMENT | all | 10.1 | Only 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_CI | all | all | Available for all jobs executed in CI/CD. true when available. |
| GITLAB_FEATURES | 10.6 | all | The comma-separated list of licensed features available for the GitLab instance and license. |
| GITLAB_USER_EMAIL | 8.12 | all | The email of the user who started the job. |
| GITLAB_USER_ID | 8.12 | all | The ID of the user who started the job. |
| GITLAB_USER_LOGIN | 10.0 | all | The username of the user who started the job. |
| GITLAB_USER_NAME | 10.0 | all | The name of the user who started the job. |
| TRIGGER_PAYLOAD | 13.9 | all | The webhook payload. Only available when a pipeline is triggered with a webhook. |
使用變量
| 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. |
變量可以通過依賴job 來繼承。
關鍵詞
全局關鍵詞
before_script
before_script 關鍵詞是用于在每個任務之前執行的腳本,但是會在artifacts恢復之后執行。你可以這樣定義一個全局的before_script,
default:before_script:- echo "Execute this script in all jobs that don't already have a before_script section."也可以在一個任務中中單獨定義
job:before_script:- echo "Execute this script instead of the global before_script."script:- echo "This script executes after the job's `before_script`"任務中的before_script會覆蓋全局的before_script
after_script
after_script與before_script類似,用于定義多行腳本,會在任務執行完成后執行,即使任務失敗也會被執行。如果任務被取消或者超時,after_script就不會被執行了,可以定義全局的,也可以定義局部的
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可以導入一個或多個額外的yaml文件到CI/CD配置里,這就可以將一個很長的流水線,分隔出來。使用include來引入。
也可以將幾個流水線中相同的配置,提取出來,公用。引入的文件擴展名 必須是.yaml或者.yml兩種,其他的不行。
include 關鍵詞下,有四個可選性:
- local, 引入一個當前項目的文件
- file, 引入一個不同項目的文件
- remote, 引入一個公網文件,
- template, 引入一個由GitLab提供的模板
Stage關鍵詞
Job關鍵詞
script
任務要執行的shell腳本內容,內容會被runner執行,在這里,你不需要使用git clone …克隆當前的項目,來進行操作,因為在流水線中,每一個的job的執行都會將項目下載,恢復緩存這些流程,不需要你再使用腳本恢復。你只需要在這里寫你的項目安裝,編譯執行,如
npm install 另外值得一提的是,腳本的工作目錄就是當前項目的根目錄,所有可以就像在本地開發一樣。此外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***是一個job的必填內容,不可或缺。一個job最少有二個屬性,一個是 job name, 任務名稱, 一個就是script。
?
image
指定一個基礎Docker鏡像作為基礎運行環境,經常用到的鏡像有node, nginx ,docker
job:image: node:latestscript: npm installimage的作用就是給當前任務或者當前流水線設置一個基礎環境,有可能是nodejs,也有可能是java, go, php,可以設置當前流水線的,也可以設置當前任務的。
artifacts
制品,一個成品,作用是將流水線過程中的一些文件,文件夾,打包壓縮,提供一個外鏈供人下載,另外還能在后續的job中緩存。
比如我們構建一個前端項目后將dist目錄做成一個壓縮包,
后面的任務可以使用生成的制品。
paths 是用來指定將哪些文件,目錄放入制品中, 也可以使用exclude關鍵詞,將那些目錄,文件排除,支持正則表達式。
此外還有以下幾個屬性:
- paths :文件路徑
- exclude: 排除的文件
- name :制品名稱
- expose_as: 在UI頁面導出的名稱
- untracked :布爾類型,是否將git忽略的文件加到制品中
- when :何時上傳制品,可選值:on_success;on_failure;always
- expire_in: 過期時間默認30天
- reports: 收集測試報告
tags
tags 關鍵詞是用于指定Runner。tags的取值范圍是在該項目可見的runner tags中,可以在Setting =>CI/CD => Runner 中查看的到。不設置則默認使用公有Runner去執行流水線。每個任務可以指定一個Runner,可以指定多個標簽,但runner卻只能一個。以一個為準。tags是在注冊Runner時配置的,后續也可以更改。
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.orgcache
緩存是將當前工作環境目錄中的一些文件,一些文件夾存儲起來,用于在各個任務初始化的時候恢復。避免多個下載同樣的包,能夠大大優化流水線效率。在前端項目中,我們經常把node_modules緩存起來,這樣一條流水線都可以使用這些下載好的包。在java項目中經常把maven下載的包緩存起來。以備后用
cache下的參數有
- paths:當前工作環境下的目錄
- key:存儲的key,key不變不會重新生成緩存,
- prefix:使用一些文件制作成文件hash值,當做key的一部分,
- untracked :是否緩存git忽略的文件
- when :定義何時存儲緩存,值 on_success;on_failure;always
- policy:緩存是否要在job完成后重新上傳
緩存可以設置流水線全局,也可以在job中設置
cache:key: hello-vue-cachepaths:- node_modules##### cache:key:files:- Gemfile.lock- package.jsonpaths:- vendor/ruby- node_modulesstage
stage 是階段的意思,用于歸檔一部分的job,按照定義的stage順序來執行。默認的stage有build,test,deploy, 此外還有兩個特殊的.pre 和 .post。以下執行順序: job_1,job_0。
stages:- build- test- deployjob_0:stage: testscript: echo 'tets'job_1:stage: buildscript: echo 'build'when
when關鍵字是實現在發生故障或盡管發生故障時仍能運行的作業。比如你要在任務失敗后需要觸發一個job,
或者你需要手動執行任務,或者當你一個任務執行成功后,執行另一個任務.
- on_success 所有任務執行成功后
- on_failure 當至少一個任務失敗后
- always 執行作業,而不考慮作業在早期階段的狀態。
- manual 手動執行任務
- delayed 延遲執行任務
- never
在rules中不排除執行的任務
在workflow:rules不允許的流水線
only/except
only/except 是規定當前job的可見狀態,一個項目有很多分支,tag,我們的流水線,為了對特定的分支,特定的tag執行不同的job,這里就要使用only和except
在任務上加上這一段代碼,就表明當前任務只有在master分支可以運行
only:- master也可以根據當前的代碼變動是合并,還是推送,還是使用API來觸發的。如果一個任務沒有only屬性,那默認就是
only: ['branches', 'tags'] 操作分支或者tags都會觸發流水線。dependencies
dependencies關鍵詞是定義特定的job運行規則。默認artifacts是從當前階段產生,在后續的階段都會被下載,但我們可以使用dependencies關鍵詞來控制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 #依賴test:linux:stage: testscript: make test:linuxdependencies:- build:linux #依賴deploy:stage: deployscript: make deploy任務test:osx 依賴build:osx。任務test:linux 依賴 build:linux。這樣配置以后 任務test:linux 就不用等任務build:osx 執行完成在執行了,只需要等待任務build:linux完成
extends
這個關鍵詞可以使一個任務繼承另一個任務。
.tests:script: rake teststage: testonly:refs:- branchesrspec:extends: .testsscript: rake rspeconly:variables:- $RSPEC任務rspec 繼承了.tests任務,在流水線中.tests是一個隱藏的任務,在流水線中,以點(.) 開頭的任務名,都是隱藏的任務。不會被執行。 被rspec 繼承后,相同的key會以rspec為準,rspec沒有的,而.tests有的,則合并到rspec中。
合并后的結果是:
使用這一個手段,可以寫一個模板,只要稍微改改就能后使用。非常適合大批量編寫流水線。
trigger
trigger 是應對那些更加復雜的CI/CD流程,如多流水線,父子流水線。使用它可以定義一個下游的流水線,配置了trigger的任務是不能跑腳本的,就是說不能定義script, before_script, 和 after_script.
#流水線執行完test任務后就會去執行my/deployment項目的流水線 rspec:stage: testscript: bundle exec rspecstaging:stage: deploytrigger: my/deployment --- #配置下游流水線式也可以執行分支 rspec:stage: testscript: bundle exec rspecstaging:stage: deploytrigger:project: my/deploymentbranch: stablezrules
rules是用于規定任務的執行規則,使用一個表達式,來規范那些任務執行,那些任務不執行.還可以在任務成功,或者失敗后,觸發另一個任務。
docker build:script: docker build -t my-image:$CI_COMMIT_REF_SLUG .rules: #如果當前的分支是master分支則任務延遲3個小時執行,并且允許失敗。- if: '$CI_COMMIT_BRANCH == "master"'when: delayedstart_in: '3 hours'allow_failure: truerules的下面可選屬性
- if:使用if表達式 添加或移除一個任務, 類似 only:variables.
- changes:根據某些個文件是否改變來追加或移除一些任務。類似 only:changes.
- exists :根據是否存在特定文件來追加或移除一些任務
allow_failure
allow_failure是一個布爾類型, true或false, 默認為false,表示當前任務是否允許失敗。如果一個任務設置了allow_failure: true,并且這個任務報錯了,那么它將會顯示黃色警告。但有種情況任務失敗了也會停止的, 那就是任務設置了when: manual,即手動操作的任務。手動啟動的任務,報錯了就會停止,不會繼續執行后續任務,除非在rule設置報錯的處理邏輯。
coverage
coverage 是用于獲取項目的代碼覆蓋率,這個配置項的值只能是一個正則表達式,官方有提供一些,在CI/CD的General pipelines里
覆蓋率可以添加到項目的readme上。
pages
pages是一項特殊的工作,用于將靜態內容上傳到GitLab,可用于為您的網站提供服務,其實就是可以托管你的網站。它具有特殊的語法,因此必須滿足以下兩個要求:
- 任何靜態內容都必須放在public/目錄下。
- 制品artifacts必須是目錄public/,就是編譯后的文件必須存放在public中
下面的示例將所有文件從項目的根目錄移至public/目錄。這里必須先創建一個.public 目錄,防止根目錄下已經存在public了,導致循環復制。
pages:stage: deployscript:- mkdir .public- cp -r * .public- mv .public publicartifacts:paths:- publiconly:- masterrelease
release關鍵詞是用于創建一個release,即創建一個發布,可以配置這些內容
- tag_name: tag 名稱
- description: 描述
- name :名稱
- ref :提交的hash值
- milestones: 要關聯的里程碑
- released_at: 創建時間
resource_group
有時在環境中同時運行多個作業或流水線時可能會導致在部署過程中出錯。為了避免這些錯誤,resource_group可以使用該屬性來確保運行程序不會同時運行某些任務。資源組的行為類似于其他編程語言中的信號燈。
當一個任務設置了resource_group , 同一項目的不同管道之間任務的運行是互斥的。如果屬于同一資源組的多個任務同時進入隊列,則運行程序僅選擇其中一個作業。其他作業將等到 resource_group釋放。
deploy-to-production:script: deployresource_group: production在這種情況下,兩個deploy-to-production單獨流水線中的兩個作業永遠無法同時運行。最后的結果及時你可以確保永遠不會在生產環境中發生并發部署。
您可以為每個環境定義多個資源組。例如,當部署到物理設備時,您可能有多個物理設備。可以將每個設備部署到,但是在任何給定時間每個設備只能部署一個。
resource_group值只能包含字母,數字,-, _, /, $, {, }, .,和空格。它不能以開頭或結尾/。
retry
retry可以設置一個任務的重試次數,值的類型是數字 最大是2,如果設置2,就表明該任務最多可以執行3次,其中包括2次重試。對于網絡不穩定的部署,非常有用。
test:script: rspecretry: 2timeout
timeout是用于設置一個任務的超時時間
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"總結
以上是生活随笔為你收集整理的gitlab-ci详细说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ncat详细介绍
- 下一篇: Mockito详细介绍