- Learning Github Actions
This is a beginner course on Github actions. I am creating this as a newbee who is also learning. Following this Repository will help you learn GitHub Actions as I learn too.
Event- A triger that will kick of a Github Action.It could be a Pull Request, Push to a branch , Issue created, Closed or any thing else that you can imagine as a triggerWorkFlow- This fines the task that will be run or an action done based on thetrigger- All workflows exist within a repository (As of the time of recording)
- Jobs - this is within a workflow and runs parallel by default with other jobs in the workflow
- Steps - These are steps done inside a Job and all steps inside a job is done sequentially by default.
Runners- This is the host on which the job runns- GitHub Runners
- Ubuntu
- Windows
- Mac
- Self Hosted Runners
- Built and customized by you
- GitHub Runners
-
Install the GH CLI
-
Create a repo
gh repo create KiranChilledOut/LearningGithubActions --public -
Clone the repo
gh repo clone kiranChilledOut/LearningGitHubActions -
In order to have workflow we need a
.githubfolder in the repository -New-Item -Type Directory -Name '.github' -
cdinto the folder and great a new folderworkflows-cd .\.github\andNew-Item -Type Directory -Name 'workflows' -
cdinto theworkflowfolder and create fileshello_world.yaml-cd .\workflows\andNew-Item -Type File -Name 'hello_world.yaml' -
Edit the workflow file with
vsCodeand add a 1st line the defines theworkflow namename: Hello World workflow
-
Next specify a trigger
on: push: branches: - main pull_request: - main workflow_dispatch:
on- Keyword for the triggerpush- trigger actionbranches- action entity
- In short it says whenever there is a
pushaction on themainbranch or apull requeston themainbranch or amanual triggeron the workflow - All Triggers Documentation
-
Next specify a Job
jobs: hello: runs-on: windows-latest steps: - uses: actions/checkout@v4 - name: hello world run: write-host "hello World" shell: powershell goodbye: runs-on: windows-latest steps: - name: goodbye world run: write-host "goodbye World" shell: powershell
jobs- Specifies the actions based on the trigger.Here the jobs arehelloandgoodbyeruns-on- Specifies the host/runner the job runs on.uses: actions/checkout@v4- this is used to use a community or a self created action. Click to know more about actions/checkout
-
Commit and push you code and go to the GitHub Repository in Browser
-
You will see in Actions tab of your repository the workflow you created
- We will be using github community action
Contextin order for the github actions to comment on an issue it needs to know some information like issue id etc. This is called Context- Before proceeding please check if you actions have the
writepermissions given to it- Settings -> Actions -> General ->
Read and write permissions
- Settings -> Actions -> General ->
-
Create a new file
issue_comment.yaml -
Add the below code
name: Create a comment on new issues on: issues: types: [opened] jobs: comment-with-action: runs-on: windows-latest steps: - name: "dump github context" run: | $json = '${{toJson(github.event)}}' Write-Output $json shell: pwsh
- Commit the above code and go the Github Browser and create a new issue.
- You will notice that it triggers the above workflow and you will see all the content of the issue because that is what the context using
github.eventwas used to trigger this workflow. - Go through the output and you will see a key called
numberwhich is the issuenumber - Now that we understood how the issue can be used as trigger lets add a comment when an issue is created.
-
Add the below step in the file
issue_comment.yamlcreated in the above step- name: Create comment uses: peter-evans/create-or-update-comment@v4 with: issue-number: ${{github.event.issue.number}} body: | This is a multi-line test comment - With GitHub **Markdown** :sparkles: - Created by [create-or-update-comment][1] [1]: https://github.com/peter-evans/create-or-update-comment reactions: '+1'
- Notice
${{github.event.issue.number}}is just the json tree output we saw in previous step.
- Notice
-
Commit changes and push the code.
-
Create a new issue and you will see the Github Actions commenting on it. This is so cool . Ain't it ? 😜 Next let us create the same commenting with an API
- Added the job for ubuntu and windows runners to the file
issue_comment.yamlcreated in the above step - Note the way you need to interact with Ubuntu and Windows Runner is different.
- Now create and issue and watch the action.
- To get a list of API and actions