{"id":31190,"date":"2022-09-07T06:10:48","date_gmt":"2022-09-07T06:10:48","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31190"},"modified":"2022-12-23T05:47:28","modified_gmt":"2022-12-23T05:47:28","slug":"gitlab-tutorials-example-of-gitlab-ci-yml","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/gitlab-tutorials-example-of-gitlab-ci-yml\/","title":{"rendered":"GitLab Tutorials: Example of .gitlab-ci.yml"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># This file is a template, and might need editing before it works on your project.<\/span>\n<span class=\"hljs-comment\"># This is a sample GitLab CI\/CD configuration file that should run without any modifications.<\/span>\n<span class=\"hljs-comment\"># It demonstrates a basic 3 stage CI\/CD pipeline. Instead of real tests or scripts,<\/span>\n<span class=\"hljs-comment\"># it uses echo commands to simulate the pipeline execution.<\/span>\n<span class=\"hljs-comment\">#<\/span>\n<span class=\"hljs-comment\"># A pipeline is composed of independent jobs that run scripts, grouped into stages.<\/span>\n<span class=\"hljs-comment\"># Stages run in sequential order, but jobs within stages run in parallel.<\/span>\n<span class=\"hljs-comment\">#<\/span>\n<span class=\"hljs-comment\"># For more information, see: https:\/\/docs.gitlab.com\/ee\/ci\/yaml\/index.html#stages<\/span>\n<span class=\"hljs-comment\">#<\/span>\n<span class=\"hljs-comment\"># You can copy and paste this template into a new `.gitlab-ci.yml` file.<\/span>\n<span class=\"hljs-comment\"># You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.<\/span>\n<span class=\"hljs-comment\">#<\/span>\n<span class=\"hljs-comment\"># To contribute improvements to CI\/CD templates, please follow the Development guide at:<\/span>\n<span class=\"hljs-comment\"># https:\/\/docs.gitlab.com\/ee\/development\/cicd\/templates.html<\/span>\n<span class=\"hljs-comment\"># This specific template is located at:<\/span>\n<span class=\"hljs-comment\"># https:\/\/gitlab.com\/gitlab-org\/gitlab\/-\/blob\/master\/lib\/gitlab\/ci\/templates\/Getting-Started.gitlab-ci.yml<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/09d79145529f07f0b6a64d6ad7476d3d.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">.gitlab-ci.yml file to run with Docker runner<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">image: busybox:latest\n\nbefore_script:\n  - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Before script section\"<\/span>\n  - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"For example you might run an update here or install a build dependency\"<\/span>\n  - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Or perhaps you might print out some debugging details\"<\/span>\n\nafter_script:\n  - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"After script section\"<\/span>\n  - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"For example you might do some cleanup here\"<\/span>\n\nbuild1:\n  stage: build\n  script:\n    - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Do your build here\"<\/span>\n\ntest1:\n  stage: test\n  script:\n    - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Do a test here\"<\/span>\n    - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"For example run a test suite\"<\/span>\n\ntest2:\n  stage: test\n  script:\n    - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Do another parallel test here\"<\/span>\n    - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"For example run a lint test\"<\/span>\n\ndeploy1:\n  stage: deploy\n  script:\n    - <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Do your deploy here\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><code>Build a Docker image with CI\/CD and push to the GitLab registry<\/code><\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># To contribute improvements to CI\/CD templates, please follow the Development guide at:<\/span>\n<span class=\"hljs-comment\"># https:\/\/docs.gitlab.com\/ee\/development\/cicd\/templates.html<\/span>\n<span class=\"hljs-comment\"># This specific template is located at:<\/span>\n<span class=\"hljs-comment\"># https:\/\/gitlab.com\/gitlab-org\/gitlab\/-\/blob\/master\/lib\/gitlab\/ci\/templates\/Docker.gitlab-ci.yml<\/span>\n\n<span class=\"hljs-comment\"># Build a Docker image with CI\/CD and push to the GitLab registry.<\/span>\n<span class=\"hljs-comment\"># Docker-in-Docker documentation: https:\/\/docs.gitlab.com\/ee\/ci\/docker\/using_docker_build.html<\/span>\n<span class=\"hljs-comment\">#<\/span>\n<span class=\"hljs-comment\"># This template uses one generic job with conditional builds<\/span>\n<span class=\"hljs-comment\"># for the default branch and all other (MR) branches.<\/span>\n\ndocker-build:\n  <span class=\"hljs-comment\"># Use the official docker image.<\/span>\n  image: docker:latest\n  stage: build\n  services:\n    - docker:dind\n  before_script:\n    - docker login -u <span class=\"hljs-string\">\"$CI_REGISTRY_USER\"<\/span> -p <span class=\"hljs-string\">\"$CI_REGISTRY_PASSWORD\"<\/span> $CI_REGISTRY\n  <span class=\"hljs-comment\"># Default branch leaves tag empty (= latest tag)<\/span>\n  <span class=\"hljs-comment\"># All other branches are tagged with the escaped branch name (commit ref slug)<\/span>\n  script:\n    - |\n      <span class=\"hljs-keyword\">if<\/span> &#91;&#91; <span class=\"hljs-string\">\"$CI_COMMIT_BRANCH\"<\/span> == <span class=\"hljs-string\">\"$CI_DEFAULT_BRANCH\"<\/span> ]]; then\n        tag=<span class=\"hljs-string\">\"\"<\/span>\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'\"<\/span>\n      <span class=\"hljs-keyword\">else<\/span>\n        tag=<span class=\"hljs-string\">\":$CI_COMMIT_REF_SLUG\"<\/span>\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Running on branch '$CI_COMMIT_BRANCH': tag = $tag\"<\/span>\n      fi\n- docker build --pull -t <span class=\"hljs-string\">\"$CI_REGISTRY_IMAGE${tag}\"<\/span> .\n    - docker push <span class=\"hljs-string\">\"$CI_REGISTRY_IMAGE${tag}\"<\/span>\n  <span class=\"hljs-comment\"># Run this job in a branch where a Dockerfile exists<\/span>\n  rules:\n    - <span class=\"hljs-keyword\">if<\/span>: $CI_COMMIT_BRANCH\n      exists:\n        - Dockerfile\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>","protected":false},"excerpt":{"rendered":"<p>.gitlab-ci.yml file to run with Docker runner Build a Docker image with CI\/CD and push to the GitLab registry<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-31190","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=31190"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31190\/revisions"}],"predecessor-version":[{"id":31198,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31190\/revisions\/31198"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}