How To Create GitHub Actions Using API
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
You can implement CI/CD pipeline in your GitHub repository using GitHub Actions. In this blog post, we will discuss how to create GitHub actions using API. In a nutshell, GitHub actions is a set of instructions known as workflow that is triggered based on some events or schedule and perform some actions in the form of jobs which can have multiple steps in a shared or dedicated server know as GitHub runner.
GitHub Actions Components:
Workflows: Workflow is a file written in YAML which contains a set of instructions to be executed. Workflow contains all the configurations like when to trigger, where to trigger, what to trigger etc. for your GitHub CI/CD pipeline.
Events: Like any modern application, GitHub is also event driven and generates a event with respect to any change. GitHub generates an event whenever we perform a action on GitHub like pushing a change, creation of branch, pull requests etc. You can use these events to trigger the CI/CD pipeline (GitHub Actions).
Jobs: Jobs are the unit of action that you want to perform in your CI/CD pipeline. You can use a job to build, test or deploy your application. Jobs gets executed in the GitLab runner and you can execute multiple jobs at once or create a dependency chain on jobs like if the build job fails, your test and deploy job will be skipped.
Steps: Steps are individual task or command that you execute in a job. A job contains multiple steps which can be executed sequentially or parallelly. Each step on a job executes in the same runner and you can pass data (artifacts) from one step to another or from one job to another.
Actions: Actions are standalone commands that gets executed in a step. You can create your own actions or use GitHub provided action to execute a command in GitHub runner.
Runners: Runners are servers where your CI/CD pipeline gets executed. GitHub provides their own set of shared runners that you can integrate in your GitHub pipeline or you can build and integrate your own dedicated runner in GitHub.
Next, we are going to create our first GitHub CI/CD pipeline.
How To Create GitHub Actions 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 <github_personal_access_token>" \ -d '{ "name": "<github_repository_name>", "description": "GitHub Actions Demo", "auto_init": true, "private": true }' \ https://api.github.com/user/repos |
Step 2: Create a workflow file that will contain your GitHub CI/CD configurations.
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 26 27 28 29 30 31 |
## Create your workflow file cat << EOF > myworkflow.yaml name: GitHub Actions Demo on: [push] jobs: job1: name: job1 runs-on: ubuntu-latest steps: - name: step1 run: echo "action 1" - name: step2 run: echo "action 2" job2: name: job2 runs-on: ubuntu-latest steps: - name: step1 run: echo "action 3" - name: step2 run: echo "action 4" EOF ## Create base64 version of workflow CONTENT=$(cat myworkflow.yaml | base64 -w 0) && cat << EOF > content.json { "message":"file myworkflow.yaml added", "content":"$CONTENT" } EOF |
Step 3: Upload the workflow file in .github/workflow directory.
1 2 3 4 5 |
## Upload the workflow file in GitHub curl -X PUT \ -H "Authorization: token <github_personal_access_token>" \ https://api.github.com/repos/<github_user_name>/<github_repository_name>/contents/.github/workflows/myworkflow.yaml \ -d @content.json |
Note: This will trigger the CI/CD pipeline immediately as we have set “on[push]” in the workflow definition. We are using a GitHub provided shared runner with ubuntu OS “runs-on: ubuntu-latest”.
Please follow this blog series to get details on all the configuration options available in GitHub workflow.
Step 4: Get the workflow details.
1 2 3 4 5 6 7 |
## Get workflow details curl -H "Authorization: token <github_personal_access_token>" \ https://api.github.com/repos/<github_user_name>/<github_repository_name>/actions/workflows ## Get details on workflow run status curl -H "Authorization: token <github_personal_access_token>" \ https://api.github.com/repos/<github_user_name>/<github_repository_name>/actions/runs |
You can also check the workflow execution status directly from GitHub.
Step 5: Delete the GitHub repository.
1 2 3 4 |
## Delete the GitHub repository curl -X DELETE \ -H "Authorization: token <github_personal_access_token>" \ https://api.github.com/repos/<github_user_name>/<github_repository_name> |
Hope you have enjoyed this article. To know more about GitHub, please refer below official documentation