Skip to content

Commit 49f1bd6

Browse files
committed
Fixed review comments
1 parent 22c865c commit 49f1bd6

File tree

2 files changed

+75
-33
lines changed

2 files changed

+75
-33
lines changed

learn-pr/github/create-custom-github-actions/includes/create-custom-github-action.md

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ GitHub Actions is a powerful feature that helps you to go from code to cloud, al
22

33
## Types of GitHub actions
44

5-
:::image type="content" source="../media/action-types.png" alt-text="Diagram that displays the three types of GitHub Actions; Docker, JavaScript, and composite run steps actions." border="false":::
5+
![alt text](../media/action-types.png)
66

77
Actions are individual tasks that you can use to customize your development workflows. You can create your own actions by writing custom code that interacts with your repository to perform custom tasks, or by using actions the GitHub community shares. Navigating through various actions, you'll notice that there are three different types of actions: _Docker container actions_, _JavaScript actions_, and _composite run steps actions_. Let's take a closer look at each action type.
88

@@ -36,9 +36,9 @@ Composite run steps actions allow you to reuse actions by using shell scripts. Y
3636

3737
# Packaged Composite Action
3838

39-
Packaged composite actions are a way to bundle multiple steps into a reusable unit. These actions are defined in a repository and can be referenced in workflows across different repositories. By packaging a composite action, you can simplify workflows, reduce duplication, and improve maintainability.
39+
Packaged composite actions bundle multiple steps into a reusable unit. These actions are defined in a repository and can be referenced in workflows across different repositories. By packaging a composite action, workflows are simplified, duplication is reduced, and maintainability is improved.
4040

41-
When creating a packaged composite action, you define the steps in a single `action.yml` file. This file specifies the inputs, outputs, and the sequence of commands or actions to execute. Packaged composite actions are particularly useful for automating repetitive tasks or combining multiple shell commands into a single reusable action.
41+
When creating a packaged composite action, the steps are defined in a single `action.yml` file. This file specifies the inputs, outputs, and the sequence of commands or actions to execute. Packaged composite actions are particularly useful for automating repetitive tasks or combining multiple shell commands into a single reusable action.
4242

4343
## Create a Composite Action
4444

@@ -47,11 +47,14 @@ When creating a packaged composite action, you define the steps in a single `act
4747
You must place your composite action in its own directory inside the repository.
4848

4949
**Example Directory Structure:**
50-
.github/actions/my-composite-action/ ├── action.yml └── scripts/ └── my-script.sh
50+
.github/actions/my-composite-action/
51+
├── action.yml
52+
└── scripts/
53+
└── my-script.sh
5154

5255
### 2. Define the `action.yml` File
5356

54-
Inside the **composite action directory**, create an `action.yml` file to define the action.
57+
Inside the **my-composite-action directory**, create an `action.yml` file.
5558

5659
```yaml
5760
name: "My Composite Action"
@@ -87,7 +90,7 @@ jobs:
8790
with:
8891
node-version: '18'
8992
```
90-
If your composite action is in **another repository**, reference it like this:
93+
If your composite action is shared from **another repository**, reference it like this:
9194
```
9295
uses: owner/repository/.github/actions/my-composite-action@v1
9396
```
@@ -127,26 +130,25 @@ jobs:
127130
128131
| **Best Practice** | **Description** |
129132
|--------------------------|---------------------------------------------------------------------------------|
130-
| **Use Versioning** | Use a `v1` tag to reference stable versions. |
133+
| **Use Versioning** | Use a `v1` tag to reference stable version 1. |
131134
| **Keep Actions Modular** | Group related steps inside a composite action. |
132135
| **Document Inputs & Outputs** | Add descriptions for inputs/outputs in `action.yml`. |
133136
| **Test Before Publishing** | Validate the composite action in a test repository. |
134137

135138
## Composite Action in a Workflow
139+
![alt text](../media/composite-action-workflow.png)
136140

137-
![alt text](../media/composite-action-workflow.png)
138141
## Benefits of Composite Actions:
139142
- **Reusability** - Define actions once and use them in multiple workflows.
140143
- **Maintainability** - Reduce duplication by centralizing logic in a single action.
141144
- **Modularity** - Combine multiple shell commands or other actions into a single unit.
142145

143146
# Develop an Action to Set Up a CLI on GitHub Actions Runners
144-
Many CI/CD workflows need a **specific CLI tool** to interact with cloud services, manage infrastructure, or execute scripts.
145-
Instead of manually installing a CLI in every workflow, you can create a **reusable GitHub Action** that:
147+
Many CI/CD workflows require a **specific version of a CLI tool** to interact with cloud services, manage infrastructure, or execute scripts. While GitHub-hosted runners come preinstalled with many tools, they may not include the exact version your workflow needs, especially if it's an older or unsupported version. Instead of installing the required CLI version in every workflow, you can create a **reusable GitHub Action** that:
146148

147-
- Ensures consistent CLI installation across jobs.
148-
- Simplifies workflows by reducing repeated installation steps.
149-
- Optimizes caching for faster workflow execution.
149+
- Ensures consistent installation of the required CLI version across jobs.
150+
- Simplifies workflows by centralizing the installation logic.
151+
- Optimizes caching for faster workflow execution.
150152

151153
## How to Develop a CLI Setup Action
152154

@@ -156,12 +158,36 @@ A **CLI setup action** is a **JavaScript-based action** that installs and config
156158

157159
### Step 1: Set Up the Action Directory
158160

159-
Create a new directory inside your GitHub repository:
160-
161-
```sh
162-
mkdir my-cli-action
163-
cd my-cli-action
164-
```
161+
To manually create the directory for your CLI setup action, follow these steps:
162+
163+
1. **Navigate to Your Repository**
164+
Open your terminal or command prompt and navigate to the root directory of your GitHub repository. For example:
165+
```sh
166+
cd /path/to/your/repository
167+
```
168+
169+
2. **Create a New Directory for the Action**
170+
Use the `mkdir` command to create a new directory named `my-cli-action` inside the `.github/actions` folder. This ensures your action is organized and follows GitHub's recommended structure for custom actions.
171+
```sh
172+
mkdir -p .github/actions/my-cli-action
173+
```
174+
175+
3. **Navigate to the New Directory**
176+
Change into the newly created directory to start adding files for your action:
177+
```sh
178+
cd .github/actions/my-cli-action
179+
```
180+
181+
4. **Verify the Directory Structure**
182+
After creating the directory, your repository structure should look like this:
183+
```
184+
your-repository/
185+
├── .github/
186+
│ ├── actions/
187+
│ │ ├── my-cli-action/
188+
```
189+
190+
You are now ready to proceed with creating the `action.yml` file and other necessary files for your CLI setup action.
165191
### Step 2: Define the action.yml Metadata File
166192
Create an action.yml file to describe the action.
167193

@@ -180,7 +206,7 @@ runs:
180206
using: "node16"
181207
main: "index.js"
182208
```
183-
Why use using: node16?
209+
Why use *using: node16?*
184210
This action runs JavaScript code using Node.js 16.
185211

186212
### Step 3: Create a JavaScript Script to Install the CLI
@@ -216,7 +242,11 @@ Create a workflow file (.github/workflows/test.yml) in your repository:
216242
```yaml
217243
name: Test MyCLI Setup
218244

219-
on: push
245+
on:
246+
push:
247+
branches:
248+
- main
249+
- feature/*
220250

221251
jobs:
222252
test:
@@ -230,20 +260,33 @@ jobs:
230260
with:
231261
version: '1.2.3'
232262

233-
- name: Check CLI Version
234-
run: mycli --version
263+
- name: Verify CLI Installation
264+
run: |
265+
echo "Checking MyCLI version..."
266+
mycli --version
235267
```
236-
**Caching the CLI Installation (Optimization)**
237-
To speed up workflows, you can cache the CLI installation using the actions/cache action.
238-
Modify test.yml:
268+
### Key Notes:
269+
1. **Branch Selection**: The workflow triggers on pushes to the `main` branch and any branch matching the `feature/*` pattern. Adjust this as needed for your repository.
270+
2. **Action Reference**: The `uses: ./.github/actions/my-cli-action` line references the custom action locally. Ensure the action directory and `action.yml` file are correctly set up.
271+
3. **CLI Version Input**: The `version` input specifies the CLI version to install. Update this value as required.
239272

273+
### Testing Locally
274+
To test this workflow locally, use the [`act`](https://github.com/nektos/act) CLI tool:
275+
```bash
276+
act -j test
277+
```
278+
This simulates the GitHub Actions environment on your local machine, allowing you to debug and validate the workflow before pushing changes.
279+
280+
### Optimization Tip: Caching
281+
To improve workflow performance, cache the CLI installation directory using the `actions/cache` action:
240282
```yaml
241-
- name: Cache MyCLI
242-
uses: actions/cache@v4
243-
with:
244-
path: ~/.mycli
245-
key: mycli-${{ runner.os }}-${{ inputs.version }}
283+
- name: Cache MyCLI
284+
uses: actions/cache@v4
285+
with:
286+
path: ~/.mycli
287+
key: mycli-${{ runner.os }}-${{ inputs.version }}
246288
```
289+
This ensures that subsequent runs reuse the cached CLI installation, reducing setup time.
247290

248291
### Best Practices for CLI Setup Actions
249292

@@ -341,12 +384,11 @@ try {
341384

342385

343386
### Best Practices for Debugging JavaScript Actions
344-
345387
| **Practice** | **Description** |
346388
|--------------------------|------------------------------------------------------------------------------|
347389
| **Use core.debug()** | Hide verbose logs unless debugging is enabled. |
348390
| **Validate action.yml** | Ensure inputs and outputs are correctly defined. |
349-
| **Bundle code** | Use `@vercel/ncc` to compile JavaScript into a single file. |
391+
| **Bundle code** | Use `@vercel/ncc` to compile JavaScript into a single file. This reduces dependencies and ensures all required modules are included, preventing runtime errors caused by missing files. |
350392
| **Test with act** | Simulate runs locally for faster iterations. |
351393
| **Use try/catch** | Prevent workflows from failing silently. |
352394

-2.13 KB
Loading

0 commit comments

Comments
 (0)