Modern Python Projects Transcripts
Chapter: CI
Lecture: GitHub Actions in action

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's see GitHub actions in action. I have pushed my calculator project to GitHub, and now I want to add some GitHub actions to it.
0:10 Setting up a basic workflow is quite easy. You can go to the actions tab, and select the workflow most suitable to your project, as you can see GitHub
0:24 has automatically detected that this is a Python project and I have some suggestions at the top. But down there I have even more options.
0:38 So, here we have to select which workflow we want to start with. Don't worry if none of them looks perfect, you can always edit it by hand.
0:47 So, just like with a cookie cutter template, select something that looks decent and then we will modify it. Let's go with the Python application.
0:59 Now we have a chance to modify this configuration file and then we can click, start commit to commit it to our repository.
1:07 Name is how this GitHub Action will be displayed in the interface, so we can change it to whatever we want
1:12 Next we have to define when we want to run this action. With this configuration, we will run it each time we push a new commit to
1:26 master branch, and also each time we create a new pull request to the master branch. And that's actually what I want to have.
1:33 So let's leave it like that. Next we specified that we want to use the latest version of Ubuntu image and then we have our steps. In the first step,
1:42 we check out the code, then we set up Pytho 3.8. If you want, you can specify multiple Python versions,
1:49 but 3.8 is good enough. Then we install some dependencies. So first we install an update.
1:56 pip,Then we install Flake ate pytest that we're going to use later. And then if we have requirements.txt file.
2:05 We also install dependencies from that file. So again, this is a very good setup. I'm going to leave it next.
2:12 We run Flake 8 twice, each time with different arguments. I will leave it for now as it is, to see how it works. And then in the last step,
2:20 we run pytest. So Actually, this is a very good setup, so let's use it. And once we're happy with it, let's add black later on. So I click,
2:31 Start commit I need a commit message and we're all set. Let's go back to our code.
2:45 Let's modify some files and let's try to create a new pull request to see that our GitHub action is working. So first,
2:53 let's do git pull to get our latest code from github. Now let's go to the code editor. And let's, for example, modify one of the tests.
3:04 Let's change it, so it fails. Now let's try to commit this code. As you can see, I'm not running Tox, and I have pre-commit disabled.
3:21 So nothing stops me from committing a broken test with code repository.
3:27 When we refresh the repository, you can see that there is this yellow dots suggesting that there is something happening in the GitHub actions.
3:35 When we click it, you see that we have one check in progress. Let's open the details and it's broken, our GitHub
3:46 action has failed during the pytest phase, and here we have the error message. You can click around to see the interface,
3:58 and once you're done, you can go back to the code and you can fix it. This time let's try to create a new branch and let's use a pull
4:13 request with this new commit. So we have our first pull request.
4:49 As you can see, the gitlab actions are still running. It should take around half a minute. We can click the details. Okay,
5:01 Now it's starting. So as you can see, those are all the steps that we set up. So, first we check out our code. Then we set up Python version.
5:10 Next we install dependencies. We linked with, Flake 8. We test and now we have this green check,
5:18 which means that we have just fixed our test, if we open it. There is not much happening here because there was no error code
5:27 currently, so we can go back to our pull request. You can see this green check mark here, which means that the GitHub actions are passing.
5:38 So now we know that we can safely merge this pull request and it's going to fix our tests. Great. So one last thing that I want to set up
5:49 is to use black to format, our code. Let's do this directly in the code editor. Let's open our GitHub Workflows configuration file,
5:58 and we actually have to figure out how to add black. So let's go back to the browser and the best place to search for available GitHub
6:06 actions is the GitHub marketplace. So here we can set for Black to see I already search in the past.
6:14 And there are some results. None of them seems to be like the oficial black workflow from the PSF. So, I will just select first one,
6:25 and this one looks pretty easy to use. So, actually, if we want to run in with default settings, we just need this one line. We add a new step here.
6:39 Actually, I can skip the name and I can do this like that. Let's actually run black before running tests. Okay, let's see if it works.
6:57 Okay. just this one file. I'm in the wrong branch. Okay, I can push it directly to Master.
7:30 Don't do get push first I'm just too lazy to untangle this mess. Okay, We should be set up. Let's see if our GitHub actions are still working.
7:42 This might take a bit longer because we just added than new plug in, but in the future, GitHub should use the cache, so it's going to be faster.
7:56 Okay, everything looks fine. So one last thing. Let's go back to our code and let's try to break it So black has something
8:05 to complain about. Again Let's remove some white spaces. That's the easiest way. That's what happens when you don't set up your git repository
8:29 correctly. As you saw, it took a moment before the GitHub actions kicked
8:56 in. So, we saw this green button saying that it's ready to merge. But after a few seconds, it actually disappeared and our GitHub action started.
9:08 So, let's wait for it to finish. Let's see the details, so we actually have something to look at.
9:16 So using black as a plugin seems to be kind of slow because it takes one minute to set it up. So maybe a better idea would be to actually go
9:26 to your git hub actions and in the install dependencies. Just add black here and here you just run command black. just like flake8
9:34 does. Let's go back to our GitHub action to see And it failed. For some reason. I don't get the output, When you refresh,
9:46 you can see the full output from this command. And then black says one file would be reformatted, two files would be left unchanged.
9:55 And we have this huge red icon indicating that there is something wrong with this build
9:58 So now you know that you actually have to go back to this pull request and you have to fix it.
10:09 I will leave this task to you because this lesson is already becoming quite long.


Talk Python's Mastodon Michael Kennedy's Mastodon