GitHub Actions Workflow Part 1 – Name On
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In today’s blog post, we will discuss the name and on elements of GitHub Action workflow and will have a demo on how to trigger a GitHub pipeline manually using API.
name (optional): You can define the name of your workflow using name parameter in GitHub action workflow. GitHub uses the value of name parameter in the repository action page. If you omit name, GitHub sets it to the workflow file path relative to the root of the repository.
1 |
name: GitHub Actions Demo |
on (required): The name of the event that will trigger your GitHub workflow. You can define multiple events as trigger for your GitHub workflow.
1 2 |
name: GitHub Actions Demo on: [push] # Triggers the pipeline whenever you push changes to the repository |
You can also define branches for push and pull request events.
1 2 3 4 5 6 7 8 |
name: GitHub Actions Demo on: # Triggers the pipeline whenever you push or raise pull request in main branch push: branches: - main pull_request: branches: - main |
In some events, you can also define the event types.
1 2 3 4 5 6 7 |
name: GitHub Actions Demo on: # Triggers the pipeline when a pull request is closed in main branch pull_request: types: - closed branches: - main |
Instead of event, you can also define a schedule to trigger your GitHub workflow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
name: GitHub Actions Demo on: # Triggers the pipeline every first day of a month @ 4 AM schedule: - cron: '0 4 1 * *' ## ┌───────────── minute (0 - 59) ## │ ┌───────────── hour (0 - 23) ## │ │ ┌───────────── day of the month (1 - 31) ## │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) ## │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) ## │ │ │ │ │ ## │ │ │ │ │ ## │ │ │ │ │ ## * * * * * |
You can also manually trigger the pipeline using repository_dispatch or workflow_dispatch events as explained in the demo.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
name: GitHub Actions Demo on: # Pipeline needs to be triggered manually from UI or API workflow_dispatch: inputs: who: description: 'Who is triggering the workflow' required: true default: 'debjeet' jobs: say_hello: runs-on: ubuntu-latest steps: - run: echo "Hello ${{ github.event.inputs.who }}!" |
How To Trigger A GitHub Action Workflow Manually Using API
Prerequisites:
Step 1: Create a new GitHub repository for this demo.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## I am using Linux Shell for this demo ## Replace github_personal_access_token ## github_repository_name, github_user_name ## Create a new GitHub repository curl -i -H "Authorization: token -d '{ "name": " "description": "mycicdpipeline", "auto_init": true, "private": true }' \ https://api.github.com/user/repos |
Step 2: Create a workflow file for your GitHub CI/CD pipeline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## Create your workflow file cat << 'EOF' > myworkflow.yaml name: GitHub Actions Demo on: workflow_dispatch: inputs: who: description: 'Who is triggering the workflow' required: true default: 'debjeet' jobs: say_hello: runs-on: ubuntu-latest steps: - run: echo "Hello ${{ github.event.inputs.who }}!" EOF ## Create base64 version of workflow CONTENT=$(cat myworkflow.yaml | base64 -w 0) && cat << EOF > content.json { "message":"workflow added", "content":"$CONTENT" } EOF |
Step 3: Upload the workflow file in your GitHub repository.
1 2 3 4 5 |
## Upload the workflow file in GitHub curl -X PUT \ -H "Authorization: token https://api.github.com/repos/ -d @content.json |
Note: The workflow will not get triggered automatically since we have given “on: workflow_dispatch” parameter in our GitHub workflow file.
You can manually trigger this pipeline from the GitHub UI or API. We will trigger the workflow using API.
Step 4: Get the GitHub workflow id using API.
1 2 3 4 |
## Get the workflow id WORKFLOW_ID=$(curl -s -H "Authorization: token https://api.github.com/repos/ echo $WORKFLOW_ID |
Step 5: Trigger the GitHub Actions Workflow Manually Using API
1 2 3 4 5 |
## Trigger the workflow manually using API curl -X POST \ -H "Authorization: token https://api.github.com/repos/ -d '{"ref":"main", "inputs": { "who":" |
Step 6: Get the workflow execution details.
1 2 3 4 5 6 7 |
## Get workflow details curl -H "Authorization: token https://api.github.com/repos/ ## Get details on workflow run status curl -H "Authorization: token https://api.github.com/repos/ |
You can also check from GitHub repository under Actions.
Step 7: Delete your GitHub repository using API.
1 2 3 4 |
## Delete the GitHub repository curl -X DELETE \ -H "Authorization: token https://api.github.com/repos/ |
Hope you have enjoyed this article. To know more about GitHub, please refer below official documentation