Using Docker Containers with GitHub Actions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone Andrew here and in this video I'm going to show you how to use Docker containers with GitHub actions if you haven't worked with it before GitHub actions is a built-in CI CD platform on GitHub that lets you automate tasks by running jobs as code updates are added to your repo why use Docker with this though well I build my blog using GitHub actions but there's a few specific requirements that my static site compiler needs it's easier to use a container image that I've put together with every requirement pre-installed there's two main ways to use Docker with actions the first is that we can run a whole job inside of a container or we can use a container as a single step of a job and I'll show you an example of both for the whole job let's say that we have a PHP project so we have this class file here and I want to run my automated test Suite which I have in this test directory with PHP unit which I have acquired in my composer Json file here now I've already gone and created a sample GitHub workflow file it's a yaml file in a DOT GitHub workflows directory and right now it doesn't do anything but what I want to be able to do is spin up a Docker container that runs our test Suite as a whole job so we've already specified that on push this starts running the jobs down here we only have one job though right now it's run tests and now we need to do is provided a container to run those tests on so underneath runs on Ubuntu latest which is kind of the de facto standard for GitHub actions we can specify a container to run it on now you could just specify the container that you want right here so in our case I'm going to be running this on composer's latest container but the problem is we need to pass in some additional configuration to this because as it stands right now this won't have access to our code so instead of container just being a string it's going to be a subset of more attributes our image though is going to be composer latest so that's the docker image that we'll be using to build this container and and then we need to specify some volumes to actually push data from this local environment into the composer container now in Docker the first value is the folder in our local environment and then separated by a colon to the directory inside of the container that we want to have this attached to for us we'll just have a directory called my app and it will connect to the root app folder inside of the composer container which is where that container's working directory is located and now we move on to the steps underneath of the container which is where we tell the job what to actually do during it so we'll give a name to this step we need to take first the first one for this should be running composer install since our vendor directory isn't available inside of the GitHub repo and we just pass in a run attribute with the commands that we need to install through this container and for us that's just composer install now we need to specify a second step in this job and that's running phpunit so now we can just run from the vendor directory that we have installed in bin PHP unit and tests passing in the directory where our tests lie which is in this test directory in our app okay so that should complete everything that we need let's go ahead and save this and commit it and see if our action runs all right if we head over to our actions tab we can see that our action seems to have failed if we click on it we see that we have an error composer could not find a composer Json file in our application directory and the reason for this is because by default our GitHub repos source is not actually available to the workflow unless we check it out in it so we need to go back and add some additional steps into our workflow so before we run this composer install command we need to have our app's Source available in this directory that we specified so first We'll add a new Step at the top here and we'll just call it check out the repo and it's going to use a GitHub actions action actions checkout at version 2 and what that's going to do is check out the contents of the repo in the current directory now we don't actually know what that directory is it's kind of automatically created based on your repo's name instead of remembering that and adding it into this volume what we can do instead is change this directory and use one of the variables provided by GitHub actions so using the dollar sign and two curly brackets we can Echo out github.workspace and that will take the current directory that we are right now in and attach it to that app directory in the composer container let's go ahead and commit these changes and see what happens now all right so now let's check out our actions tab and it looks like our workflow is now running successfully so we have a check mark on here and we can see that it initialize the containers it then checked out our repo ran composer installed successfully but most importantly it Ram PHP unit and all three tests came through successfully which is what we want now this whole job has passed and we only need to use one Docker container instead of specifying both the composer install and the PHP unit run okay now what about the individual jobs going back to my previous example I said that I used GitHub actions with Docker on my site compilation and it's true I'm using Docker to compile my site assets but I'm only using it in one specific step of my job I just want a container that handles taking raw source files and spits out compiled assets then GitHub itself will handle the deployment and everything else now I went ahead and created another example repo for this and it's a PHP project that has both composer dependencies and npm requirements we go into this Resources directory here we have some JavaScript and CSS assets that need to be compiled and we have some content here that needs to be reduced down to an HTML file using PHP in the form of a static site generator now opening up the source code for this we already have a compile assets workflow that I've created but right now all it's doing is checking out the repo so we are getting these stores from GitHub and we are putting it into the current directory inside of this project what I need to do now though is create a new step to run a Docker container and compile the assets and for the uses instead of specifying an action here we're going to specify Docker as a protocol and then specify the container image we're going to use in our case that's my a schmelion Cleaver latest image which is what this static side generator is built off of this image contains PHP and node so it allows us to use composer and npm which allows me to compile these assets that we need using a single image and now we specify with which takes in a series of arguments which are added into our Docker run line I'm going to make this multi-line so it's easier to read and in our case first we need to set the volume mount like before to the GitHub workspace directory and we're setting that to VAR www which is the working directory for this container up here and then we're running bin shc in order to pass a list of commands to this container we want those to be composer install nbm install and npm run production to install the dependencies and then finally compile the assets into a directory now normally I would use that directory and then push it up to a production server but in this example I just want to verify that our compiled assets are there so I'll create a new Step called verify assets and all it's going to do is run LSL on a disk directory which should only be there if the compiled assets ran through successfully so all right let's save this commit it to the repo and see it in action okay if we go and take a look at our actions well it looks like it failed and we can see that the verify assets it their directory doesn't exist which means that our assets didn't compile scrolling through here I can identify the problem even though we're passing it in the arguments our custom commands aren't running so we can use this method to run a Docker container but not if we need additional arguments set at the end of it like these here we can't get around this though using a different action so instead of using the docker protocol here we can use addnab Docker run action at V3 and that with attribute it takes an image so our Cleaver container from earlier and an options string which acts as our arguments from earlier so volume mount to the GitHub workspace directory to VAR www and then run which specifies the commands that we want to run on this container composer install npm install and npm run production whoops forgot a colon here so now if we go ahead and Commit This one let's see if this runs now all right let's go ahead and look at our actions now and it looks like this ran through successfully if we take a look at our compile assets job in the verify assets we can see our disk directory does exist and it contains our compiled assets from our workflow so the docker container ran successfully in this one step using the custom commands that we provided it all right I think that about wraps it up you've learned how to use a Docker container to run an entire job in GitHub actions or use container images to just run specific steps of a much larger job as always if you have any questions about this or any other web development topics please feel free to reach out to me on Twitter or in the comments below thanks for watching
Info
Channel: Andrew Schmelyun
Views: 32,245
Rating: undefined out of 5
Keywords: docker, docker github, github, github actions, docker github actions, docker tutorial, docker tutorials, github actions tutorial, github actions php, github actions composer, docker composer, docker php, webdev tutorial, web development tutorial, web development
Id: U7TY_qUD8yA
Channel Id: undefined
Length: 9min 36sec (576 seconds)
Published: Fri Sep 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.