Why Use Recipes0:01
We've already looked at a few ways. You can run commands on your server through Forge, but both one-off commands and deploy scripts are meant to run more at the site level. Recipes, on the other hand, have two primary benefits against those. First they run at the server level, not at the site level, and you can choose the user. And second, you can write them once and apply them to one server or all your servers,
HSTS Recipe Example0:17
And second, you can write them once and apply them to one server or all your servers, or every time you spin up a new server, you can share them with your team. They're meant to be used in many settings, not just one. Let me show you a recipe I keep in my Forge account as an example. So there's this concept called HSTS that basically just forces browsers to only connect on the SSL version of your website.
that basically just forces browsers to only connect on the SSL version of your website. It's a security thing. So if you want to use HSTS, you can check to make sure that your HSTS is working. And as you can see on these brand new domains that we just spun up in this course, there is no HSTS header because Forge doesn't add it by default. So what I have done is I built a shell script that when you run, it goes through all of your forged domains on a given server
that when you run, it goes through all of your forged domains on a given server and adds the HSTS headers to them. So let's go take a look and we can see this is just a simple bash script. It is basically setting up some variables. It's working through particular files that I have in my server. In this case it's folders under the sites available directory.
In this case it's folders under the sites available directory. It is checking for something within them and it is writing to them if they need. And you don't need to understand Bash in order to be able to understand what we're doing here. But I just wanna show you, this is a script that I want to be able to run as many times as I need to on any of my servers and know that it's gonna look for any sites missing the HSTS headers,
of my servers and know that it's gonna look for any sites missing the HSTS headers, and it's gonna add the headers to all those sites. So I've got this as a shell script, but it would still be a pain for me to upload it to every single server or copy that script and paste it to every server every single time I wanna run it. It'd be a lot easier if it was just saved in my Forge account ready to run whenever I want.
Creating a New Recipe1:44
It'd be a lot easier if it was just saved in my Forge account ready to run whenever I want. Recipes are perfect for this. So let's look at how to bring my shell script over into a recipe. First, let's head over to our recipes on Forge and we're gonna hit new recipe. And for each recipe, there's a few things you're defining. So right now we're gonna say HSTS, all the sites. You can run it as root or forge.
So right now we're gonna say HSTS, all the sites. You can run it as root or forge. If you're not sure, start with Forge. And if it doesn't work, then you can try root, but be more careful. You can paste your actual bash in here and then you can share it with circles, which we'll talk about a little bit later. So if my thing was just to say Echo, hello world, and that was what my recipe was,
Running Recipes on Servers2:20
So if my thing was just to say Echo, hello world, and that was what my recipe was, then every single time I run it, all it's gonna do is go echo, hello world on the server I choose. So let's see what it looks like to run that recipe. We go to our HSTS, all the sites recipe, and we hit the run button. And when you hit run, you're choosing not just which recipe you're running, but also which server it's applying to
And when you hit run, you're choosing not just which recipe you're running, but also which server it's applying to and whether or not you want to be sent to report by email. So we'll skip the email for right now and we'll just say, I want you to run it on my production server. So what it's gonna do is it's gonna SSH into that server and it's gonna run echo, hello world for us. And if we look at the recent run, we can see the output. So if I were to modify this
And if we look at the recent run, we can see the output. So if I were to modify this and actually put the right content in there, and then I paste it in there. Now anytime I want, I can just run this script. It's gonna run against my server, and it's gonna run to add HSCS header to all of my sites. And if I want, in any given moment when I'm running it, I can run it against multiple servers at the same time. And if I want, I can get that status, not just in the ui,
I can run it against multiple servers at the same time. And if I want, I can get that status, not just in the ui, but actually emailed to me. When the recipe is done running, you can also get the same popup from the recipes list by hitting the three dots and hitting run. So when we were first creating this recipe, we had the opportunity to choose who we were sharing it with. And you can see it's not there in the edit form.
Sharing Recipes with Teams3:43
who we were sharing it with. And you can see it's not there in the edit form. So that's really only something you do when you're creating a new recipe and you wanna share this recipe with other teams that you're a part of. But if you wanna share it with that team later, you can just go to the teams page. And when you're editing a team, there's a recipes option and you can choose to add any of your pre-existing recipes into this team
Community Recipe Sharing4:00
and you can choose to add any of your pre-existing recipes into this team to be shared with the rest of this team. And there's one other way that we share recipes, which is if you go to forge recipes.com, you will see that there is a community managed site here that has recipes that people have shared of their favorite forge recipes. So you can even find my HSTS recipe in there somewhere. You can see that recipe. And it's just the description and the bash script, and that's it.
You can see that recipe. And it's just the description and the bash script, and that's it. So if you come up with a great Forge recipe, make sure you share it here for everybody else.
