GitHub Actions Lab Guide - Part 2
GitHub Actions Lab Manual............................................................................................................................... 2
Introduction to GitHub Actions.........................................................................................................................2
Key Elements of GitHub Actions..................................................................................................................... 2
Creating Your First Workflow...........................................................................................................................3
Using Predefined GitHub Actions....................................................................................................................4
Node.js Example Workflow............................................................................................................................. 4
Deploying Applications with GitHub Actions....................................................................................................5
Running Jobs Sequentially..............................................................................................................................6
Using Multiple Triggers....................................................................................................................................6
Using Expressions and Contexts.................................................................................................................... 7
Conclusion.......................................................................................................................................................7
GitHub Actions Lab Manual
Introduction to GitHub Actions
GitHub Actions is a CI/CD and automation service integrated directly into GitHub. It enables developers to
automate workflows, such as building, testing, and deploying applications, triggered by events like code
pushes, pull requests, or scheduled tasks.
Key Elements of GitHub Actions
1. Workflow: A workflow is an automated process defined in a YAML file. It consists of one or more jobs
and is stored in the .github/workflows directory.
2. Job: A job is a set of steps executed on the same runner. Jobs can run independently or depend on the
results of other jobs.
3. Step: A step is an individual task that can run commands or use actions. Steps are executed
sequentially within a job.
Creating Your First Workflow
Steps:
1. Navigate to your GitHub repository.
2. Create a directory: .github/workflows/.
3. Add a new YAML file, for example, first-workflow.yml.
Sample Workflow:
name: First Workflow
on:
workflow_dispatch: # Manually triggers the workflow
jobs:
hello-world:
runs-on: ubuntu-latest
steps:
- name: Say Hello
run: echo "Hello World!"
- name: Say Goodbye
Author: Sunny Rampalli | Date: 25-Dec-2024 | Document Title: GitHub Complete Lab Guide
2
run: echo "Goodbye!"
Explanation:
● name: Defines the name of the workflow.
● on: Specifies events that trigger the workflow. Here, workflow_dispatch enables manual triggering.
● jobs: A collection of tasks to execute.
● runs-on: Defines the environment for the job (e.g., ubuntu-latest).
● steps: Tasks executed sequentially within the job.
Running the Workflow:
1. Navigate to the Actions tab in your repository.
2. Select the workflow and click Run Workflow.
3. Observe the logs for each step.
Using Predefined GitHub Actions
GitHub provides reusable actions to simplify workflow development.
Example: Checkout Code
steps:
- name: Checkout Code
uses: actions/checkout@v3
● uses: Specifies an action from GitHub Marketplace.
Node.js Example Workflow
Steps:
1. Set up a Node.js application.
2. Add the following workflow to your repository:
name: Node.js CI/CD
Author: Sunny Rampalli | Date: 25-Dec-2024 | Document Title: GitHub Complete Lab Guide
3
on: push
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set Up Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- name: Install Dependencies
run: npm install
- name: Run Tests
run: npm test
Explanation:
● Trigger (on): The workflow triggers when code is pushed to the repository.
● Steps:
○ actions/checkout: Retrieves repository code.
○ actions/setup-node: Configures a specific Node.js version.
○ npm install: Installs dependencies.
○ npm test: Runs test scripts.
Deploying Applications with GitHub Actions
GitHub Actions can automate deployments to staging or production environments.
Deployment Workflow Example:
name: Deploy Application
on: push
jobs:
Author: Sunny Rampalli | Date: 25-Dec-2024 | Document Title: GitHub Complete Lab Guide
4
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set Up Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- name: Build Project
run: npm run build
- name: Deploy
run: echo "Deploying to production..."
Additional Notes:
● Deployment actions vary based on the hosting platform (e.g., AWS, Azure, or DigitalOcean).
Running Jobs Sequentially
Jobs can depend on each other to run in a specific order.
Example:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Run Tests
run: echo "Testing the application..."
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- name: Deploy Application
run: echo "Deploying to production..."
Author: Sunny Rampalli | Date: 25-Dec-2024 | Document Title: GitHub Complete Lab Guide
5
Explanation:
● needs: Specifies dependencies. The deploy job runs only after the test job succeeds.
Using Multiple Triggers
Workflows can respond to multiple events.
Example:
name: Multi-Trigger Workflow
on:
push:
pull_request:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy Application
run: echo "Deploying for push or pull request..."
Using Expressions and Contexts
GitHub provides metadata and context objects for workflows.
Example:
jobs:
metadata:
runs-on: ubuntu-latest
steps:
- name: Print GitHub Context
run: echo "${{ toJSON(github) }}"
Author: Sunny Rampalli | Date: 25-Dec-2024 | Document Title: GitHub Complete Lab Guide
6
Explanation:
● github: Accesses metadata such as repository name, workflow name, and trigger events.
● toJSON: Converts objects into readable JSON format.
Conclusion
GitHub Actions provide a robust framework for CI/CD and workflow automation. By understanding its key
elements—workflows, jobs, and steps—and leveraging reusable actions, you can automate and streamline your
software development processes.
Author: Sunny Rampalli | Date: 25-Dec-2024 | Document Title: GitHub Complete Lab Guide
7