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