Schedule Python Scripts with GitHub Actions FOR FREE | Python Automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
python is awesome for automation and today i want to show you how we can schedule and run python scripts with github actions for free so basically this is like a hosted python script in the cloud that can do any automation task we want and i use this for several personal projects for example i have this tweet scraper project this runs once a day and looks at my twitter timeline and if it finds a popular tweet then it updates this in the readme and it also stores the tweet in a new file and all these changes are committed automatically so this is also something we can do with a github action so for example if we have a look at the commit timeline we see the initial commit is by me and all the other ones are committed by this actions user so this is super helpful and also easy to set up and then i also want to show you this cool project i found by ethan rosenthal he uses github actions to run once a week and then back up his spotify discover weekly playlist so i think this is a super cool project and earns a star and if you also have a cool idea for what we can use github actions and python scripts and drop this in the comments below and now let's get into it so first of all what are github actions github actions are used to automate all your software workflows and usually they are used for cicd pipelines so when you push something to the repository then this action will be triggered and can for example execute some tests so this is a very common use case but what most people don't know is that we can also run github actions on a chrome job basis so basically at any time we specify and this is what we do in this example and all of this can be done for free because the free tier in github includes 2 000 minutes of github actions and this is more than enough for several projects because on average i think most of my actions if we have a look at them they take only 22 seconds so yeah this is more than enough to run the scripts for free and now let's see how we can set up these actions so to get started i created this python github action template and i will put the link in the description so let's go briefly over this so first we have a dot github workflows directory this is the most important part which i will show you in a moment then we have a dot git ignore a readme a main.pie and in here we use logging and requests and then we call a weather api and lock the information so you can see we also have a log file so it will lock this into a file we can see whether in berlin is 35 degrees celsius it's getting hot today and then you also see that it locks a token value so i will also show you how we can store and access a secret token for example an api token so this is a very common use case you might need then we also have a requirements txt so in this case it installs requests so i show you how you can use third-party packages as well so this project here covers more or less all the normal use cases in a python project and one thing i want to show you so this is scheduled to run every two hours and if we have a look at the commits then again you will see the first ones are by me and then here it updated the locks and this was all done by this action user and you see this was 25 minutes ago two hours four hours six seven hours so this is one thing i want to note here that this is not super precise to the exact minute or hour and if you need something more precise then a github action is not the solution for you then you might look into another paid solution but for my use cases this is perfectly fine so for some projects i schedule them once a day or also every two hours approximately and this is fine yeah so this is how it works and now in order to set up a github action we need this so let's have a look at this so to set up a github action we need to create a folder that is called dot github and then an inner folder that is called workflows so it needs to have this name and here we create a action.cml file and here is where we define how this looks like so we give it a name run main.pi then we define on so when this will be triggered for example here you can also say on a push to the repository in our case we say schedule and then use a cron syntax and in this case that this should run every two hours if you don't know how the syntax works then i have a tutorial for you on my channel that i can link here i can also point you to this very helpful website crontab.guru so here you can play around with this for example if you say zero and then stars then it will run at every full hour and for example with this you can do a step so at minute zero passed every second hour this is what we use here and now we set up the jobs so here we can define different steps so the first step is to check out the repo content and for this we use an existing actions so actions checkout then we set up python so for this we use actions setup python version four and you can by the way find them on the official actions repository on github so you can search for them and then have a look at all the available ones so in this case we use setup python and we specify the python version then we install python packages if we have third-party packages so we can run a terminal command and say python dash m pip install dash upgrade pip and then pip install dash requirements txt and now we can finally run our remainder pi so we say run python main.pi and we can also specify a environment with the secret variables so in this case it gets the name some secret and then we have to use the same name in the python script and also when we save this in github i will show you this in a moment and then with this we can commit the files and then push the changes with the existing github push action so yeah this is what you need to do in order to run a python script as github action and then in the repository you find this actions tab so you can click on this and here you have an overview of all the past runs so you so you see the name run main.pi this was one hour ago and it took 60 seconds 16 seconds and then three hours ago and 21 seconds and so on so yeah this is what you have to do and now let's have a look at how you can do this for your own project step by step so to set this up of course you can clone this code here but i want to show you how to do this for a brand new repository so let's create a new one and let's give this a name let's call this actions test then we can specify public or private it also works for private ones and then we can click on create and now you select if you use https or ssh in my case i use ssh to manage my credentials and now we um copy so you can do this to create a new repository or push an existing one i copy this and now let's grab this and go to the terminal then i create a new directory with the same name and i want to cd into this and now i paste all these commands and i need to do a slight modification but this is only because i manage multiple github accounts on one machine so i need to give it this specifier you don't have to do this and now if we execute this and then refresh the page then now here we have our readme markdown file and now we can open our editor and then we can set up the action and a python script so when i start a new project i often create a virtual environment with this command and then i activate it with this command and now we can pip install the packages we need then let's create our main dot pi then like i said we need to create a folder that is called github and then here an inner folder that is called workflows and now here we create the actions.yaml then i also want to create a dot get ignore and now let's copy the content from here so i want this and then you can click on grep and then you can put this in here this will just ignore some common names like the environments then let's go back and select the yaml file and again copy the whole thing and paste this in here of course you can adapt this so feel free to modify the schedule or the steps for example if you don't need this token you can remove this or remove the push changes and by the way this github token secrets.github token is automatically created for us so you don't really have to worry about this but you need this if you push changes and i will show you how to use this in a moment so the some secret environment variable is one thing that we have to create and now for the main dot pi let's also use the exact same code from here so let's copy and paste this in here and now this needs the requests module so for this we can say pip install requests and then we can say pip freeze grader sign requirements txt this will create this file but actually we can get rid of the rest so we only need requests here and now let's save all those files and now for example we can test this so we can say python python main dot pi and now you see it also created our first log file and it says token value token is not available but the weather is this so it doesn't need the api token for this example in order to work i will just i just want to show you how you can access this so to access this we say os and byron and then the name so in this case i didn't set the environment variable locally that's why it's not available but we will define this in github in in one moment but we have to remember that we have to give it this name some secret and then we can access it like so so now let's add all the changes by saying get at dot and then we can say git commit and then dash m and then give it a message so we say set up action and enter and then we say git push and now it pushed all the files to our actions test so now if we refresh this then you see we now have all these files in here so the main.pile and the workflows with the actions and now if we head over to the actions we should see run main.pi so this will run at every two hours and now the last thing to do is to specify the same environment variable so for this we click on settings and then here you find secrets actions and then you click on new secret and now here we have to give it the same name some secret and then here we put in our api token so the value and click on add secret and now the next time um sorry the next time this should run then in main.pi it should be able to access this and then lock the value here and yeah this is basically everything you need so i hope you really enjoyed this and again if you have cool project ideas for what we can use github actions then let me know in the comments and then i hope to see you in the next video bye
Info
Channel: Patrick Loeber
Views: 51,203
Rating: undefined out of 5
Keywords: Python
Id: PaGp7Vi5gfM
Channel Id: undefined
Length: 12min 32sec (752 seconds)
Published: Wed Jul 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.