GitHub Actions

Introduction:  

Custom GitHub Actions has revolutionized the software development workflow, offering a robust and flexible platform for automating tasks. While the built-in options cover a wide range, the true magic lies in custom actions, allowing you to tailor automation to your specific needs. In this blog, we’ll delve into the three main types of custom actions and explore how they can supercharge your development process. 

To learn more about GitHub Actions read our blog Unleash the Power of Automation with GitHub Actions. 

Three types of Custom GitHub Actions: 

  1. Composite Actions 
  2. Docker Container Actions
  3. JavaScript Actions 

1. Composite Actions:  

Combine existing actions (both built-in and custom) into a single workflow, creating powerful and reusable automation. 

Example:

Custom GitHub Actions- Composite Action
  • Create  test_addition.yaml  file inside the .github/workflows  folder 

File Name:

Code:

  • Create actions.yaml and addition.py file inside the test_actions folder.

File Name:

Code:

  • runs key is used to specify the environment in which a job or step should run. 
  • When runs is configured with using: composite, it means that the job or step will run a series of steps defined as a composite run steps action. Composite run steps allow you to define and reuse sets of steps across multiple workflows or jobs. 

File Name:

Code:

2. Docker Container Actions:

Package your action’s logic within a Docker container, leveraging pre-built environments and dependencies. This ensures consistency, isolation, and portability across different systems. 

Example:

Custom GitHub Actions- Docker Container Actions
  • test_addition.yaml and addition.py file will be the same in this action. Basically, in docker actions, we need to add one file which is Dockerfile to build the container and need to slightly update an actions.yaml file. 

File Name:

Code:

  • runs is configured with using: ‘docker’, it means that the job or step will be executed within a Docker container. 
  • image: ‘Dockerfile’: Specifies the Docker image to use for running the job or step. In this case, ‘Dockerfile’ is likely referring to a Dockerfile in your repository’s root directory that defines the configuration for building the Docker image. The value provided here should be the name of the Docker image or the path to a Dockerfile

File Name:

Code:

3. JavaScript Actions:

Write your action’s logic directly in JavaScript, offering flexibility and rapid development. Perfect for quick scripts and simple tasks.  

Example:

Custom GitHub Actions- Javascript Actions

File Name:

Code:

File Name:

Code:

  • runs is configured with using: ‘node20’, it means that using: ‘node20’ Specifies that the job or step will run using Node.js version 20.x. GitHub Actions provides pre-configured environments for different programming languages and tools, including specific versions of Node.js. In this case, node20 likely refers to Node.js version 20.x. 
  • main: ‘index.js’: Specifies the entry point for the execution. In this case, ‘index.js’ is likely the main JavaScript file for your application or project. When the job or step runs, it will execute the code in index.js

File Name:

Code:

Choosing the Right Action: 

  1. Complexity of the task: For complex tasks, Docker container actions offer more control and isolation. Simple tasks can be handled by JavaScript actions. 
  2. Need for specific tools: If you need specific tools or frameworks, Docker container actions are the way to go. 
  3. Ease of development: JavaScript actions are easier to develop if you’re familiar with JavaScript. 
  4. Reusability: Composite actions are ideal for creating reusable workflows. 

Conclusion: 

Custom GitHub actions empower you to automate tasks, streamline your workflow, and focus on what truly matters – building great software. With the flexibility of Docker container actions, the agility of JavaScript actions, and the orchestration power of composite actions. 

shamsoddin-mujawar

Test Engineer