{"id":15640956,"url":"https://github.com/timja/training-writing-javascript-actions","last_synced_at":"2026-03-18T03:56:30.907Z","repository":{"id":85475401,"uuid":"602013938","full_name":"timja/training-writing-javascript-actions","owner":"timja","description":null,"archived":false,"fork":false,"pushed_at":"2023-06-01T06:58:12.000Z","size":1809,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-30T13:14:56.018Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc-by-4.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timja.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-15T10:11:52.000Z","updated_at":"2023-02-15T10:11:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"cfabb9db-10e3-436c-827e-613421bba14c","html_url":"https://github.com/timja/training-writing-javascript-actions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":"skills/write-javascript-actions","purl":"pkg:github/timja/training-writing-javascript-actions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timja%2Ftraining-writing-javascript-actions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timja%2Ftraining-writing-javascript-actions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timja%2Ftraining-writing-javascript-actions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timja%2Ftraining-writing-javascript-actions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timja","download_url":"https://codeload.github.com/timja/training-writing-javascript-actions/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timja%2Ftraining-writing-javascript-actions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30646256,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-18T02:48:56.676Z","status":"ssl_error","status_checked_at":"2026-03-18T02:48:55.747Z","response_time":104,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T11:40:44.407Z","updated_at":"2026-03-18T03:56:30.889Z","avatar_url":"https://github.com/timja.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!--\n  \u003c\u003c\u003c Author notes: Header of the course \u003e\u003e\u003e\n  Include a 1280×640 image, course name in sentence case, and a concise description in emphasis.\n  In your repository settings: enable template repository, add your 1280×640 social image, auto delete head branches.\n  Next to \"About\", add description \u0026 tags; disable releases, packages, \u0026 environments.\n  Add your open source license, GitHub uses Creative Commons Attribution 4.0 International.\n--\u003e\n\n# GitHub Actions: Writing JavaScript Actions\n\n\u003c!--\n  \u003c\u003c\u003c Author notes: Start of the course \u003e\u003e\u003e\n  Include start button, a note about Actions minutes,\n  and tell the learner why they should take the course.\n  Each step should be wrapped in \u003cdetails\u003e/\u003csummary\u003e, with an `id` set.\n  The start \u003cdetails\u003e should have `open` as well.\n  Do not use quotes on the \u003cdetails\u003e tag attributes.\n--\u003e\n\n\u003cdetails id=0\u003e\n\u003csummary\u003e\u003ch2\u003eWelcome\u003c/h2\u003e\u003c/summary\u003e\n\nWrite your own GitHub JavaScript Action and automate customized tasks unique to your workflow.\n\n- **Who is this for**: Developers, GitHub users, users new to Git, students, managers, and for teams.\n- **What you'll learn**: How to consume actions within a workflow file, create custom JavaScript based actions and publish your newly created action to the marketplace.\n- **Prerequisites**: Before you start, you should be familiar with GitHub, GitHub Actions, and Continuous Integration with GitHub Actions.\n- **How long**: This course is 6 steps long and takes about 1 to 2 hours to be completed.\n\n## How to start this course\n\n1. Right-click **Start course** and open the link in a new tab.\n   \u003cbr /\u003e[![start-course](https://user-images.githubusercontent.com/1221423/218596841-0645fe1a-4aaf-4f51-9ab3-8aa2d3fdd487.svg)](https://github.com/skills/writing-javacript-actions/generate)\n2. In the new tab, follow the prompts to create a new repository.\n   - For owner, choose your personal account or an organization to host the repository.\n   - We recommend creating a public repository—private repositories will [use Actions minutes](https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions).\n   ![Create a new repository](https://user-images.githubusercontent.com/1221423/218594143-e60462b6-9f2a-4fa3-80de-063ac5429aab.png)\n3. After your new repository is created, wait about 20 seconds, then refresh the page. Follow the step-by-step instructions in the new repository's README.\n\n\u003c/details\u003e\n\n\n\u003c!--\n  \u003c\u003c\u003c Author notes: Step 1 \u003e\u003e\u003e\n  Choose 3-5 steps for your course.\n  The first step is always the hardest, so pick something easy!\n  Link to docs.github.com for further explanations.\n  Encourage users to open new tabs for steps!\n  TBD-step-1-notes.\n--\u003e\n\n### Configuring a workflow\n\nActions are enabled on your repository by default, but we still have to tell our repository to use them. We do this by creating a workflow file in our repository.\n\nA **workflow** file can be thought of as the recipe for automating a task. They house the start to finish instructions, in the form of `jobs` and `steps`, for what should happen based on specific triggers.\n\nYour repository can contain multiple **workflow** files that carry out a wide variety of tasks. It is important to consider this when deciding on a name for your **workflow**. The name you choose should reflect the tasks being performed.\n\n_In our case, we will use this one **workflow** file for many things, which leads us to break this convention for teaching purposes._\n\nRead more about [workflows](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/configuring-a-workflow#choosing-the-type-of-actions-for-your-workflow)\n\n\u003cdetails id=1\u003e\n\n\u003csummary\u003e\u003ch2\u003e Step 1: Initialize a new JavaScript project\u003c/h2\u003e\u003c/summary\u003e\n\n## On to your development environment\n\nOur JavaScript actions are going to leverage the [GitHub ToolKit](https://github.com/actions/toolkit) for developing GitHub Actions.\n\nThis is an external library that we will install using `npm` which means that you will need [Node.js](https://nodejs.org/) installed.\n\nWe find writing actions to be easier from a local environment vs trying to do everything right here in the repository. Doing these steps locally allows you to use the editor of your choice so that you have all the extensions and snippets you are used to when writing code.\n\nIf you do not have a preferred environment then we suggest following along exactly as you see on the screen, which means you'll need to install [Visual Studio Code](https://code.visualstudio.com/).\n\n## Don't forget to set up your workstation\n\nMost of your work going forward will take place away from your Skills repository, so before continuing with the course ensure you have the following installed on your **local machine**.\n\n1. [ ] [Node.js](https://nodejs.org)\n2. [ ] [Visual Studio Code](https://code.visualstudio.com/) or your editor of choice\n3. [ ] [Git](https://git-scm.com/)\n  \n### Activity 1: Initialize a new JavaScript project\n\nOnce you have the necessary tools installed locally, follow these steps to begin creating your first action.\n\n1. Open the **Terminal** (Mac and Linux) or **Command Prompt** (Windows) on your local machine\n2. Clone your Skills repo to your local machine:\n   ```shell\n   git clone \u003cthis repository URL\u003e.git\n   ```\n3. Navigate to the folder you just cloned:\n   ```shell\n   cd \u003clocal folder with cloned repo\u003e\n   ```\n4. We are using branch called `main`. \n   ```shell\n   git switch main\n   ```\n5. Create a new folder for our actions files:\n   ```shell\n   mkdir -p .github/actions/joke-action\n   ```\n6. Navigate to the `joke-action` folder you just created:\n   ```shell\n   cd .github/actions/joke-action\n   ```\n7. Initialize a new project:\n   ```shell\n   npm init -y\n   ```\n8. Install the **request**, **request-promise** and **@actions/core** dependencies using `npm` from the [GitHub ToolKit] (https://github.com/actions/toolkit):\n   ```shell\n   npm install --save request request-promise @actions/core\n   ```\n9. Commit those newly added files,we will remove the need to upload **node_modules** in a later step:\n   ```shell\n   git add .\n   git commit -m 'add project dependencies'\n   ```\n10. Push your changes to your repository:\n    ```shell\n    git push\n    ```\n11. Wait about 20 seconds then refresh this page for the next step.\n\n\u003c/details\u003e\n  \n\u003cdetails id=2\u003e\n\u003csummary\u003e\u003ch2\u003e Step 2: Configure Your Action\u003c/h2\u003e\u003c/summary\u003e\n\n### Excellent!\n  \nNow that we have the custom action pre-requisites, let us create **joke-action** action.\n\n### Activity 1: Configure Your Action\n\nAll of the following steps take place inside of the `.github/actions/joke-action` directory.\n\nWe will start with using the parameters that are **required** and later implement some optional parameters as our action evolves.\n\n1. Create a new file in: `.github/actions/joke-action/action.yml`\n2. Add the following contents to the `.github/actions/joke-action/action.yml` file:\n   ```yaml\n   name: \"my joke action\"\n\n   description: \"use an external API to retrieve and display a joke\"\n\n   runs:\n     using: \"node12\"\n     main: \"main.js\"\n   ```\n3. Save the `action.yml` file\n4. Commit the changes and push them to the `main` branch:\n   ```shell\n   git add action.yml\n   git commit -m 'create action.yml'\n   git pull\n   git push\n   ```\n5. Wait about 20 seconds then refresh this page for the next step.\n\n\u003c/details\u003e\n  \n\u003cdetails id=3\u003e\n\u003csummary\u003e\u003ch2\u003e Step 3: Create the metadata file\u003c/h2\u003e\u003c/summary\u003e\n\n## Action metadata\n\nEvery GitHub Action that we write needs to be accompanied by a metadata file. This file has a few rules to it, as are indicated below:\n\n- Filename **must** be `action.yml`.\n- Required for both Docker container and JavaScript actions.\n- Written in YAML syntax.\n\nThis file defines the following information about your action:\n\n| Parameter   | Description                                                                                                                                            |      Required      |\n| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------: |\n| Name        | The name of your action. Helps visually identify the actions in a job.                                                                                 | :white_check_mark: |\n| Description | A summary of what your action does.                                                                                                                    | :white_check_mark: |\n| Inputs      | Input parameters allow you to specify data that the action expects to use during runtime. These parameters become environment variables in the runner. |        :x:        |\n| Outputs     | Specifies the data that subsequent actions can use later in the workflow after the action that defines these outputs has run.                          |        :x:        |\n| Runs        | The command to run when the action executes.                                                                                                           | :white_check_mark: |\n| Branding    | You can use a color and Feather icon to create a badge to personalize and distinguish your action in GitHub Marketplace.                               |        :x:        |\n\n---\n\nRead more about [Action metadata](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/metadata-syntax-for-github-actions)\n\n### Activity 1: Create the metadata file\n  \nAll of the following steps take place inside of the `.github/actions/joke-action` directory.\n\nOur action does not require much metadata for it to run correctly. We will not be accepting any inputs, we will however be setting a single output this time.\n\n1. Update the action metadata file `.github/actions/joke-action/action.yml` with the following content:\n   ```yaml\n   name: \"my joke action\"\n\n   description: \"use an external API to retrieve and display a joke\"\n\n   outputs:\n     joke-output:\n       description: The resulting joke from the icanhazdadjokes API\n\n   runs:\n     using: \"node12\"\n     main: \"main.js\"\n   ```\n2. Save the `action.yml` file\n3. Commit the changes and push them to GitHub:\n   ```shell\n   git add action.yml\n   git commit -m 'add metadata for the joke action'\n   git pull\n   git push\n   ```\n4. Wait about 20 seconds then refresh this page for the next step.\n\n\u003c/details\u003e\n  \n\u003cdetails id=4\u003e\n\u003csummary\u003e\u003ch2\u003e Step 4: Create the JavaScript files for your action\u003c/h2\u003e\u003c/summary\u003e\n\n## Files\n\nAs you probably know, in JavaScript and other programming languages it is common to break your code into modules so that it is easier to read and maintain going forward. Since JavaScript actions are just programs written in JavaScript that run based on a specific trigger we are able to make our action code modular as well.\n\nTo do so we will create two files. One of them will contain the logic to reach out to an external API and retrieve a joke for us, the other will call that module and print the joke to the actions console for us. We will be extending this functionality in our third and final action.\n\n### Fetching a joke\n\n**Joke API**\n\n\nThe first file will be `joke.js` and it will fetch our joke for us. We will be using the [icanhazdadjoke API](https://icanhazdadjoke.com/api) for our action. This API does not require any authentication, but it does however that we set a few parameters in the [HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers). We need to point out what those are when we get to the code, however it is outside of the scope of this course to cover HTTP in any depth.\n\nWhen we make our request to this API we will get back a JSON Object in the response. That Object looks like this:\n\n```\n{\n  id: '0LuXvkq4Muc',\n  joke: \"I knew I shouldn't steal a mixer from work, but it was a whisk I was willing to take.\",\n  status: 200\n}\n```\n\nIt contains 3 key/value pairs of data that we can use in our own program or service. In our case, we are only interested in the `joke` field.\n\n**Joke Module**\n\nWe will create a file named `joke.js` and it will reside in the `.github/action/joke-action` directory.\n\nThe joke module will look like this:\n\n```javascript\nconst request = require(\"request-promise\");\n\nconst options = {\n  method: \"GET\",\n  uri: \"https://icanhazdadjoke.com/\",\n  headers: {\n    Accept: \"application/json\",\n    \"User-Agent\":\n      \"Writing JavaScript action GitHub Skills course.\"\n  },\n  json: true\n};\n\nasync function getJoke() {\n  const res = await request(options);\n  return res.joke;\n}\n\nmodule.exports = getJoke;\n```\n\n\u003cdetails\u003e\u003csummary\u003eNeed an advanced description of the \u003ccode\u003ejoke.js\u003c/code\u003e source code?\u003c/summary\u003e\nWe first bring in the `request-promise` library that we installed earlier using `npm`.\n\nNext we define a set of `options` that the `request-promise` library will use when it makes the request.\n\nRead more about [request-promise](https://github.com/request/request-promise/)\n\nInside of the `options` block we add a key named `headers`. This defines the HTTP headers that the **icanhazdadjoke** API expects in each request that comes it's way.\n\n**icanhazdadjoke** cares the most about the keys, **Accept** and **User-Agent**, so we need to make sure we fill them in.\n\nNext we define an **asynchronous JavaScript function** to make the request for us, storing the JSON Object that is returned in a variable named `res`.\n\nLastly, we `return` the `res.joke` which is only the value associated with the `joke` key of the JSON Object. This value will be random every time our action runs because of how we are interacting with the **icanhazdadjoke** API.\n\nThis file finishes up by exporting the newly created function so that we can use it in our `main.js` file.\n  \n\u003c/details\u003e\n\n### Creating the main entry point for your action\n\n**Main Module**\n\nWe will also create a file named `main.js` that resides inside of the `.github/actions/joke-action` directory.\n\nThat file will look like this:\n\n```javascript\nconst getJoke = require(\"./joke\");\nconst core = require(\"@actions/core\");\n\nasync function run() {\n  const joke = await getJoke();\n  console.log(joke);\n  core.setOutput(\"joke-output\", joke);\n}\n\nrun();\n```\n\n\u003cdetails\u003e\u003csummary\u003eNeed an advanced description of the \u003ccode\u003emain.js\u003c/code\u003e source code?\u003c/summary\u003e\nLike we did in the `joke.js` file, we are first going to bring in our dependencies. Only this time, our dependencies include something we wrote! To do that we simply use `require()` to point to the location of the file we wish to bring in.\n\nWe also bring in `@actions/core` so that we can set the output of our action.\n\nNext we write another **asynchronous JavaScript function** that stores the return value of `getJoke()` in a variable called **joke**.\n\nThen we log the joke to the console.\n\nFinally we finish the function with by setting the contents of the joke as the value of the `joke-output` output parameter. We will use this output later in the course.\n_Don't forget to call the `run()` function._\n\n\u003c/details\u003e\n  \n### Activity 1: Creating the JavaScript files for your new action.\n\n1. Create and add the following contents to the `.github/actions/joke-action/joke.js` file:\n\n   ```javascript\n   const request = require(\"request-promise\");\n\n   const options = {\n     method: \"GET\",\n     uri: \"https://icanhazdadjoke.com/\",\n     headers: {\n       Accept: \"application/json\",\n       \"User-Agent\":\n         \"Writing JavaScript action GitHub Skills course.\"\n     },\n     json: true\n   };\n\n   async function getJoke() {\n     const res = await request(options);\n     return res.joke;\n   }\n\n   module.exports = getJoke;\n   ```\n\n2. Save the `joke.js` file.\n3. Create and add the following contents to the `.github/actions/joke-action/main.js` file:\n\n   ```javascript\n   const getJoke = require(\"./joke\");\n   const core = require(\"@actions/core\");\n\n   async function run() {\n     const joke = await getJoke();\n     console.log(joke);\n     core.setOutput(\"joke-output\", joke);\n   }\n\n   run();\n   ```\n\n4. Save the `main.js` file.\n5. Commit the changes to this branch and push them to GitHub:\n   ```shell\n   git add joke.js main.js\n   git commit -m 'creating joke.js and main.js'\n   git pull\n   git push\n   ```\n\n\u003c/details\u003e\n  \n\u003cdetails id=5\u003e\n\u003csummary\u003e\u003ch2\u003e Step 5: Add your action to the workflow file\u003c/h2\u003e\u003c/summary\u003e\n\n### Great job!\n  \nAll of the following steps will add the action to the workflow file that’s already in the repo [`my-workflow.yml` file](/.github/workflows/my-workflow.yml)\n  \n### Activity 1: Edit the custom action at the bottom of the workflow file.\n\n```yaml\n      - name: ha-ha\n        uses: ./.github/actions/joke-action\n```\n\nHere is what the full file should look like (we’re using issues instead of the pull request event and removing the reference to the hello world action.) \n\n```yaml\nname: JS Actions\n\non:\n  issues:\n    types: [labeled]\n\njobs:\n  action:\n    if: ${{ !github.event.repository.is_template }}\n    runs-on: ubuntu-latest\n\n    steps:\n      - uses: actions/checkout@v3\n      - name: ha-ha\n        uses: ./.github/actions/joke-action\n\n```\n  \n\u003c/details\u003e\n  \n\u003cdetails id=6\u003e\n\n\u003csummary\u003e\u003ch2\u003e Step 6: Trigger the joke action\u003c/h2\u003e\u003c/summary\u003e\n\n### Great job! \nEverything is all set up and now we are ready to start laughing. You will find you have some joke related labels available to you in this repository. You don't have to use them, any label will trigger our workflow, but the easiest way to follow along would be to use suggested labels.\n\n### Trigger a joke\n\n1. Open issue #1 in the \"Issues tab\"\n2. Apply the `first-joke` label to the issue\n3. Wait a few seconds and then apply the `second-joke` label to the issue\n4. Check the workflow results on the \"Actions tab\"\n \n\u003c/details\u003e\n\n\u003cdetails id=X open\u003e\n\u003csummary\u003e\u003ch2\u003eFinish\u003c/h2\u003e\u003c/summary\u003e\n\n### Congratulations, you've completed this course! \n\nIn this course, you've learned a lot about developing custom actions using JavaScript and Actions Toolkit.\n\n## Publishing your actions\n\nPublishing your actions is a great way to help others in your team and across the GitHub community. Although actions do not need to be published to be consumed, by adding them to the marketplace you make them easier to find.\n\nSome notable actions you will find on the marketplace are:\n\n- [Actions for Discord](https://github.com/marketplace/actions/actions-for-discord)\n- [GitHub Action for Slack](https://github.com/marketplace/actions/github-action-for-slack)\n- [Jekyll action](https://github.com/marketplace/actions/jekyll-action)\n- [Run Jest](https://github.com/marketplace/actions/run-jest)\n\nAnd that just scratches the surface of the 1600+ and counting actions you will find on the marketplace \n\nFollow [this guide](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/publishing-actions-in-github-marketplace#publishing-an-action) to learn how to publish your actions to the GitHub Marketplace\n  \n### What's next?\n\n- [Take another GitHub Skills course](https://github.com/githubskills).\n- [Read the GitHub Getting Started docs](https://docs.github.com/en/get-started).\n- To find projects to contribute to, check out [GitHub Explore](https://github.com/explore).\n\n\u003c/details\u003e\n\n---\n\nGet help: [Post in our discussion board](https://github.com/skills/.github/discussions) \u0026bull; [Review the GitHub status page](https://www.githubstatus.com/)\n\n\u0026copy; 2022 GitHub \u0026bull; [Code of Conduct](https://www.contributor-covenant.org/version/2/1/code_of_conduct/code_of_conduct.md) \u0026bull; [CC-BY-4.0 License](https://creativecommons.org/licenses/by/4.0/legalcode)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimja%2Ftraining-writing-javascript-actions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimja%2Ftraining-writing-javascript-actions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimja%2Ftraining-writing-javascript-actions/lists"}