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
+19-23Lines changed: 19 additions & 23 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
+
:::image type="content" source="../media/action-types.png" alt-text="Diagram of the three types of GitHub Actions; Docker, JavaScript, and composite run steps actions." border="false":::
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
@@ -33,7 +33,6 @@ The steps to build a JavaScript action are minimal and straightforward:
33
33
34
34
Composite run steps actions allow you to reuse actions by using shell scripts. You can even mix multiple shell languages within the same action. If you have many shell scripts to automate several tasks, you can now easily turn them into an action and reuse them for different workflows. Sometimes it's easier to just write a shell script than using JavaScript or wrapping your code in a Docker container.
35
35
36
-
37
36
# Packaged composite action
38
37
39
38
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. Packaging a composite action simplifies workflows, reduces duplication, and improves maintainability.
@@ -79,7 +78,7 @@ runs:
79
78
```
80
79
**Note:** The using: **"composite"** field indicates that this action is a composite action.
81
80
82
-
### 3. Using the composite action in a workflow
81
+
### 3. Use the composite action in a workflow
83
82
Once the composite action is created, it can be referenced in a GitHub Actions workflow.
84
83
```yaml
85
84
jobs:
@@ -96,9 +95,9 @@ If your composite action is shared from **another repository**, reference it lik
:::image type="content" source="../media/composite-action-in-a-workflow.png" alt-text="Composite action used in a workflow." border="false":::
98
+
:::image type="content" source="../media/composite-action-in-a-workflow.png" alt-text="Screenshot of a composite action used in a workflow." border="false":::
100
99
101
-
## Adding outputs to a composite action
100
+
## Add outputs to a composite action
102
101
Composite actions can define outputs that workflows can use to pass data between steps or jobs. Outputs are particularly useful for sharing results or computed values from one action to another.
103
102
104
103
The following example demonstrates how to define and use an output in a composite action:
@@ -120,7 +119,7 @@ runs:
120
119
run: echo "result=Success" >> $GITHUB_OUTPUT
121
120
shell: bash
122
121
```
123
-
:::image type="content" source="../media/define-an-output-in-action.png" alt-text="Define an output in a composite action." border="false":::
122
+
:::image type="content" source="../media/define-an-output-in-action.png" alt-text="Screenshot of defining an output in a composite action." border="false":::
124
123
125
124
#### Use the output in a workflow
126
125
@@ -164,7 +163,7 @@ Composite actions are a powerful way to simplify workflows by bundling multiple
164
163
- **Maintainability** - Reduce duplication by centralizing logic in a single action.
165
164
- **Modularity** - Combine multiple shell commands or other actions into a single unit.
166
165
167
-
# Develop an action to set up a CLI on GitHub Actions runners
166
+
## Develop an action to set up a CLI on GitHub Actions runners
168
167
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:
169
168
170
169
- Ensures consistent installation of the required CLI version across jobs.
@@ -182,7 +181,7 @@ To manually create the directory for your CLI setup action, follow these steps:
182
181
183
182
1. **Navigate to your repository**
184
183
185
-
:::image type="content" source="../media/javascript-action-root-repo.png" alt-text="Root repository structure showing .github/actions/my-cli-action directory for a JavaScript action." border="false":::
184
+
:::image type="content" source="../media/javascript-action-root-repo.png" alt-text="Screenshot of the root repository structure showing for a JavaScript action." border="false":::
186
185
187
186
2. **Create a new directory for the action**
188
187
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.
@@ -199,7 +198,7 @@ To manually create the directory for your CLI setup action, follow these steps:
199
198
│ │ ├── my-cli-action/
200
199
```
201
200
202
-
:::image type="content" source="../media/javascript-action-directory.png" alt-text="Directory structure for a JavaScript action inside .github/actions." border="false":::
201
+
:::image type="content" source="../media/javascript-action-directory.png" alt-text="Screenshot of directory structure for a JavaScript action inside '.github/actions.' " border="false":::
203
202
204
203
You are now ready to proceed with creating the `action.yml` file and other necessary files for your CLI setup action.
205
204
@@ -224,7 +223,7 @@ runs:
224
223
Why use *using: node16?*
225
224
This action runs JavaScript code using Node.js 16.
226
225
227
-
:::image type="content" source="../media/javascript-action-yaml.png" alt-text="YAML metadata file for a JavaScript GitHub Action." border="false":::
226
+
:::image type="content" source="../media/javascript-action-yaml.png" alt-text="Screenshot of YAML metadata file for a JavaScript GitHub Action." border="false":::
228
227
229
228
### Step 3: Create a JavaScript script to install the CLI
230
229
In the same directory, create a file named index.js and add the following code:
@@ -250,7 +249,7 @@ run();
250
249
```
251
250
The JavaScript code above uses core.getInput() to retrieve the CLI version specified as input. It then executes a curl command to download and install the CLI. If the installation process fails, the action uses core.setFailed() to mark the workflow as failed.
252
251
253
-
:::image type="content" source="../media/javascript-action-index-js.png" alt-text="JavaScript code for index.js in a GitHub Action." border="false":::
252
+
:::image type="content" source="../media/javascript-action-index-js.png" alt-text="Screenshot of JavaScript code for index.js in a GitHub Action." border="false":::
254
253
255
254
### Step 4: Test the action locally
256
255
Before using the action in a workflow, test it on a GitHub-hosted runner.
@@ -264,7 +263,7 @@ on:
264
263
- main
265
264
- feature/*
266
265
```
267
-
**a. Triggering the workflow**
266
+
**1. Triggering the workflow**
268
267
The workflow is triggered on pushes to the main branch and any branch matching the feature/* pattern. You can adjust this to match your repository's branching strategy.
269
268
270
269
```yaml
@@ -275,7 +274,7 @@ jobs:
275
274
- name: Checkout repository
276
275
uses: actions/checkout@v4
277
276
```
278
-
**b. Checkout the repository**
277
+
**2. Clone the repository**
279
278
The *actions/checkout@v4* action is used to clone the repository onto the runner. This ensures that the workflow has access to the repository's files.
280
279
281
280
```yaml
@@ -284,7 +283,7 @@ jobs:
284
283
with:
285
284
version: '1.2.3'
286
285
```
287
-
**c. Run the custom action**
286
+
**3. Run the custom action**
288
287
The uses: *./.github/actions/my-cli-action* line references the custom action locally. Ensure that the action directory and action.yml file are correctly set up. The version input specifies the CLI version to install—in this case, version 1.2.3.
289
288
290
289
```yaml
@@ -293,12 +292,12 @@ The uses: *./.github/actions/my-cli-action* line references the custom action lo
293
292
echo "Checking MyCLI version..."
294
293
mycli --version
295
294
```
296
-
**d. Verify the CLI installation**
295
+
**4. Verify the CLI installation**
297
296
This step runs a shell command to verify that the CLI was installed successfully. It checks the version of the installed CLI by running mycli --version.
298
297
299
-
:::image type="content" source="../media/javascript-action-test.png" alt-text="Screenshot showing the test results of a JavaScript GitHub Action." border="false":::
298
+
:::image type="content" source="../media/javascript-action-test.png" alt-text="Screenshot of the test results of a JavaScript GitHub Action." border="false":::
300
299
301
-
### Testing locally
300
+
### Test locally
302
301
To test this workflow locally, use the [`act`](https://github.com/nektos/act) CLI tool:
303
302
```bash
304
303
act -j test
@@ -325,7 +324,6 @@ This ensures that subsequent runs reuse the cached CLI installation, reducing se
| **Provide Documentation** | Explain usage and inputs in `README.md`. |
327
326
328
-
329
327
# Troubleshoot JavaScript actions
330
328
331
329
When working with JavaScript-based GitHub Actions, you may encounter unexpected behavior, errors, or failures during workflow execution. This unit provides techniques and tools to help you identify and resolve issues in your JavaScript actions.
@@ -363,7 +361,7 @@ To enable **runner diagnostic logs**, set:
363
361
ACTIONS_RUNNER_DEBUG=true
364
362
```
365
363
366
-
### How to set secrets for debugging
364
+
### Set secrets for debugging
367
365
1. Go to your GitHub repository.
368
366
2. Navigate to **Settings** > **Secrets and variables** > **Actions**.
369
367
3. Add new secrets with the following names and values:
@@ -434,9 +432,7 @@ Docker container actions are powerful for encapsulating complex tools and enviro
434
432
## Understand the Docker action lifecycle
435
433
436
434
Before troubleshooting, it's helpful to understand how Docker container actions run.
437
-
![
438
-
(github-docker-workflow-blue.png)
439
-
]
435
+
:::image type="content" source="github-docker-workflow-blue.png" alt-text="Diagram showing how Docker container actions run in a GitHub Actions workflow." border="false":::
440
436
441
437
Note: Docker container actions run in a clean, isolated environment. File system state, installed tools, and environment variables must all be defined within the Dockerfile.
442
438
@@ -628,7 +624,7 @@ branding:
628
624
629
625
Here's an example of a Checkout action badge on the GitHub Marketplace:
630
626
631
-
:::image type="content" source="../media/actions-branding.png" alt-text="Screenshot that shows an action's branding on the GitHub Marketplace.":::
627
+
:::image type="content" source="../media/actions-branding.png" alt-text="Screenshot of an action's branding on the GitHub Marketplace.":::
0 commit comments