In this election, we'll take a look at how you can use predefined environment variables when using
it catalepsy and maybe just doesn't make a lot of sense right now.
But let's imagine the following problem.
Now we have this very beautiful and very simple website, and sometimes we can make changes to it and
deployed, but not always.
The changes that we make are really available to the website.
So if we do not change a text or change an image or something like that, it will not be able to tell
which version exactly is deployed.
So one idea would be to include some are on the website, a small information like version to know which
committee has generated this particular version that we are seeing on a production seper.
Unfortunately, get already comes with a lot of predefined environmental variables that we can use,
and at this page, which you'll find attached as a resource to this lecture, will find quite a lot
of variables that we can use.
One of the easiest way to include a short version is to use the commit hash and there's this variable
I commit to short as page eight, and this will give us an eight characters from the commit sort of
from the commit hash, which is quite short and good.
And then we'll be able to identify which committee has generated a specific version on the website.
And using predefined environment variables provided, but it's quite an easy way to do such dynamic
things in our jobs.
So whatever your needs are, it's definitely worth having a look at the variable reference to see which
of these variables are interesting for your specific own cases.
But for what we need right now, this one will do just fine.
So let's take a look on how we can use it in our pipeline scripts.
So in order to change something about the website, we need to take a look at the bills that because
this is where the website is being built and this is where the artifact with the website is being published.
So let's first take a look at what exactly this thing looks like.
So what you can do here is simply type in echo dollar sign and then I can use the variable name.
And when the script will be executed, it will display the value of the environment variable.
But still, this will not help us really understand how we can include this in the fight.
So for that reason, we're going to go inside a project and if we go into source code pages and let's
have a look at the index page, which is actually responsible for generating the first page that we
see on the website.
Now, the idea would be the following we insert here inside a page, a marker, and we'll try to find
a way to replace that marker every time we generate the website.
So inside the repository, you will not have to commit anything, but this will automatically grate
through place every time you build a new version of the website.
So let's write here a new div.
Let's say something like version.
And we can use the mark or something like this.
And we can even make it uppercase.
This is a very simple way on how I decided to create this marker, but there's absolutely no rule on
how you can make it just to know it.
That's a marker and that will be later replaced.
So now when Gatsby will generate the website, it will generate an index e-mail file and the index HTML5
will look exactly like this will include version and then this marker and we need a way to replace it.
Fortunately, unique system come with a lot of utilities and is one of those utilities, as Ed said,
stents for 3M Ed.
And one of the things said can do for us is to replace a string inside a file.
So the syntax for replacing a string will look something like this.
And we use the Desh e flac in order to edit this file in place.
So we don't want to create a copy of the file.
We simply want to use the same file for the output end.
For example, work one in the simple example would be replaced with what tool we use s for substitute
and the G flag at the end for a global replace.
So what that means is that set, we'll look inside the file that we have specified and will replace
overall word one with work to.
Now, let's take a look at how we can replace this inside our pipeline.
So this step has to happen after Gatsby has finished building the website and in our case, the command
will look like this.
So instead of what one, I have used our marker.
And instead of what do I have used the variable that we want to use and I didn't use the global flag
because it's only one occurrence, so we do not need to globally replace it.
And finally, I have specified the location of the file, and that is in public and the name of the
file is index that HDMI.
So let's save this and see how our website looks right now.
One or two, first thing that you will notice when running the built step is that Echo will display
the strength that will be used in our case as operation.
Now, going back to our pipeline, you will see that now the entire pipeline is successful, and this
means that if we go back to the website and we do a refresh.
We'll be able to see that now the version is being displayed now, it will be displayed only for a short
second because actually what gets bidders is that it has some JavaScript and will replace the contents
of the page.
We haven't actually replaced this marker in all the files.
We could do that as well, but we just wanted to see that we have something dynamically added to our
pipeline.
There's one more step that we can do, and that is to change our test to check for the specific version.
And especially interesting is the test deployment job, because we now have deployed something and it's
really important to see that the version that we have built is the one that is available to the public.
That's the one that has been deployed.
So what we can do here is to duplicate this command.
And Carl will not be affected by the problem that we have previously solved because there's no JavaScript
being executed when downloading the first page.
So the index page.
So for that reason, this has absolutely no reason not to work.
So here, instead of high people, we are simply going to use.
The variable from Gottleib.
And that's about it, this is how you can use the predefined get variables inside your scripts.