Answers: <p>There currently isn't a solution for building on a specific runner in GitLab, but there is an issue open for <a href="https://gitlab.com/gitlab-org/gitlab-ce/issues/29447" rel="nofollow noreferrer">Sticky Runners</a>, which hopefully will be out in the next 3-6 months according to the Milestones!</p>
  
  <hr>
  
  <p>The work around I've done so far to build a project on a specific runner is to use the <a href="https://docs.gitlab.com/ce/api/runners.html" rel="nofollow noreferrer">GitLab Runner API</a>, in a rather hacky way, along the lines of:</p>
  
  <ul>
  <li>Get all project runners</li>
  <li>As I know I've deployed the latest runner, that would have the highest runner "number"</li>
  <li>Pause all the other runners associated with the project in question</li>
  <li>Trigger the pipeline to build on the latest runner</li>
  <li>Poll the GitLab API to get the status of the pipeline</li>
  <li>Once that succeeds, resume all other runners!</li>
  <li>If the pipeline fails, remember to resume the paused runners...</li>
  </ul>
  
Answers: CC BY-SA 3.0
Answers: 2017-11-29T16:45:36.827Z
Answers: 2017-11-29T16:45:36.827Z
Answers: Rekovni
Body: <p><strong>Scenario</strong></p>
  
  <ol>
  <li>Create a new virtual machine with runner installed</li>
  <li>Trigger a pipeline to run (from another project), which runs on this new virtual machine</li>
  <li>Do something else, or fail if pipeline above fails on the new virtual machine</li>
  </ol>
  
  <p><strong>Question</strong></p>
  
  <p>I'm using GitLabs Triggers API to run a pipeline from a project, however would like it to run on this newly created virtual machine. </p>
  
  <p>What would be the best way of parsing this information to the GitLab API, or would something like tags be the best way of doing this?</p>
  
  <hr>
  
  <p><strong>.gitlab-ci.yml</strong></p>
  
  <pre><code>stages:
    - deploy to test
    - test
    - deploy to prod
  
  Terraform Deploy To Test:
    stage: deploy to test
    script:
      - cd test_deploy
      - python deploy.py
  
  Testing:
    stage: test
    script:
      - curl --request POST --form token=TOKEN --form ref=master http://gitlab.example.com/api/v4/projects/12/trigger/pipeline
  
  Terraform Deploy to Prod:
    stage: deploy to prod
    script:
      - do something if the above testing succeeds, or fail
  </code></pre>
  
  <p>The above <code>gitlab-ci</code> file would generate a new build machine and deploy it to the "test group" within vsphere with a python script. (Deploying it would activate a <code>gitlab-runner</code> and tie it to a specific project and have some tags associated with it).</p>
  
  <p>To test, it then triggers a pipeline call using the GitLab API, and I would like that pipeline to run on the new generated build machine on vsphere.</p>
  
  <hr>
  
  <p>Would a work around maybe be something like using <a href="https://docs.gitlab.com/ce/ci/environments.html#manually-deploying-to-environments" rel="nofollow noreferrer">gitlab runner exec</a> command or using <a href="https://docs.gitlab.com/ce/ci/environments.html#manually-deploying-to-environments" rel="nofollow noreferrer">environments</a>?</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-10-24T07:48:22.677Z
Comments: The pipeline will run locally or on a runner, without insight of what kind of pipeline it is it's hard to answer. Could you extend to give more details and maybe show your .gitlab-ci.yml so we can have a better idea of what it is supposed to do ?
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2017-10-24T12:20:17.717Z
Comments: I think I see the overall idea, but that's very broad and if your deploy.py doen't give a return about the machine which has been spin up, that's just a dead end, and we have no idea what your porject 12 .gitlab-ci.yml does and as such no way to give advice on how to deport it on the created machine. (All in all this sounds a bad approach to the problem)
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2017-10-24T13:26:47.567Z
Comments: @Tensibai thanks for the comment - the `deploy.py` is just running some Terraform commands and can output the ip of the machine generated. I was more thinking of using tags, but then there are issues of tags on the existing project. Project 12 is just a test project with echo calls in a pipeline. 
  
  Would you have any other suggestions on how to do something like this?
Comments: Rekovni
Comments: CC BY-SA 3.0
Comments: 2017-10-24T13:29:50.770Z
Comments: For the second part, just as any async batch job, loop asking for the status of this pipeline id (which id is returned by the trigger call within the json object), knowing what this second pipeline does and if it can do it through ssh or if it need some other convoluted way is a matter of divination
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2017-10-24T13:57:22.437Z
Comments: @Tensibai was thinking of something similar using the GitLab API to get the status of the job - thanks for clarifying. Any ideas for the first part?
Comments: Rekovni
Comments: CC BY-SA 3.0
Comments: 2017-10-24T14:02:34.977Z
Comments: Again I can't have an idea for the first part with that few input, I've no idea what machine are spin up, what is accessible, what does deploy.py and what terraform 'plan' is ...
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2017-10-24T14:10:58.543Z
Comments: @Tensibai so it's just a simple windows virtual machine with a gitlab runner for testing. `deploy.py` just runs `terraform init, terraform plan, and terraform apply`, and that produces a vsphere virtual image.
Comments: Rekovni
Tags: <gitlab><gitlab-ci-runner>
Title: GitLab - trigger pipeline to build on specific runner
UserDisplayName: Rekovni
ThreadText: # GitLab - trigger pipeline to build on specific runner
  
  (Asked by: username_0 on 2017-10-23)
  
  **Scenario**
  
  1. Create a new virtual machine with runner installed
  2. Trigger a pipeline to run (from another project), which runs on this new virtual machine
  3. Do something else, or fail if pipeline above fails on the new virtual machine
  
  **Question**
  
  I'm using GitLabs Triggers API to run a pipeline from a project, however would like it to run on this newly created virtual machine.
  
  What would be the best way of parsing this information to the GitLab API, or would something like tags be the best way of doing this?
  
  ---
  
  **.gitlab-ci.yml**
  
  ```
  stages:
    - deploy to test
    - test
    - deploy to prod
  
  Terraform Deploy To Test:
    stage: deploy to test
    script:
      - cd test_deploy
      - python deploy.py
  
  Testing:
    stage: test
    script:
      - curl --request POST --form token=TOKEN --form ref=master http://gitlab.example.com/api/v4/projects/12/trigger/pipeline
  
  Terraform Deploy to Prod:
    stage: deploy to prod
    script:
      - do something if the above testing succeeds, or fail
  
  ```
  
  The above `gitlab-ci` file would generate a new build machine and deploy it to the "test group" within vsphere with a python script. (Deploying it would activate a `gitlab-runner` and tie it to a specific project and have some tags associated with it).
  
  To test, it then triggers a pipeline call using the GitLab API, and I would like that pipeline to run on the new generated build machine on vsphere.
  
  ---
  
  Would a work around maybe be something like using [gitlab runner exec](https://docs.gitlab.com/ce/ci/environments.html#manually-deploying-to-environments) command or using [environments](https://docs.gitlab.com/ce/ci/environments.html#manually-deploying-to-environments)?
  
  **Tags:** gitlab, gitlab-ci-runner
  
  > **Comments:**
  > **username_1**: The pipeline will run locally or on a runner, without insight of what kind of pipeline it is it's hard to answer. Could you extend to give more details and maybe show your .gitlab-ci.yml so we can have a better idea of what it is supposed to do ?  
  > **username_1**: I think I see the overall idea, but that's very broad and if your deploy.py doen't give a return about the machine which has been spin up, that's just a dead end, and we have no idea what your porject 12 .gitlab-ci.yml does and as such no way to give advice on how to deport it on the created machine. (All in all this sounds a bad approach to the problem)  
  > **username_0**: @username_1 thanks for the comment - the `deploy.py` is just running some Terraform commands and can output the ip of the machine generated. I was more thinking of using tags, but then there are issues of tags on the existing project. Project 12 is just a test project with echo calls in a pipeline.   Would you have any other suggestions on how to do something like this?  
  > **username_1**: For the second part, just as any async batch job, loop asking for the status of this pipeline id (which id is returned by the trigger call within the json object), knowing what this second pipeline does and if it can do it through ssh or if it need some other convoluted way is a matter of divination  
  > **username_0**: @username_1 was thinking of something similar using the GitLab API to get the status of the job - thanks for clarifying. Any ideas for the first part?  
  > **username_1**: Again I can't have an idea for the first part with that few input, I've no idea what machine are spin up, what is accessible, what does deploy.py and what terraform 'plan' is ...  
  > **username_0**: @username_1 so it's just a simple windows virtual machine with a gitlab runner for testing. `deploy.py` just runs `terraform init, terraform plan, and terraform apply`, and that produces a vsphere virtual image.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-11-29. Score: 2 | Accepted answer
  
  There currently isn't a solution for building on a specific runner in GitLab, but there is an issue open for [Sticky Runners](https://gitlab.com/gitlab-org/gitlab-ce/issues/29447), which hopefully will be out in the next 3-6 months according to the Milestones!
  
  ---
  
  The work around I've done so far to build a project on a specific runner is to use the [GitLab Runner API](https://docs.gitlab.com/ce/api/runners.html), in a rather hacky way, along the lines of:
  
  * Get all project runners
  * As I know I've deployed the latest runner, that would have the highest runner "number"
  * Pause all the other runners associated with the project in question
  * Trigger the pipeline to build on the latest runner
  * Poll the GitLab API to get the status of the pipeline
  * Once that succeeds, resume all other runners!
  * If the pipeline fails, remember to resume the paused runners...
  
  
  ---
  
  