|
1 |
| -# Create a JavaScript Action |
| 1 | +# SSM Send Command Action for GitHub Actions |
2 | 2 |
|
3 |
| -[](https://github.com/super-linter/super-linter) |
4 |
| - |
| 3 | +This action sends commands to an EC2 instance via AWS Systems Manager (SSM). You |
| 4 | +can use it to execute commands on your EC2 instances directly from your GitHub |
| 5 | +Actions workflows. |
5 | 6 |
|
6 |
| -Use this template to bootstrap the creation of a JavaScript action. :rocket: |
| 7 | +## Example of Usage |
7 | 8 |
|
8 |
| -This template includes compilation support, tests, a validation workflow, |
9 |
| -publishing, and versioning guidance. |
| 9 | +### Send Commands to an EC2 Instance |
10 | 10 |
|
11 |
| -If you are new, there's also a simpler introduction in the |
12 |
| -[Hello world JavaScript action repository](https://github.com/actions/hello-world-javascript-action). |
| 11 | +Before using this action, make sure to include the following |
13 | 12 |
|
14 |
| -## Create Your Own Action |
15 |
| - |
16 |
| -To create your own action, you can use this repository as a template! Just |
17 |
| -follow the below instructions: |
18 |
| - |
19 |
| -1. Click the **Use this template** button at the top of the repository |
20 |
| -1. Select **Create a new repository** |
21 |
| -1. Select an owner and name for your new repository |
22 |
| -1. Click **Create repository** |
23 |
| -1. Clone your new repository |
24 |
| - |
25 |
| -> [!IMPORTANT] |
26 |
| -> |
27 |
| -> Make sure to remove or update the [`CODEOWNERS`](./CODEOWNERS) file! For |
28 |
| -> details on how to use this file, see |
29 |
| -> [About code owners](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners). |
30 |
| -
|
31 |
| -## Initial Setup |
32 |
| - |
33 |
| -After you've cloned the repository to your local machine or codespace, you'll |
34 |
| -need to perform some initial setup steps before you can develop your action. |
35 |
| - |
36 |
| -> [!NOTE] |
37 |
| -> |
38 |
| -> You'll need to have a reasonably modern version of |
39 |
| -> [Node.js](https://nodejs.org) handy. If you are using a version manager like |
40 |
| -> [`nodenv`](https://github.com/nodenv/nodenv) or |
41 |
| -> [`nvm`](https://github.com/nvm-sh/nvm), you can run `nodenv install` in the |
42 |
| -> root of your repository to install the version specified in |
43 |
| -> [`package.json`](./package.json). Otherwise, 20.x or later should work! |
44 |
| -
|
45 |
| -1. :hammer_and_wrench: Install the dependencies |
46 |
| - |
47 |
| - ```bash |
48 |
| - npm install |
49 |
| - ``` |
50 |
| - |
51 |
| -1. :building_construction: Package the JavaScript for distribution |
| 13 | +```yaml |
| 14 | +- name: Configure AWS credentials |
| 15 | + uses: aws-actions/configure-aws-credentials@v4 |
| 16 | + with: |
| 17 | + role-to-assume: arn:aws:iam::123456789012:role/my-github-actions-role |
| 18 | + aws-region: us-east-1 |
| 19 | +``` |
52 | 20 |
|
53 |
| - ```bash |
54 |
| - npm run bundle |
55 |
| - ``` |
| 21 | +#### Send commands to an EC2 instance |
56 | 22 |
|
57 |
| -1. :white_check_mark: Run the tests |
| 23 | +```yaml |
| 24 | +- name: Send commands to EC2 instance |
| 25 | + uses: your-github-username/ssm-send-command-action@v1 |
| 26 | + with: |
| 27 | + instanceName: my-ec2-instance |
| 28 | + workingDirectory: /path/to/dir |
| 29 | + commands: | |
| 30 | + echo "Hello World" |
| 31 | + ls -la |
| 32 | +``` |
58 | 33 |
|
59 |
| - ```bash |
60 |
| - $ npm test |
| 34 | +## Inputs |
61 | 35 |
|
62 |
| - PASS ./index.test.js |
63 |
| - ✓ throws invalid number (3ms) |
64 |
| - ✓ wait 500 ms (504ms) |
65 |
| - ✓ test runs (95ms) |
| 36 | +- `instanceId` (optional): The ID of the EC2 instance you want to connect to. |
| 37 | +- `instanceName` (optional): The name of the EC2 instance you want to connect |
| 38 | + to. If both `instanceId` and `instanceName` are provided, `instanceId` takes |
| 39 | + precedence. |
| 40 | +- `workingDirectory` (required): The working directory where you want to execute |
| 41 | + commands. |
| 42 | +- `commands` (required): The commands you want to execute on the instance. |
66 | 43 |
|
67 |
| - ... |
68 |
| - ``` |
| 44 | +## Outputs |
69 | 45 |
|
70 |
| -## Update the Action Metadata |
| 46 | +- `commandId`: The ID of the executed command. |
71 | 47 |
|
72 |
| -The [`action.yml`](action.yml) file defines metadata about your action, such as |
73 |
| -input(s) and output(s). For details about this file, see |
74 |
| -[Metadata syntax for GitHub Actions](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions). |
| 48 | +## Credentials |
75 | 49 |
|
76 |
| -When you copy this repository, update `action.yml` with the name, description, |
77 |
| -inputs, and outputs for your action. |
| 50 | +This action relies on the |
| 51 | +[AWS SDK for JavaScript](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials.html) |
| 52 | +to determine AWS credentials and region. Use the |
| 53 | +[aws-actions/configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials) |
| 54 | +action to configure the GitHub Actions environment with appropriate AWS |
| 55 | +credentials and region. |
78 | 56 |
|
79 |
| -## Update the Action Code |
| 57 | +```yaml |
| 58 | +- name: Configure AWS credentials |
| 59 | + uses: aws-actions/configure-aws-credentials@v4 |
| 60 | + with: |
| 61 | + role-to-assume: arn:aws:iam::123456789012:role/my-github-actions-role |
| 62 | + aws-region: us-east-1 |
| 63 | +``` |
80 | 64 |
|
81 |
| -The [`src/`](./src/) directory is the heart of your action! This contains the |
82 |
| -source code that will be run when your action is invoked. You can replace the |
83 |
| -contents of this directory with your own code. |
| 65 | +### Required Permissions |
84 | 66 |
|
85 |
| -There are a few things to keep in mind when writing your action code: |
| 67 | +Ensure that the IAM role or user associated with the AWS credentials has |
| 68 | +permissions to execute SSM commands. |
86 | 69 |
|
87 |
| -- Most GitHub Actions toolkit and CI/CD operations are processed asynchronously. |
88 |
| - In `main.js`, you will see that the action is run in an `async` function. |
| 70 | +#### Example |
89 | 71 |
|
90 |
| - ```javascript |
91 |
| - const core = require('@actions/core') |
92 |
| - //... |
| 72 | +Here’s the example IAM Policy you can use for running this GitHub Action: |
93 | 73 |
|
94 |
| - async function run() { |
95 |
| - try { |
96 |
| - //... |
97 |
| - } catch (error) { |
98 |
| - core.setFailed(error.message) |
| 74 | +```json |
| 75 | +{ |
| 76 | + "Version": "2012-10-17", |
| 77 | + "Statement": [ |
| 78 | + { |
| 79 | + "Effect": "Allow", |
| 80 | + "Action": [ |
| 81 | + "ec2:DescribeInstances", |
| 82 | + "ssm:SendCommand", |
| 83 | + "ssm:ListCommandInvocations", |
| 84 | + "ssm:DescribeInstanceInformation" |
| 85 | + ], |
| 86 | + "Resource": "*" |
99 | 87 | }
|
100 |
| - } |
101 |
| - ``` |
102 |
| - |
103 |
| - For more information about the GitHub Actions toolkit, see the |
104 |
| - [documentation](https://github.com/actions/toolkit/blob/main/README.md). |
105 |
| - |
106 |
| -So, what are you waiting for? Go ahead and start customizing your action! |
107 |
| - |
108 |
| -1. Create a new branch |
109 |
| - |
110 |
| - ```bash |
111 |
| - git checkout -b releases/v1 |
112 |
| - ``` |
113 |
| - |
114 |
| -1. Replace the contents of `src/` with your action code |
115 |
| -1. Add tests to `__tests__/` for your source code |
116 |
| -1. Format, test, and build the action |
117 |
| - |
118 |
| - ```bash |
119 |
| - npm run all |
120 |
| - ``` |
121 |
| - |
122 |
| - > [!WARNING] |
123 |
| - > |
124 |
| - > This step is important! It will run [`ncc`](https://github.com/vercel/ncc) |
125 |
| - > to build the final JavaScript action code with all dependencies included. |
126 |
| - > If you do not run this step, your action will not work correctly when it is |
127 |
| - > used in a workflow. This step also includes the `--license` option for |
128 |
| - > `ncc`, which will create a license file for all of the production node |
129 |
| - > modules used in your project. |
130 |
| -
|
131 |
| -1. Commit your changes |
132 |
| - |
133 |
| - ```bash |
134 |
| - git add . |
135 |
| - git commit -m "My first action is ready!" |
136 |
| - ``` |
137 |
| - |
138 |
| -1. Push them to your repository |
139 |
| - |
140 |
| - ```bash |
141 |
| - git push -u origin releases/v1 |
142 |
| - ``` |
143 |
| - |
144 |
| -1. Create a pull request and get feedback on your action |
145 |
| -1. Merge the pull request into the `main` branch |
146 |
| - |
147 |
| -Your action is now published! :rocket: |
148 |
| - |
149 |
| -For information about versioning your action, see |
150 |
| -[Versioning](https://github.com/actions/toolkit/blob/main/docs/action-versioning.md) |
151 |
| -in the GitHub Actions toolkit. |
152 |
| - |
153 |
| -## Validate the Action |
154 |
| - |
155 |
| -You can now validate the action by referencing it in a workflow file. For |
156 |
| -example, [`ci.yml`](./.github/workflows/ci.yml) demonstrates how to reference an |
157 |
| -action in the same repository. |
158 |
| - |
159 |
| -```yaml |
160 |
| -steps: |
161 |
| - - name: Checkout |
162 |
| - id: checkout |
163 |
| - uses: actions/checkout@v3 |
164 |
| - |
165 |
| - - name: Test Local Action |
166 |
| - id: test-action |
167 |
| - uses: ./ |
168 |
| - with: |
169 |
| - milliseconds: 1000 |
170 |
| - |
171 |
| - - name: Print Output |
172 |
| - id: output |
173 |
| - run: echo "${{ steps.test-action.outputs.time }}" |
| 88 | + ] |
| 89 | +} |
174 | 90 | ```
|
175 | 91 |
|
176 |
| -For example workflow runs, check out the |
177 |
| -[Actions tab](https://github.com/actions/javascript-action/actions)! :rocket: |
178 |
| -
|
179 |
| -## Usage |
| 92 | +For details on the required permissions, see the |
| 93 | +[AWS documentation on SSM](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-ssm-agent.html). |
180 | 94 |
|
181 |
| -After testing, you can create version tag(s) that developers can use to |
182 |
| -reference different stable versions of your action. For more information, see |
183 |
| -[Versioning](https://github.com/actions/toolkit/blob/main/docs/action-versioning.md) |
184 |
| -in the GitHub Actions toolkit. |
| 95 | +## Troubleshooting |
185 | 96 |
|
186 |
| -To include the action in a workflow in another repository, you can use the |
187 |
| -`uses` syntax with the `@` symbol to reference a specific branch, tag, or commit |
188 |
| -hash. |
| 97 | +### Command not executing |
189 | 98 |
|
190 |
| -```yaml |
191 |
| -steps: |
192 |
| - - name: Checkout |
193 |
| - id: checkout |
194 |
| - uses: actions/checkout@v4 |
195 |
| -
|
196 |
| - - name: Run my Action |
197 |
| - id: run-action |
198 |
| - uses: actions/javascript-action@v1 # Commit with the `v1` tag |
199 |
| - with: |
200 |
| - milliseconds: 1000 |
201 |
| - |
202 |
| - - name: Print Output |
203 |
| - id: output |
204 |
| - run: echo "${{ steps.run-action.outputs.time }}" |
205 |
| -``` |
| 99 | +- Ensure that the `workingDirectory` exists on the instance and that you have |
| 100 | + proper permissions. |
| 101 | +- Verify that the `commands` input is correctly formatted. |
0 commit comments