Actions are built by creating one or more workflow files. In this video, explore how they are created and learn how to use one of the existing templates to build one.
- [Instructor] At the heart of GitHub actions is the workflow. You create actions by building a series of workflow files in your repositories. So let's explore how they are created, and we'll use one of the existing templates to build one. Now to create a workflow, you create a series of YAML files which are just like TXT files, and they go in a special folder in the .github folder and also in the workflows folder inside that folder. You can create up to 20 of them at this time, and each of the workflow files will respond to a specific event. In addition to that, each one of these workflows can have a number of jobs that perform different actions, and this is also where you specify the platform that you want to run the action under. More on that later. The first time you create a workflow, you'll see that there are some templates that you can use to create your first workflow. We'll take a look at that in a minute. You'll be using the YAML format which is a little bit weird, but it helps to think of it as a JSON type format that has no punctuation. So it's a data format, easy to read, a little bit harder to write, but the tools in GitHub are going to make it easier for you to write. So let's go ahead and dig in. I'm going to start at this URL, and this is just a repo with a typical website, and I'm going to click on this Use This Template button right here, and then I'm going to give this a repository name. So you'd be doing this under your account. I'll call this github-actions. I'm going to create a repository from this template, and now I have something to play around with. You can do this on your own projects if you want to, but it's actually better to start off by creating a template repository or a brand new repository to play around with actions. So let's go ahead and add actions. First, I'm going to switch over here to this Actions tab, and you'll see that right now there aren't any workflows over here on the left-hand side, or the workflow is called Untitled. And the first thing that we can do here is either create a workflow by ourselves or use one of these different templates that appear right here on this list, but there's actually a lot more templates than this. You can click on this tab right here, and the one that we're looking for is this Simple Workflow action. So we'll go ahead and hit this Set Up This Workflow button, and that's going to create a file for us. Right now it's called blank.yml. Let's rename it firstaction. And you'll notice that it's creating a .github folder for us and then also a workflows subfolder, and it's placing this action in that folder structure. It's automatically created some text for us, giving the workflow a name. We can change that if we want to. And this workflow will respond to a specific event which is the push event. So whenever somebody pushes something to this repository, it's going to execute a series of jobs. The first job that it's going to execute is going to be called build, and we want this job to run on a Linux machine which will be a Ubuntu machine with the latest version, and then we define a series of steps. Now this is going to use an existing action called checkout@v1 and essentially echoes something using a terminal. So we're going to give each one of these commands a name, and then this command will execute something, and it'll just be an echo of Hello World which will print Hello World, and then the second task that we want this to execute is going to allow you to run a script in multiple lines. So all it's going to do is echo two lines, and they are right here, and you can modify these if you want. Notice the indentation is important. Whenever you see a dash like this, it means that you're creating a list of sorts. So each one of these elements is part of the list. Alright, let's go ahead and start the commit. We'll go ahead and commit the new file, and once you do that, it's going to generate essentially a push on your repository which is going to trigger the action. If you click on the Actions tab, it's going to take sometimes awhile for it to get started, but you can see the Hello Action is already right here. It should be appearing in under All Workflows, and it might after awhile, and there it is. So you see that it's running the action, and you can actually click on this action to see what is going on in real time with the action. So you can see that it actually ran so quickly that we didn't get to see this in sequence, but it would run through each of the tasks and execute them, and if you open these up, you can see that the one-line script is just hello world, and then the multi-line script is the other text that we added in there, and then it completed the job. So that's pretty cool. This is where you can look at how the action is executing. If you want to go back into the action, then just click on the Actions tab again, and if you want to edit this text, just go right here and select View Workflow File, and you can edit the action some more just like you would with anything on GitHub. You can hit Edit This File and edit these different actions. So that's how you create the most basic action using an existing template. Let's dig into some of the other features of actions in the next video.