{"id":3533,"date":"2023-05-11T14:44:58","date_gmt":"2023-05-11T17:44:58","guid":{"rendered":"https:\/\/danillolima.com\/en\/?p=3533"},"modified":"2023-05-12T12:16:35","modified_gmt":"2023-05-12T15:16:35","slug":"how-deploy-your-application-just-on-successful-stages-on-gitlab-ci","status":"publish","type":"post","link":"https:\/\/danillolima.com\/en\/continuous-integration\/how-deploy-your-application-just-on-successful-stages-on-gitlab-ci\/","title":{"rendered":"How deploy your application just on successful stage(s) on GitLab CI"},"content":{"rendered":"\n<p>I&#8217;m not very proficient with <strong>GitLab CI\/CD\/Pipelines<\/strong>, and I have been trying to automate deployment only <strong>when a specific stage is successful<\/strong>, in my case, the test stage. I attempted to use global variables, but it seems that they cannot be reassigned within one job and used in another job. After researching on GitLab Docs [1] and Stack Overflow [2], I found a solution that works for me. <\/p>\n\n\n\n<p>Below is the structure of my <code>.gitlab-ci.yml<\/code> file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">stages:\n  - build\n  - test\n  - deploy\n\nbuild:\n  stage: build\n  script:\n    - mvn -DskipTests package\n\ntest:\n  stage: test\n  script:\n    - echo \"Executing the tests...\"\n    - mvn test\n  after_script:\n    - echo $CI_JOB_STATUS >> .\/results.txt\n  artifacts:\n    paths:\n      - results.txt\n\ndeploy:\n  stage: deploy\n  script:\n    - echo \"Checking if tests are successful...\"\n    - cat results.txt | if [[ \"$(cat)\" == *\"success\"* ]]; then\n      echo \"Okay! now I should do the deploy\";\n      else\n      echo \"Ops, there are test failing\";\n      fi\n  dependencies:\n    - test<\/pre>\n\n\n\n<p>We&#8217;re using artifacts and dependencies to pass information from the earlier stage (test) to the deploy stage.<\/p>\n\n\n\n<p>You can check the results.txt file for the status of &#8216;success,&#8217; &#8216;failed,&#8217; or &#8216;canceled.&#8217; Additionally, you can include other predefined variables [3] and informations.<\/p>\n\n\n\n<p>If you&#8217;re using Windows, remember to save the <code>.gitlab-ci.yml<\/code> file with LF (line separator <code>\\n<\/code>) to avoid any problems when running the script.<\/p>\n\n\n\n<p>If you are aware of a better approach for accomplishing this task, please feel free to share it with me. <\/p>\n\n\n\n<p>References:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.gitlab.com\/ee\/ci\/yaml\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">https:\/\/docs.gitlab.com\/ee\/ci\/yaml\/<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/38140996\/how-can-i-pass-gitlab-artifacts-to-another-stage\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">https:\/\/stackoverflow.com\/questions\/38140996\/how-can-i-pass-gitlab-artifacts-to-another-stage<\/a>  <\/li>\n\n\n\n<li><a href=\"https:\/\/docs.gitlab.com\/ee\/ci\/variables\/predefined_variables.html\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">https:\/\/docs.gitlab.com\/ee\/ci\/variables\/predefined_variables.html<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m not very proficient with GitLab CI\/CD\/Pipelines, and I have been trying to automate deployment only when a specific stage is successful, in my case, the test stage. I attempted to use global variables, but it seems that they cannot be reassigned within one job and used in another job. After researching on GitLab Docs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3548,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-3533","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-continuous-integration"],"_links":{"self":[{"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/posts\/3533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/comments?post=3533"}],"version-history":[{"count":15,"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/posts\/3533\/revisions"}],"predecessor-version":[{"id":3552,"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/posts\/3533\/revisions\/3552"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/media\/3548"}],"wp:attachment":[{"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/media?parent=3533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/categories?post=3533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danillolima.com\/en\/wp-json\/wp\/v2\/tags?post=3533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}