You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/github/create-custom-github-actions/includes/create-custom-github-action.md
+75-33Lines changed: 75 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@ GitHub Actions is a powerful feature that helps you to go from code to cloud, al
2
2
3
3
## Types of GitHub actions
4
4
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
+

6
6
7
7
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.
8
8
@@ -36,9 +36,9 @@ Composite run steps actions allow you to reuse actions by using shell scripts. Y
36
36
37
37
# Packaged Composite Action
38
38
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.
40
40
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.
42
42
43
43
## Create a Composite Action
44
44
@@ -47,11 +47,14 @@ When creating a packaged composite action, you define the steps in a single `act
47
47
You must place your composite action in its own directory inside the repository.
- **Reusability** - Define actions once and use them in multiple workflows.
140
143
- **Maintainability** - Reduce duplication by centralizing logic in a single action.
141
144
- **Modularity** - Combine multiple shell commands or other actions into a single unit.
142
145
143
146
# 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:
146
148
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.
150
152
151
153
## How to Develop a CLI Setup Action
152
154
@@ -156,12 +158,36 @@ A **CLI setup action** is a **JavaScript-based action** that installs and config
156
158
157
159
### Step 1: Set Up the Action Directory
158
160
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.
165
191
### Step 2: Define the action.yml Metadata File
166
192
Create an action.yml file to describe the action.
167
193
@@ -180,7 +206,7 @@ runs:
180
206
using: "node16"
181
207
main: "index.js"
182
208
```
183
-
Why use using: node16?
209
+
Why use *using: node16?*
184
210
This action runs JavaScript code using Node.js 16.
185
211
186
212
### 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:
216
242
```yaml
217
243
name: Test MyCLI Setup
218
244
219
-
on: push
245
+
on:
246
+
push:
247
+
branches:
248
+
- main
249
+
- feature/*
220
250
221
251
jobs:
222
252
test:
@@ -230,20 +260,33 @@ jobs:
230
260
with:
231
261
version: '1.2.3'
232
262
233
-
- name: Check CLI Version
234
-
run: mycli --version
263
+
- name: Verify CLI Installation
264
+
run: |
265
+
echo "Checking MyCLI version..."
266
+
mycli --version
235
267
```
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.
239
272
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:
240
282
```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 }}
246
288
```
289
+
This ensures that subsequent runs reuse the cached CLI installation, reducing setup time.
247
290
248
291
### Best Practices for CLI Setup Actions
249
292
@@ -341,12 +384,11 @@ try {
341
384
342
385
343
386
### Best Practices for Debugging JavaScript Actions
|**Use core.debug()**| Hide verbose logs unless debugging is enabled. |
348
390
|**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.|
350
392
|**Test with act**| Simulate runs locally for faster iterations. |
351
393
|**Use try/catch**| Prevent workflows from failing silently. |
0 commit comments