-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Hello World action #4
base: master
Are you sure you want to change the base?
Conversation
Write the Hello World source codeGreat job @bhautikpip, you now have action metadata for your first action. The next piece we will add will be the logic of our action. In this case, our logic will be written in Go. If you are not familiar with Go programming that's perfectly okay, you are here to learn about writing actions! All the necessary code for each language will be provided for you! The first iteration of our action will follow the traditional path of logging "Hello World" 👋to the console. We will expand on this as we move forward, for now it's a good test to make sure all of our files are set up correctly 😄 ⌨️ Activity: Create
|
Awesome 🎉 This action now has two of the three key files it needs to run:
Create the action's
|
Congratulations 🎉Your very first Docker action has been written and executed within a workflow! You may be wondering how I know that and the truth is that GitHub Actions provide real-time logging of the events happening within the runner! Since our action prints to the console we can just expand that step in our workflow and look at the standard output that is on our screen. You can do this in your own Actions tab or you can settle for examining the screenshot below to see our Hello World statement in place of where our previous errors existed. Whoa, that's a lot of output for a simple hello world! Don't let this alarm you, the output you see is from the 📖 Learn about Docker build |
Using input parametersA "Hello World" message is great, but let's personalize it a little bit. We will do this by adding an input parameter to the Although this example may seem a little lightweight input parameters have a very flexible use case. Consider a scenario where you need to access secret API key with your action, or when you need to specify the path to a given file. Inputs allows for these problems to be easily solved. A quick exampleTo add inputs we need to add the
Let's take a look at how this fits into an
name: "my hello action"
description: "say hello with actions"
inputs:
firstGreeting:
description: "who would you like to greet in the console"
required: true
default: "Hubot"
secondGreeting:
description: "another person to greet"
required: true
default: "Mona the Octocat"
thirdGreeting:
description: "a third greeting"
required: false The placement of your So what is actually happening here?Well, in the
Or we could leave them out and rely on their default values. The example below demonstrates a mix of both:
name: "Docker Actions"
on: [push]
jobs:
action:
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v1
- name: "hello-action"
uses: ./.github/actions/hello-world
with:
firstGreeting: "Learning Lab User" Now that there are inputs in the action's metadata the user can interface with them by supplying values. In this case Learning Lab User was passed as the value for the main.go package main
import (
"fmt"
"os"
)
func main() {
// Access Inputs as environment vars
firstGreeting := os.Getenv("INPUT_FIRSTGREETING")
secondGreeting := os.Getenv("INPUT_SECONDGREETING")
thirdGreeting := os.Getenv("INPUT_THIRDGREETING")
// Use those inputs in the actions logic
fmt.Println("Hello " + firstGreeting)
fmt.Println("Hello " + secondGreeting)
// Someimes inputs are not "required" and we can build around that
if thirdGreeting != "" {
fmt.Println("Hello " + thirdGreeting)
}
} In our actions source code we can access the inputs as if they are environment variables. GitHub Actions takes every For example:
How you access environment variables will vary by language 📖Read more about the input parameter |
Add input parameters to Hello World's metadataNow that we know what input parameters are, let's edit the metadata for our hello-world action. ⌨️ Activity: Update
|
Use input parameters in the source codeGreat job 👍 next let's update our source code to consume the inputs that are now defined. ⌨️ Activity: Extend Hello World's source code to accept the input parameters💡All of the following steps take place inside of the
I'll respond when you push changes to this pull request. |
Use input parameters in the workflowThe last piece to this actions puzzle is to edit the workflow file so that we can set custom values for these inputs. ⌨️ Activity: Assign values to the newly created input parameters in
|
Take a 👀 at what you made!Great Job 👍 making those changes @bhautikpip. I will take just a moment to walk you through what happened. If you look at the screenshot below you will see a very similar output to what your results should show. If you'd like you can open your own Actions tab to follow along. Your action now says hello to Learning Lab User which was the specified value for the What's interesting though, is that we also see Mona the Octocat and if you recall that is the value of the Why do we see the value of the If you remember, we made the Circling back to the Had you been explicit with |
Explore your input parametersTake a few minutes to play with the current code you have. I don't suggest editing the ⌨️ Activity: View the results of changing input parameters
When you are finished experimenting and examining the results merge this pull request into the master branch. Our next lesson will show you how to add external libraries to an action as well as interact with external APIs. When I notice that you have merged this branch I will open a new issue in your repository to continue guiding you. |
No description provided.