Topics Discussed:
-
-
- What is Github Actions ?
- Why Use GitHub Actions?
- Key Concepts
- Setting Up the First Workflow
-
The session started by giving an introduction to the Github actions.
What is GitHub Actions ?
In the past, companies used traditional methods to deploy code. Developers would write code in languages like React, Java, etc., and then hand it off to a separate team responsible for deploying it to servers. This process had several drawbacks, such as compatibility issues between development and production environments. Additionally, troubleshooting was often cumbersome, requiring coordination between the developer and the deployment team. Many of the steps were manual, making the process slow and prone to errors. With the advent of DevOps practices and CI/CD tools like Jenkins, GitHub Actions, and Azure DevOps, many of these issues have been addressed. The modern approach integrates code development, testing, and deployment into a streamlined, automated pipeline. Here’s how it works:
- Code Commit: Developers commit code changes to a repository.
- Testing & Building: These changes are automatically tested and built into an artifact.
- Deployment: The built artifact is deployed to an environment (such as a server or Kubernetes cluster).
- Production Deployment: After testing, the same code can be deployed to production.
CI (Continuous Integration) automates the build and testing process, while CD (Continuous Deployment/Delivery) automates the deployment of the code to the infrastructure. Tools like GitHub Actions make this process seamless by supporting CI/CD for a wide range of languages and platforms. These tools can also dynamically spin up and tear down infrastructure as needed.
Builds can be triggered in several ways:
- Commit: Automatically start a build when code is committed.
- Pull Request (PR): Trigger a build when a PR is created.
- Scheduled: Automate builds using cron-like schedules.
Why Use GitHub Actions?
- Automation : streamline repetitive tasks
- Integration : Seamlessly integrate with various deployment environments and with the Github where the code resides.
- Flexibility : We can create custom workflows as per our requirements.
- Community Support : Access to a wide range of prebuilt Actions for reference.
Key Concepts
The keys Concepts come under GitHub Actions :
- Workflow: automated processes defined in YAML files
- Events : Trigger that start workflows (e.g., push, pull request)
- Jobs : A set of steps executed on the same runner
- Steps: individual tasks within a job
- Actions : Reusable commands that can be used in workflows
Setting Up the First Workflow
Anyone with a valid email can create a GitHub account . The following steps to be followed while you create your first workflow
- Create a Repository: The repository created can be public or private. The Github Actions are free (within limits) if we create a public repo.
- Create a workflow directory: Add a .github/workflow directory under the repo created.
- Add Workflow file: create a YAML file (e.g., main.yml).
- Define Wokflow: Specify events, jobs, and steps in the YAML file.
Sample Workflow Yaml file.
The “on” keyword in a YAML file specifies the events that trigger the pipeline. This could be a general push event, a push to a specific branch, or a pull request to a particular branch. It can also include scheduled events and other triggers like page_build or release. For manual triggering, the workflow_dispatch option is used.
The “runs-on: ubuntu-latest” line defines the runner image (the agent where the code executes). This can be either a free runner provided by GitHub or a custom runner you set up.
More Attributes:
We can incorporate conditional checks to control when a job runs, such as executing only if the commit is on the main branch. GitHub Actions also support handling secrets and specifying particular versions of tools like Node.js. Using the matrix option, we can run tests across multiple versions.
In workflows with multiple jobs, each job runs in its own container or virtual machine, making them independent. To share files or data between jobs, use the artifacts block. Additionally, you can use the needs keyword to ensure that one job runs only after another has completed. For example, in the provided YAML, job_2 will execute only after job_1 has finished.
Viswajith shared his experience with creating workflows in the following GitHub repository: Viswajith’s Next.js App Workflows.
Demo Highlights
- GitHub Repository Creation: Set up a GitHub repository for the demo.
- Sample Workflow Creation: Developed and showcased a sample workflow.
- Multiple Jobs: Demonstrated multiple jobs within a workflow using features like needs and artifacts.
- Environment Configuration: Explained how to configure environments in workflows and how to create them under settings.
- Approval and Reviewers: Discussed handling approvals and reviewers within workflows.
- Concurrency Management: Showed how to manage multiple commits on the same job using the concurrency option to ensure only the most recent commit workflow runs while canceling older executions. Read more about concurrency.
- Workflow Reusability: Covered how to handle reusable workflows.
- Environment Variables and Secrets: Demonstrated working with environment variables and secrets within workflows.
Access Prometheus using secure link https://promm.duckdns.org
Access Grafana using secure link https://graffa.duckdns.org