HolkarA / write-docker-actions

https://lab.github.com/githubtraining/github-actions:-write-docker-container-actions
MIT License
0 stars 0 forks source link

Use outputs #8

Open HolkarA opened 2 years ago

HolkarA commented 2 years ago

Use outputs

github-learning-lab[bot] commented 2 years ago

HolkarA it's time to get ready for the third action: issue-maker πŸŽ‰

As with the other actions we wrote, we are going to need to setup a few directories and files.

Add dependencies for issue-maker action

This time we will start with the dependencies for our action. JavaScript projects can be packaged with a package.json file which contains metadata and configuration information about a project. In our case we will use some pieces of the actions toolkit.

:keyboard: Activity: Add package.json to issue-maker

  1. Create and add the following contents to the .github/actions/issue-maker/package.json file: You can use this link to easily create this file.

    {
     "name": "issue-maker",
     "version": "1.0.0",
     "description": "",
     "main": "index.js",
     "scripts": {
       "test": "echo \"Error: no test specified\" && exit 1"
     },
     "keywords": [],
     "author": "",
     "license": "ISC",
     "dependencies": {
       "@actions/core": "^1.2.2",
       "@actions/github": "^2.1.0"
     }
    }
  2. Commit the changes to this branch.

  3. Click the green Commit new file button


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 2 years ago

Create the issue-maker's action metadata

Like our "hello world" action, this action will require at least one input: parameter. We need this parameter so that our JavaScript for this action has access to the output: from the joke action.

If you recall, in the my-workflow.yml file, we stated this action would take a specific input named catFact: and we set it's value to the output of the previous action.

- name: create-issue
  uses: ./.github/actions/issue-maker
  with:
    catFact: ${{steps.cat.outputs.fact}}

Because of this, we need to define catFact: as one of our inputs: for this action. Remember when we did this with the first action? It looked a little like this:

inputs:
  catFact:
    description: "the cat fact retreived from a previous action"
    required: true
    default: "Mona is an Octocat"

We also will also need to authenticate to GitHub with the action so that we can interact with the GitHub API. For that we will use a special token that gets created for us when we use actions called GITHUB_TOKEN

Our action will also need to accept an input so that we can let the user specify a name for the issue that will be created when this action runs.

:keyboard: Activity: Add action.yml for issue-maker

πŸ’‘All of the following steps take place inside of the .github/actions/issue-maker directory.

  1. Create and add the following contents to the .github/actions/issue-maker/action.yml file: You can use this link to easily create this file.

    name: "issue maker"
    
    description: "create and issue with a cat fact as the body"
    
    inputs:
     issueTitle:
       description: "A name for the cat-fact issue"
       required: true
       default: "A cat fact for you"
    
     catFact:
       description: "the cat fact retreived from a previous action"
       required: true
       default: "Mona is an Octocat"
    
     repoToken:
       description: "Authentication token, use secrets.GITHUB_TOKEN"
       required: true
    
    runs:
     using: "docker"
     image: "Dockerfile"
  2. Commit the changes to this branch.

  3. Click the green Commit new file button


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 2 years ago

Add the issue-maker action's source code

:keyboard: Activity: Add src/index.js for issue-maker

πŸ’‘All of the following steps take place inside of the .github/actions/issue-maker/src directory.

  1. Create and add the following contents to the .github/actions/issue-maker/src/index.js file: You can use this link to easily create this file.

    const core = require("@actions/core");
    const github = require("@actions/github");
    
    async function run() {
     const issueTitle = core.getInput("issueTitle");
     const catFact = core.getInput("catFact");
    
     const token = core.getInput("repoToken");
     try {
       const octokit = new github.GitHub(token);
    
       const newIssue = await octokit.issues.create({
         repo: github.context.repo.repo,
         owner: github.context.repo.owner,
         title: issueTitle,
         body: catFact
       });
     } catch (error) {
       core.setFailed(error.message);
     }
    }
    
    run();
  2. Commit the changes to this branch.

  3. Click the green Commit new file button


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 2 years ago

Add the issue-maker action's Dockerfile

:keyboard: Activity: Create Dockerfile for issue-maker

One more piece to add and that is this actions Dockerfile. Once you complete this you will have all the pieces in place to use your final action!

  1. Create and add the following contents to .github/actions/issue-maker/Dockerfile: You can use this link to easily create this file.

    FROM node:slim
    
    COPY package*.json ./
    
    RUN npm install
    
    COPY . .
    
    CMD [ "node", "/src/index.js" ]
  2. Commit the changes to this branch

  3. Click the green Commit new file button

πŸ“– Become a Dockerfile guru


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 2 years ago

Trigger all the things πŸŽ‰

Let's trigger your new workflow!

  1. Add a label to this pull request, it can be any label you choose.
  2. After adding a label head over to your Actions tab if you want to watch the workflow.
  3. Once your workflow has completed check your issues tab and you should see a new issue with a cat fact as the body!

Continue experimenting with this workflow for as long as you'd like.

Try adding another label and see if you get a new fact!


When you have finished experimenting, merge this pull request and meet me in this issue to finish wrapping things up.