Deploying to a server with GitHub Actions - Up and Running - Elixir Phoenix

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
previously I set up a working Phoenix application on digital ocean and now it would be great to automate deployments if you're using github github actions is a great way to go because you can have the deployment step happen automatically right after you merge your pull request I was confused with how the actions worked since there are quite a few packages and github marketplace but I could not find a relevant one for deployment I finally realized what I actually want to do is to SSH to the server and run a bunch of commands I ran manually and there are a few packages that do just that you can trigger an action via a workflow yamo file you define as part of your codebase to use a marketplace package you just declare it in your file and there isn't anything extra that needs to be installed the first thing to do is to set up a github secret SSH key that will be used in the workflow and then at a public key to the authorized keys on your server on your local computer run this specific ssh-keygen command then - C and call it something relevant to your application now copy the contents of the private SSH key private is the dangerous one so copy that and go to your repository and github and find settings then click on secrets on the Left sidebar and click add a new secret the name we use here will be used like an environment variable save the secret and you can see that only the name shows up next grab the contents of the public key and SSH into your server we're going to add the key to the authorized hosts file open the file in vim and paste the contents at the top right and quit and just exit out of the server with that done let's create a workflow file create a github directory and workflows directory inside that then create a yellow file inside of the workflows directory it doesn't matter what you call it as long as it has a yellow extension inside of the yellow file add the name on is the trigger point in this case it will be on push set the branches to master meaning that actions will only be triggered after you push to master next specify jobs that will run you can have more than one next name of the job job one in this case the display name of the job will appear in github as it runs and then the steps each step will also have a display name note the syntax of it though then specify users this is the SSH marketplace package so just declare it here and then specify with variables it will run with first specify the host then the SSH key remember this is the name of the github secret that was created earlier and finally specify the port which I think defaults to 22 anyway now the next argument script is what will actually run on the server note the vertical pipe the next lines will just be the commands that will run as soon as you login to the server as if you are doing it yourself first change the directory to your repository then pull the latest changes from master and because this is just part of testing run git status the output of this command will be recorded so this will be a good confirmation that everything worked save this file and back on the terminal commit the changes and push to github because I committed straight to master without creating a branch the workflow action should have been triggered because remember it gets triggered on push to master so you can check and confirm that that is currently running you can click onto it for more details notice the names of the workflows and the job here you can get even more details and see the output of every command you can see that the last output is nothing to commit which is the expected output from git status so that's a success now I'm going to add all the Electra Phoenix specific deploy commands I will fetch brought only dependencies compile the application and run the database migrations then compile the assets and create a static digest finally create a mix release notice the override argument this is to prevent a prompt for the override confirmation that will happen unless you actually increase the application version then I'm going to run the command to stop the app and start it as a daemon I could probably just restart but I figured it is safer in case something goes wrong in deployment and the restart command would not work if the app isn't currently running all right so back on the console commit the changes and push them up to github again remember that this was done on master so the action should have been triggered so back and github you can see that the new action is running and the output looks like the output I would expect doing this right on the server so now that this is an action deploy task and it's actually running commands on the server it's time to test it with a real change I'm going to do it properly this time in branch off master then I'm going to make a small text change to the home page there should be enough to confirm that it works now I'm going to commit it and push the branch to github since this is a separate branch and not master I'm going to create a pull request after I merge it I would expect the updates to show up here so after merging it you can confirm that the new action has been triggered and when it's all done I'm going to go back to the webpage reloaded and see the updates I hope this was helpful I know I was confused about how github actions actually worked but now even with this pretty basic example what's going to save me and I hope you quite a bit of time
Info
Channel: omgneering
Views: 11,498
Rating: 4.9642859 out of 5
Keywords: github actions, deployment, elixir, phoenix, digital ocean, deploy
Id: gW1TDirJ5E4
Channel Id: undefined
Length: 7min 5sec (425 seconds)
Published: Sat Apr 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.