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
+34-10Lines changed: 34 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ 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 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.
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. Packaging a composite action simplifies workflows, reduces duplication, and improves maintainability.
40
40
41
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
@@ -96,9 +96,13 @@ If your composite action is shared from **another repository**, reference it lik
Composite actions can define outputs that can be used in workflows.
99
+
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.
100
100
101
-
### 1. Define an Output in action.yml
101
+
The following example demonstrates how to define and use an output in a composite action:
102
+
103
+
#### Define an Output in `action.yml`
104
+
105
+
The `action.yml` file specifies an output named `script-result`. This output retrieves its value from the `result` output of the `run-script` step. The `run-script` step runs a Bash command to set the output value.
102
106
103
107
```yaml
104
108
outputs:
@@ -113,7 +117,11 @@ runs:
113
117
run: echo "result=Success" >> $GITHUB_OUTPUT
114
118
shell: bash
115
119
```
116
-
### 2. Access the Output in a Workflow
120
+
121
+
#### Use the Output in a Workflow
122
+
123
+
Once the composite action is created, its output can be accessed in a workflow. Here's an example:
- The composite action is invoked using the `uses` keyword.
140
+
- Access the output `script-result` using the `steps.<step-id>.outputs.<output-name>` syntax.
141
+
- Display the result in the workflow logs.
142
+
143
+
Define outputs in composite actions to create reusable and modular workflows. This approach simplifies data sharing and improves maintainability.
144
+
130
145
## Best Practices for Composite Actions
131
146
132
147
| **Best Practice** | **Description** |
@@ -233,8 +248,7 @@ async function run() {
233
248
234
249
run();
235
250
```
236
-
**Explanation:**
237
-
The JavaScript action 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.
251
+
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.
238
252
239
253
### Step 4: Test the Action Locally
240
254
Before using the action in a workflow, test it on a GitHub-hosted runner.
@@ -247,28 +261,38 @@ on:
247
261
branches:
248
262
- main
249
263
- feature/*
264
+
```
265
+
**a. Triggering the Workflow**
266
+
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.
250
267
268
+
```yaml
251
269
jobs:
252
270
test:
253
271
runs-on: ubuntu-latest
254
272
steps:
255
273
- name: Checkout repository
256
274
uses: actions/checkout@v4
275
+
```
276
+
**b. Checkout the Repository**
277
+
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.
257
278
279
+
```yaml
258
280
- name: Install MyCLI
259
281
uses: ./.github/actions/my-cli-action
260
282
with:
261
283
version: '1.2.3'
284
+
```
285
+
**c. Run the Custom Action**
286
+
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.
262
287
288
+
```yaml
263
289
- name: Verify CLI Installation
264
290
run: |
265
291
echo "Checking MyCLI version..."
266
292
mycli --version
267
293
```
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.
294
+
**d. Verify the CLI Installation**
295
+
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.
272
296
273
297
### Testing Locally
274
298
To test this workflow locally, use the [`act`](https://github.com/nektos/act) CLI tool:
0 commit comments