{"id":451,"date":"2023-02-18T15:06:44","date_gmt":"2023-02-18T15:06:44","guid":{"rendered":"https:\/\/premsvmm.com\/?p=451"},"modified":"2023-02-18T15:08:01","modified_gmt":"2023-02-18T15:08:01","slug":"github-script-to-wait-for-another-workflow-to-complete","status":"publish","type":"post","link":"https:\/\/premsvmm.com\/index.php\/2023\/02\/18\/github-script-to-wait-for-another-workflow-to-complete\/","title":{"rendered":"Github script to wait for another workflow to complete"},"content":{"rendered":"\n<p>There are situations where one workflow must be finished before another workflow can begin. An example of this is when we require a Docker image to be constructed before moving forward with the deployment of the image to the test automation environment and executing the suite of tests.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Workflow 1: <\/h4>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;yaml&quot;,&quot;mime&quot;:&quot;text\/x-yaml&quot;,&quot;theme&quot;:&quot;duotone-light&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;YAML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;yaml&quot;}\">on: [ push ]\njobs:\n  cancel-previous-job:\n    name: Cancel previous workflow\n    if: always()\n    timeout-minutes: 1\n    runs-on: ubuntu-latest\n    steps:\n      - name: Cancel previous workflow\n        uses: styfle\/cancel-workflow-action@0.9.1\n        if: github.ref != 'refs\/heads\/master'\n        with:\n          all_but_latest: true\n          access_token: ${{ secrets.GITHUB_TOKEN }}\n          workflow_id: ${{ github.event.workflow.id }}\n  job1:\n    name: Test Job 1\n    needs: [ cancel-previous-job ]\n    runs-on: ubuntu-latest\n    steps:\n      - name: Checkout code\n        uses: actions\/checkout@v2\n      - name: Run single command\n        run: |\n          sleep 30\n          echo &quot;Job executed&quot;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Workflow 2:<\/h4>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;yaml&quot;,&quot;mime&quot;:&quot;text\/x-yaml&quot;,&quot;theme&quot;:&quot;duotone-light&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;YAML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;yaml&quot;}\">on: [ push ]\njobs:\n  cancel-previous-job:\n    name: Cancel previous workflow\n    if: always()\n    timeout-minutes: 1\n    runs-on: ubuntu-latest\n    steps:\n      - name: Cancel previous workflow\n        uses: styfle\/cancel-workflow-action@0.9.1\n        if: github.ref != 'refs\/heads\/master'\n        with:\n          all_but_latest: true\n          access_token: ${{ secrets.GITHUB_TOKEN }}\n          workflow_id: ${{ github.event.workflow.id }}\n  job2:\n    name: Test Job 2s\n    needs: [ cancel-previous-job ]\n    runs-on: ubuntu-latest\n    steps:\n      - name: Checkout code\n        uses: actions\/checkout@v2\n      - name: Get Build Image Workflow Status\n        env:\n          GITHUB_TOKEN: ${{ github.token }}\n          BRANCH: ${{ github.event.pull_request.head.ref }}\n          WORKFLOW: workflow_1.yml\n        run: |\n          echo &quot;WORKFLOW_STATUS=$(sh .\/.github\/actions\/status.sh)&quot; &gt;&gt; $GITHUB_ENV\n      - name: Check Workflow Status\n        run: echo $WORKFLOW_STATUS<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Script : <\/h4>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;duotone-light&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">#!\/bin\/sh\nget_workflow_status_value() {\n  URI=&quot;https:\/\/api.github.com&quot;\n  API_HEADER=&quot;Accept: application\/vnd.github.v3+json&quot;\n  AUTH_HEADER=&quot;Authorization: token ${GITHUB_TOKEN}&quot;\n  branch_name=${BRANCH}\n  workflow_file=${WORKFLOW}\n  workflow_response=$(curl -sSD build_image_workflow_response_header.txt -H &quot;${AUTH_HEADER}&quot; -H &quot;${API_HEADER}&quot; &quot;${URI}\/repos\/${GITHUB_REPOSITORY}\/actions\/workflows\/$workflow_file\/runs?branch=$branch_name&amp;per_page=1&quot; | jq '.')\n\n  echo &quot;$workflow_response&quot; &gt; build_image_workflow_response_body.txt\n  cat build_image_workflow_response_header.txt &gt;&gt; build_image_workflow_log.txt\n  cat build_image_workflow_response_body.txt &gt;&gt; build_image_workflow_log.txt\n\n  workflow_details=$(echo &quot;$workflow_response&quot; | jq --raw-output '{conclusion: .workflow_runs[0].conclusion, status: .workflow_runs[0].status}')\n  workflow_status=$(echo &quot;$workflow_details&quot; | jq --raw-output '.status')\n  while [ &quot;$workflow_status&quot; != &quot;completed&quot; ]\n  do\n    sleep 1m\n    workflow_response=$(curl -sSD build_image_workflow_response_header.txt -H &quot;${AUTH_HEADER}&quot; -H &quot;${API_HEADER}&quot; &quot;${URI}\/repos\/${GITHUB_REPOSITORY}\/actions\/workflows\/$workflow_file\/runs?branch=$branch_name&amp;per_page=1&quot; | jq '.')\n\n    echo &quot;$workflow_response&quot; &gt; build_image_workflow_response_body.txt\n    cat build_image_workflow_response_header.txt &gt;&gt; build_image_workflow_log.txt\n    cat build_image_workflow_response_body.txt &gt;&gt; build_image_workflow_log.txt\n\n    workflow_details=$(echo &quot;$workflow_response&quot; | jq --raw-output '{conclusion: .workflow_runs[0].conclusion, status: .workflow_runs[0].status}')\n    workflow_status=$(echo &quot;$workflow_details&quot; | jq --raw-output '.status')\n  done\n  workflow_conclusion=$(echo &quot;$workflow_details&quot; | jq --raw-output '.conclusion')\n}\n\nget_workflow_status_value\necho $workflow_conclusion<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example  <\/h4>\n\n\n\n<p>Job 2 waits till Job 1 completes another workflow<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"279\" src=\"https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.18-PM-1024x279.png\" alt=\"\" class=\"wp-image-453\" srcset=\"https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.18-PM-1024x279.png 1024w, https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.18-PM-300x82.png 300w, https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.18-PM-768x209.png 768w, https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.18-PM.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"331\" src=\"https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.28-PM-1024x331.png\" alt=\"\" class=\"wp-image-452\" srcset=\"https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.28-PM-1024x331.png 1024w, https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.28-PM-300x97.png 300w, https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.28-PM-768x248.png 768w, https:\/\/premsvmm.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-18-at-8.34.28-PM.png 1530w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example Github Repo :<\/h4>\n\n\n\n<p><a href=\"https:\/\/github.com\/premsvmm\/workflow-wait-example\">https:\/\/github.com\/premsvmm\/workflow-wait-example<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are situations where one workflow must be finished before another workflow can begin. An example of this is when we require a Docker image to be constructed before moving forward with the deployment of the image to the test automation environment and executing the suite of tests. Workflow 1: Workflow 2: Script : Example &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/premsvmm.com\/index.php\/2023\/02\/18\/github-script-to-wait-for-another-workflow-to-complete\/\"> <span class=\"screen-reader-text\">Github script to wait for another workflow to complete<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[7],"tags":[15],"_links":{"self":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/451"}],"collection":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/comments?post=451"}],"version-history":[{"count":3,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":456,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/451\/revisions\/456"}],"wp:attachment":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/media?parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/categories?post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/tags?post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}