Skip to content

Commit d9a6a2d

Browse files
authored
Update create-custom-github-action.md
1 parent 13163ee commit d9a6a2d

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

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

Lines changed: 19 additions & 23 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+
:::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":::
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

@@ -33,7 +33,6 @@ The steps to build a JavaScript action are minimal and straightforward:
3333

3434
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.
3535

36-
3736
# Packaged composite action
3837

3938
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:
7978
```
8079
**Note:** The using: **"composite"** field indicates that this action is a composite action.
8180
82-
### 3. Using the composite action in a workflow
81+
### 3. Use the composite action in a workflow
8382
Once the composite action is created, it can be referenced in a GitHub Actions workflow.
8483
```yaml
8584
jobs:
@@ -96,9 +95,9 @@ If your composite action is shared from **another repository**, reference it lik
9695
uses: owner/repository/.github/actions/my-composite-action@v1
9796
```
9897
99-
:::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":::
10099
101-
## Adding outputs to a composite action
100+
## Add outputs to a composite action
102101
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.
103102
104103
The following example demonstrates how to define and use an output in a composite action:
@@ -120,7 +119,7 @@ runs:
120119
run: echo "result=Success" >> $GITHUB_OUTPUT
121120
shell: bash
122121
```
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":::
124123

125124
#### Use the output in a workflow
126125

@@ -164,7 +163,7 @@ Composite actions are a powerful way to simplify workflows by bundling multiple
164163
- **Maintainability** - Reduce duplication by centralizing logic in a single action.
165164
- **Modularity** - Combine multiple shell commands or other actions into a single unit.
166165

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
168167
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:
169168

170169
- 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:
182181

183182
1. **Navigate to your repository**
184183

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":::
186185

187186
2. **Create a new directory for the action**
188187
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:
199198
│ │ ├── my-cli-action/
200199
```
201200

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":::
203202

204203
You are now ready to proceed with creating the `action.yml` file and other necessary files for your CLI setup action.
205204

@@ -224,7 +223,7 @@ runs:
224223
Why use *using: node16?*
225224
This action runs JavaScript code using Node.js 16.
226225

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":::
228227

229228
### Step 3: Create a JavaScript script to install the CLI
230229
In the same directory, create a file named index.js and add the following code:
@@ -250,7 +249,7 @@ run();
250249
```
251250
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.
252251

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":::
254253

255254
### Step 4: Test the action locally
256255
Before using the action in a workflow, test it on a GitHub-hosted runner.
@@ -264,7 +263,7 @@ on:
264263
- main
265264
- feature/*
266265
```
267-
**a. Triggering the workflow**
266+
**1. Triggering the workflow**
268267
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.
269268
270269
```yaml
@@ -275,7 +274,7 @@ jobs:
275274
- name: Checkout repository
276275
uses: actions/checkout@v4
277276
```
278-
**b. Checkout the repository**
277+
**2. Clone the repository**
279278
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.
280279
281280
```yaml
@@ -284,7 +283,7 @@ jobs:
284283
with:
285284
version: '1.2.3'
286285
```
287-
**c. Run the custom action**
286+
**3. Run the custom action**
288287
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.
289288
290289
```yaml
@@ -293,12 +292,12 @@ The uses: *./.github/actions/my-cli-action* line references the custom action lo
293292
echo "Checking MyCLI version..."
294293
mycli --version
295294
```
296-
**d. Verify the CLI installation**
295+
**4. Verify the CLI installation**
297296
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.
298297
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":::
300299
301-
### Testing locally
300+
### Test locally
302301
To test this workflow locally, use the [`act`](https://github.com/nektos/act) CLI tool:
303302
```bash
304303
act -j test
@@ -325,7 +324,6 @@ This ensures that subsequent runs reuse the cached CLI installation, reducing se
325324
| **Cache CLI Installation** | Optimize workflow performance using `actions/cache`. |
326325
| **Provide Documentation** | Explain usage and inputs in `README.md`. |
327326

328-
329327
# Troubleshoot JavaScript actions
330328

331329
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:
363361
ACTIONS_RUNNER_DEBUG=true
364362
```
365363

366-
### How to set secrets for debugging
364+
### Set secrets for debugging
367365
1. Go to your GitHub repository.
368366
2. Navigate to **Settings** > **Secrets and variables** > **Actions**.
369367
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
434432
## Understand the Docker action lifecycle
435433

436434
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":::
440436

441437
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.
442438

@@ -628,7 +624,7 @@ branding:
628624
629625
Here's an example of a Checkout action badge on the GitHub Marketplace:
630626
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.":::
632628
633629
## Workflow commands
634630

0 commit comments

Comments
 (0)