Run a workflow for a specific commit (sha)

Hi,

I’ve a workfow that i need to run it for  selected commits.

currently the workflow runs on **push tags** with a fixed tag name (for example: **trigger-workflow**) .

when i decide the run the workflow, i tag the target commit, using above tag name, and push it to origin.  after workflow execution i delete the tag both locally and remotely, until next commit that needs to go through workflow. 

Is there any easier way to do the same job without create tag?  all i want is to pass specific commit/sha to my workflow.

Thanks

I think the easiest possible solution is use triggering on repository dispatch event. And passing the git SHA as data field in JSON.

You can then send the signal by curl and authorize by Personal Access Token.

You can read the logic how to do it from repository-dispatch action.

(And I don’t know if this is still the case, but it used to be you had to have the workflow on master branch. But then you would just send the wanted ref in payload and checkout that.)

1 Like

Hi @pivotal-kaveh ,

You can add/choose an file(eg: checkfile) in your repo, and use ‘on.push.paths’ in workflow yaml to trigger the workflow. When you’d like to trigger the workflow, just edit the checkfile and push together with your specific commit. Code sample as below:

on:
  push:
    paths:
      - '**checkfile'

all i want is to pass specific commit/sha to my workflow.

>> I’m not sure why you’d like to pass the commit/sha to workflow, it should be random and irregular, could you please let me know what do you want to get from the commit/sha?

Thanks.

Hi @weide-zhou ,

I don’t want to run the workflow for the last commit. i want to run it for a any commit/ref i choose from existing commits. 

Assuming following commits: HEAD,  commit#2, commit#3 and…

I want the workflow to checkout commit#2 or #3  or any other existing commit and run the pipeline. 

I tried the first suggestion (by @cerna ) and it works as expected. 

Thanks

Thanks @pivotal-kaveh for your reply. Yes, you can use repository_dispatch action to pass the commit/sha to workflow. Glad to know it works for you. You can mark then the ticket can be archived.

@weide-zhou sadly this is not enough.

I have update-server-workflow.yml on master, but I am changing how we update servers on branch rewrite-update-server and now I cannot fully test, and work on this feature because updated workflow is not triggered. Only the workflow that is on Master is triggered. This is huge limitation.

Hi @Evilweed,

Repository_dispatch event only works on master or default branch, if you’d like to work with other branches, you can specify ref value for actions/checkout@v2.
For example:

      - name: checkout target branch
        uses: actions/checkout@v2
        with:
          ref: rewrite-update-server       # checkout your target branch

Thanks

No @weide-zhou, this blocks us in following way:

I am on branch change-how-our-update-db-on-server-dispatch-workflow-work and I am updating existing dispatch workflow that is being externally triggered and used to update database on specific server.

While working on this branch, even though I have changed update-db-on-server-dispatch-workflow.yml on my branch, if I want to test how it behaves it triggers workflow that is on master branch, which does not contain my new changes in this workflow. This is huge limitation and pain for Developers. And on top of that, we are trying to move from GitLab CI to Github Workflows, but many of our jobs use their similar feature to “repository_dispatch”, with this small change that every dispatch uses workflow that exists on branch/commit that was requested in dispatch action. With this feature, we can have 100 devs working on 100 branches where each branch modifies workflows and can use those modifications.

I need to make changes to dispatch workflow and use those changes without merging it to master.

For manually triggered parametric action runs.

Like deployment.

Here is a scenario I think people have:

“I push a button, and a workflow with a specific sha is triggered to deploy sha to a specific env”
Click -> wait -> [2h3978c42] has been deployed to “delta.dev.server.example.com

@molszanski this can be done by built-in deployment api passing payload + workflow with “on: deployment” (except for the “button” part).

curl -X POST https://api.github.com/repos/company/repo/deployments \
-H ‘Accept: application/vnd.github.everest-preview+json’ \
-H ‘Authorization: token <token>’ \
--data ‘{“ref”: “github-actions.deploys”, “auto_merge”: false, “environment”: “manhattan”, “description”: “Something”, “required_contexts”: [], “payload”: \“{”DEPLOY_TARGET”: “manhattan”, “TRIGGERED_MANUALLY”: “true”}\“}’

@weide-zhou What is not possible is to have on master branch:

name: Update server or other custom action

on: repository_dispatch

jobs:
  update-server:
    steps:
      - uses: action/111
      - uses: action/222
      - uses: actions/checkout@v2
        with:
          ref: ...
      - run: update-server.sh
      - uses: action/111
      - uses: action/222

And on new-server-update-method branch:

name: Update server or other custom action

on: repository_dispatch

jobs:
  update-server:
    steps:
      - uses: action/333
      - uses: action/444
      - uses: action/555
      - uses: action/666
      - uses: action/777
      - uses: action/888
      - uses: actions/checkout@v2
        with:
          ref: ...
      - run: update-server.sh
      - run: send-data-to-analytics.sh
      - run: chmod ...
      - uses: action/999
      - uses: action/111

And be able to trigger this workflow from new-server-update-method branch externally, so that
the workflow that is triggered is not the one on master but the one from new-server-update-method branch as developer needs to test and use new workflow.

Hi,

I think that with the latest API you should use workflow_dispatch event.
It will require you to pass a ref

Something like

curl -H "Accept: application/vnd.github.v3+json" \
    -H "Authorization: token YOUR-AUTH" \
    --request POST \
    --data '{"ref": "master", "inputs": {"custom_input": "custom-value"}}' \
    https://api.github.com/repos/YOUR_ORG/YOUR_REPO/actions/workflows/main.yml/dispatches

you will also need to update the workflow to support manual triggering

on:
  workflow_dispatch:
    inputs:
      custom_input:
        description: GUI description for this input
        default: ""
        required: false

You can then trigger from curl./REST API or manually from the GitHub web gui.