[RAILS] What is Jenkins? ] Installation-how to use-automatic test / automatic deployment will be explained in the world's easiest way

What made you decide to write this article

As a freelance engineer, I am participating in a project using Jenkins, but after studying, let's spread the knowledge by writing a Qiita article! So I decided to write it. Also, this article is about to be written, but will be updated from time to time. https://www.udemy.com/course/jenkins-from-zero-to-hero/learn/lecture/19367702 While learning from udemy's video, I am writing while researching, so I would be very happy if you could point out any mistakes.

Target readers

People who have decided to use Jenkins at work but have no idea how to use it What is Jenkins? People mac user

What is Jenkins?

CI tool

What is a CI tool?

CI stands for Continuous Integration, which literally means continuous integration. It is also called continuous integration. If you have taken the Basic Information Technology Engineer Examination, you may have seen only the ward.

CircleCI, which has become popular recently, is also one of the CI tools. By the way, it is said that everything you can do with Jenkins can be done with CircleCI.

However, there are still many companies using Jenkins, so I don't think that gaining Jenkins knowledge will be a sunk cost at this time.

The aim of CI tools is to maintain development speed and software quality by automating the parts that can be automated of commit → build → test.

By the way, CD, which often appears as a related word, is an abbreviation for continuous deployment, which is an upward compatible concept of CI. Not only commit-> build-> test, but also subsequent release-> deployment is automated for efficient development.

Benefits of Jenkins

Jenkins allows you to automate builds and tests. And when a build or test fails, you can check it immediately with Jenkins, which can lead to early detection of defects and help improve the quality of software.

Disadvantages of Jenkins

It takes a lot of man-hours to learn Jenkins and to build a Jenkins environment. It seems that many companies are switching to CircleCI now, so what you have learned may be wasted.

How to install Jenkins

Install using Homebrew. Homebrew is software for installing software and managing installed software. If you are using mac, you probably have it installed, but if you haven't installed it yet, type the following command into terminal to install it.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Next, enter the command to install jenkins using Homebrew on the terminal. (It will take some time)

brew install jenkins

After the installation is complete, enter the command to start jenkins on the terminal. (I registered in clipy because it is a command I use often)

launchctl start homebrew.mxcl.jenkins

Now that jenkins has started, open a browser and enter "localhost: 8080". Since jenkins is an http server, we are accessing it on port number 8080.

スクリーンショット 2020-08-31 0.50.58.jpg

After a while, the following screen will appear.

スクリーンショット 2020-08-31 0.51.29.jpg

I put the password in the path shown in red, so I was asked to enter it. If you do the following on the terminal, the password will be displayed.

cat Path displayed in red letters

After entering the password, the following screen will be displayed.

スクリーンショット 2020-08-31 0.54.14.jpg

You will be asked if you want to install the recommended plugin or select and install the plugin. If you select the left, the following screen will be displayed and the recommended plug-in installation will start.

スクリーンショット 2020-08-31 0.55.28.jpg

When the installation is complete, the screen below will be displayed. Enter all and press save and continue at the bottom right. Don't forget what you enter as it will be used to log in to jenkins. スクリーンショット 2020-08-31 0.59.31.jpg

When the input is completed, the following screen will be displayed. You will be asked "Do you want to change the URL?", So change it as you like. I use it for studying, so I haven't changed it. Click save and Finish. スクリーンショット 2020-08-31 1.01.53.jpg

The following screen will appear, so click start using jenkins. スクリーンショット 2020-08-31 1.03.05.jpg

When the screen below is displayed, the installation of jenkins is complete. スクリーンショット 2020-08-31 1.03.37.jpg

How to exit Jenkins

I think that Jenkins was started by the following command during the installation.

launchctl start homebrew.mxcl.jenkins

After starting, if you do not enter the end command in terminal, it will continue to start, so enter the following command in terminal to end. (I registered in clipy because it is a command I use often)

launchctl stop homebrew.mxcl.jenkins

I'll finish it when I'm done using it.

How to uninstall Jenkins completely

Since I installed jenkins with brew, I also uninstall it with brew. Enter the following command on the terminal to uninstall.

brew uninstall jenkins

However, that alone will leave various data such as the user name and password set when starting Jenkins, so when you install jenkins again, you will be logged in using that information. If you want to delete it, you can delete it completely by entering the following command on the terminal. There is a user name directory under the Users directory, and there is a .jenkins directory under that directory, so delete it with the rm -rf command. (The pass is different for each person, so please read it.)

cd /Users/username/.jenkins
cd ../
rm -rf .jenkins

2020/09/02

Let's try Jenkins

Jenkins has the concept of jobs. A job here is a group of automated procedures. I think that understanding will improve if you try to make one, so let's create a job at once.

Click Create New Job on the screen below. スクリーンショット 2020-09-02 22.24.32.jpg

Moved to the job creation screen.

スクリーンショット 2020-09-02 22.26.22.jpg

Enter an item name represents the job name. Enter any name you like.

スクリーンショット 2020-09-02 22.34.05.jpg

Enter a name of your choice and choose to build your freestyle project. It's a little old tool, so I have a little quirk in UI operation.

[Tips]
Freestyle project builds allow you to create the most basic jobs.
Any SCM(source code manager:github or subversion)Source code from
You can grab it and build your project on any build system.
Besides, it can be used for various arbitrary automations.
I'm still not sure what the other items are, so I'll explain them later.
This time it's for testing, so in this freestyle project
"I'm the very first job in Jenkins"
Execute a shell script that displays.

Click OK at the bottom left to display the screen below.

スクリーンショット 2020-09-02 22.34.26.jpg

There are various settings, but what I want to do this time is to display a character string using a shell script. So I don't use most of the settings.

If you scroll down, there is an item called Build, so select Run Shell from Add Build Procedure.

スクリーンショット 2020-09-02 22.36.48.jpg

Then, a form for entering shell commands will appear. Enter the following.

echo "I'm the very first job in Jenkins"

スクリーンショット 2020-09-02 22.40.03.jpg

When you're done, click Save.

スクリーンショット 2020-09-02 22.40.56.jpg

You have now created your first job. Then click Run Build in the menu on the left. This operation is called "building a job".

スクリーンショット 2020-09-02 22.41.56.jpg

When I built the job, ● </ font> # 1 </ u> was created in the build history. This represents the build result. It means ● </ font>, but if it is ● (blue) </ font>, the build is successful and ● ( Red) </ font> indicates a build failure. If you cancel the build in the middle, it will be ● (gray) </ font>. Since \ # 1 is a link, click it.

スクリーンショット 2020-09-02 22.50.36.jpg

Then, you can check the build time, and you can check the build result in various ways from the menu on the left. Detailed build-time information can be found in "Console Output", so click to open it.

スクリーンショット 2020-09-02 22.57.01.jpg

This shows the log when the job was built. The second line from the bottom says "I'm the very first job in Jenkins". In other words, you can see that the shell script you just entered in the form was successfully executed.

When you actually do automatic build and automatic test, you need a little more complicated settings, but this is the general flow when creating a job.

You can return to the top page by clicking the Jenkins banner on the upper left.

スクリーンショット 2020-09-05 12.24.26.jpg

You can see that the job has been created and the display has changed. The job name of the job created earlier is displayed. You can see S and W on the left.

For S, a colored ● is displayed. Represents the final build result. ● </ font> succeeded ● </ font> fails ● </ font> has not been executed or canceled

W can find out how many of the latest were successful. : sunny: are all successful : partly_sunny: is a little successful : umbrella2: all failed

Let's edit the created job

You can edit the created job. I displayed the string in a shell script, but now let's use the shell command date to display the date and the whoami command to know my username.

Select a job from the top page and click Settings on the left menu bar.

スクリーンショット 2020-09-05 12.32.54.jpg

Rewrite the shell script as follows.

Before correction

echo "I'm the very first job in Jenkins"

Revised

echo "$(whoami)Mr. Hello! Today's date and time$(date)is"

スクリーンショット 2020-09-05 12.40.17.jpg

After rewriting, click Save to run the build and try to open the console output from the latest build result.

スクリーンショット 2020-09-05 12.41.14.jpg

You can confirm that you were able to edit the job.

When I run a job in Jenkins, in what directory does it run?

If you installed Jenkins on your mac, the job will run in the following directory:

/Users/username/.jenkins/workspace/Job that outputs characters in the shell

You can see this directory by running the pwd command, which shows the current directory in the job's shell script.

It will be different if you build Jenkins on Amazon Linux on AWS EC2 or Jenkins on Linux on VirtualBox. If you don't know, you can check by creating and building a shell script that runs pwd in your job.

Also, on mac, the area under the .jenkins directory is a hidden folder, so you need to set it to display the hidden folder when displaying it with the finder.

Where are Jenkins job data stored?

If you installed Jenkins on your mac, the job data will be stored in the following directory.

/Users/username/.jenkins/jobs/Job that outputs characters in the shell

You can also get logs directly from here. If you play around with it, Jenkins may not be able to read the job data and you may get a bug, so you should not play with it until you get used to it. (Experience story)

Let's fail to build a job in Jenkins

Up to this point, there was only a success pattern, so I will try to fail on purpose. Select a job and go to the screen where you can enter a shell script from the settings.

Delete the "" at the end.

Before correction

echo "$(whoami)Mr. Hello! Today's date and time$(date)is"

Revised

echo "$(whoami)Mr. Hello! Today's date and time$(date)is

スクリーンショット 2020-09-05 13.08.38.jpg

Click Save, then click Run Build.

スクリーンショット 2020-09-05 13.27.21.jpg

You can see that the build history has turned red. That is, the build failed. In this case, I know why the build failed, but let's check why the build failed. Follow the red link from the build history and click on console output.

スクリーンショット 2020-09-05 13.32.24.jpg

You can read the following information from the log

Which line has the error

/var/folders/sp/s32xwxcx69343zl_szqw02kw0000gn/T/jenkins13410237169821088770.sh: line 2: unexpected EOF while looking for matching `"'

Why the error occurred

Build step 'Run shell' marked build as failure

The result is failing

Finished: FAILURE

If the build fails, first check the console output to help determine the cause.

Since there is no "" at the end in the shell script, restore it and execute the build again.

Before correction

echo "$(whoami)Mr. Hello! Today's date and time$(date)is

Revised

echo "$(whoami)Mr. Hello! Today's date and time$(date)is"

スクリーンショット 2020-09-05 13.36.51.jpg

スクリーンショット 2020-09-05 13.37.31.jpg

Now the one that failed to build will succeed again.

Let's create a job that calls a shell script created from Jenkins

So far, I wrote the shell script directly on Jenkins, but now let's call the created shell script.

"But where do you put the shell script?"

Jenkins has the concept of workspace. You can read what you put in that workspace with Jenkins. Rest assured, I'll explain everything in the steps that follow.

"Why do I need to call a shell script?"

By remembering this procedure, you will be able to utilize your workspace and make applications more effective.

Immediately create a new job.

スクリーンショット 2020-09-05 14.33.50.jpg

Name the job "Job to call shell script", build a freestyle project, and click OK.

スクリーンショット 2020-09-05 14.35.56.jpg

Click Add Build Procedure → Run Shell, then click Save.

At this point, the shell script form is empty and is fine.

スクリーンショット 2020-09-05 14.36.49.jpg

Click on the workspace. Clicking on either the one displayed in the menu on the left or the one displayed on the right will take you to the same page.

If in doubt, use Kurapika theory to make your choice.

Leorio "Why is it on the left in such a case?
In this case, I'm not on the left, so I'm not calm. "

Kurapika "It is true that from the viewpoint of ethology, when people get lost or choose an unknown path
It seems that there are many cases where you unconsciously select the left. "

クラピカ理論.jpg

スクリーンショット 2020-09-05 14.40.10.jpg

When you open the workspace, you can see that the workspace does not exist. Although it says an error, this is normal.

By putting the shell script in the workspace, it can be executed from Jenkins, but the specification is that the workspace is not created unless it is built once.

So build once and then reopen the workspace.

スクリーンショット 2020-09-05 14.44.32.jpg

If you see a screen like the one above, your workspace has been created.

So where is this workspace?

/Users/username/.jenkins/workspace/Job that calls a shell script

It is in.

I tried various things, but I couldn't put the script file from Jenkins, so I went to create a shell script in the above path from Finder. (I don't know if Jenkins can do it, so I'll explain it this way)

Create a script.sh file in the above workspace and describe the contents as follows.

script.sh

#Outputs the type of shell you are using
echo "(The shell you are using is "$SHELL ")"

#Show name
LASTNAME=$1 #Pass the name as the first argument
FIRSTNAME=$2 #Pass the surname as the second argument
NAME=$FIRSTNAME$LASTNAME #Attach the surname and first name
echo "Hello,$NAME"

This shell script will output the name based on the shell you are using and the arguments.

When you're done, go back to working with Jenkins.

スクリーンショット 2020-09-05 15.04.12.jpg

You can also check the existence of script.sh on Jenkins. If it is not displayed, you can check it by refreshing the page. If it still doesn't appear, there is an error in the directory where you create the shell script.

Click Job Settings.

スクリーンショット 2020-09-05 15.07.40.jpg

Enter the following:

sh script.sh Ryuken Yamamoto

This means that in a shell called sh, the first argument "Ryuken" and the second argument "Yamamoto" are passed to script.sh and executed.

Once entered, click Save and then Run Build.

スクリーンショット 2020-09-05 20.24.02.jpg

If the build history is ● </ font>, it is successful. Click the latest build history, then click Console Output.

スクリーンショット 2020-09-05 20.25.04.jpg

(The shell you are using is "/bin/zsh ")
Hello, Yamamoto Ryûken Mr.

Is displayed, it is successful.

You have now created a job that runs a script that you have placed in your workspace.

You can end this chapter here, but to get used to the shell script, let's modify the Jenkins shell script to create variables and then pass them to script.sh.

Open the settings from the job and do the following:

Before correction

sh script.sh Ryuken Yamamoto

Revised

FIRSTNAME=Yamamoto
LASTNAME=Ryuken
sh script.sh $LASTNAME $FIRSTNAME

※Caution

FIRSTNAME=Yamamoto
LASTNAME=Ryuken
sh script.sh $LASTNAME $FIRSTNAME

,

FIRSTNAME=Yamamoto
LASTNAME=Ryuken
sh script.sh $LASTNAME $FIRSTNAME

If you put a space after and =, the job build will fail.

スクリーンショット 2020-09-05 21.27.52.jpg

Click Save to run the build and open the latest build history to see the console output.

スクリーンショット 2020-09-05 21.29.30.jpg

The result is similar.

Take advantage of Jenkins build parameterization

I've ignored it a lot, but for the first time I'd like to use one of the options, "build parameterization".

スクリーンショット 2020-09-05 21.46.17.jpg

Now, in the shell script, the FIRSTNAME and LASTNAME variables are prepared, and the values are set and used.

スクリーンショット 2020-09-05 21.47.34.jpg

Parameterize these to build. Check Build Parameterization.

スクリーンショット 2020-09-05 21.50.18.jpg

Then select a string from Add Parameter.

スクリーンショット 2020-09-05 21.50.59.jpg

Then, a field for entering the name, default value, and description appeared.

スクリーンショット 2020-09-05 21.51.22.jpg

Enter FIRSTNAME for the name, Yamamoto for the default value, and surname for the description.

スクリーンショット 2020-09-05 21.52.53.jpg

You have now parameterized one variable to your build. Next, create another one.

Select the string from Add Parameter again.

スクリーンショット 2020-09-05 21.54.14.jpg

Enter LASTNAME for the name, Ryuken for the default value, and name for the description.

スクリーンショット 2020-09-05 21.55.06.jpg

You have now parameterized the two variables in your build.

Finally, remove the unnecessary description from the Jenkins shell script and rewrite it as follows.

Before correction

FIRSTNAME=Yamamoto
LASTNAME=Ryuken
sh script.sh $LASTNAME $FIRSTNAME

Revised

sh script.sh $LASTNAME $FIRSTNAME

スクリーンショット 2020-09-05 21.58.32.jpg

Now the shell script is refreshing. This alone feels like a benefit, but it's not the only benefit. Here are the benefits of real build parameterization. Click Save.

スクリーンショット 2020-09-05 21.59.13.jpg

At this point, you will notice that one display has changed. The part that was previously a build execution has changed to a build with parameters.

Let's click it right away.

スクリーンショット 2020-09-05 21.59.59.jpg

The build execution started immediately when I clicked on the build execution, but by setting the build parameterization, it is now possible to change the value of the variable before the build execution.

Click Build as is once, and when finished, open the console output from the latest build history.

スクリーンショット 2020-09-05 22.03.51.jpg

The displayed string is the same as in the previous chapter. Then go back to the project → Build with parameters. This time, I will make my last name and also my first name.

スクリーンショット 2020-09-05 22.05.16.jpg

Click Build and when you're done open the console output from the latest build history.

スクリーンショット 2020-09-05 22.06.35.jpg

You can see that the displayed name has changed. By setting build parameterization, you can now change variables each time you run a build. You can also set default values, which is a useful feature if you need to change variables each time you run a build.

The usage of build parameters is endless. In this chapter, it's okay if you can tell that build parameterization can be used in various ways.

Make Jenkins build parameterization selective

Build parameterization can be selectable as well as string input. All the parameterization of the build is done once.

スクリーンショット 2020-09-05 22.18.42.jpg

Then choose to select additional parameters.

スクリーンショット 2020-09-05 22.19.12.jpg

Make settings to make the surname and first name selectable according to the image below. The surname can be selected from "Yamamoto", "Katsumata", and "Bannai". You will be able to create a name in the same way and select from "Ryuken", "Kenta", and "Manabu".

スクリーンショット 2020-09-05 22.20.39.jpg

Click Save, then click Build with Parameters.

スクリーンショット 2020-09-05 22.23.23.jpg

I was able to confirm that the surname and first name are selectable. Change the surname to "Bannai" and the name to "Manabu".

スクリーンショット 2020-09-05 22.24.18.jpg

cool. (Imitation of a foreign teacher)

Then click Build and then click Console Output from Latest Build History.

スクリーンショット 2020-09-05 22.25.23.jpg

It was confirmed that the selected names "Bannai" and "Manabu" are displayed. As an aside, Manab is the same age as me and is four months apart.

You have now made the build parameterization selective.

Show / hide using Jenkins build parameterization

I used to show which shell I was using, but since I don't need to show it every time, I will use build parameterization to show / hide it.

From the job settings, select a boolean value in Add Parameter. スクリーンショット 2020-09-05 22.36.48.jpg

The name is SHOWSHELL, the default is unchecked because I want to hide it, and the description can be checked to show the shell you are using. will do.

スクリーンショット 2020-09-05 22.38.26.jpg

Click Save.

Next, rewrite the script.sh placed in the workspace so that the shell is displayed if SHOWSHELL is true (1) and the shell is not displayed if SHOWSHELL is false (0).

Before correction

/Users/username/.jenkins/workspace/Job that outputs characters in the shell/script.sh


#Outputs the type of shell you are using
echo "(The shell you are using is "$SHELL ")"

#Show name
LASTNAME=$1 #Pass the name as the first argument
FIRSTNAME=$2 #Pass the surname as the second argument
NAME=$FIRSTNAME$LASTNAME #Attach the surname and first name
echo "Hello,$NAME"

Revised

/Users/username/.jenkins/workspace/Job that outputs characters in the shell/script.sh


#Variable definition
LASTNAME=$1 #Pass the name as the first argument
FIRSTNAME=$2 #Pass the surname as the second argument
SHOWSHELL=$3 #Pass a flag to the third argument whether to print the shell

#Outputs the type of shell you are using
if [ "$SHOWSHELL" = "true" ]; then
	echo "(The shell you are using is "$SHELL ")"
fi

#Show name
NAME=$FIRSTNAME$LASTNAME #Attach the surname and first name
echo "Hello,$NAME"

Go back to working with Jenkins and click Build with Parameters.

スクリーンショット 2020-09-05 22.44.30.jpg

This time, the shell I'm using is no longer displayed, so just click Build to display the console output from the latest build history.

スクリーンショット 2020-09-05 22.48.09.jpg

It was confirmed that the shell type that was displayed until now is no longer displayed.

You can now show or hide it using build parameterization.

Automatically test your rails application with Jenkins

Bring your rails application from GitHub and create a job for testing and static analysis in Jenkins.

How to create rails application, how to install Homebrew which is software to install rbenv and its rbenv which can easily rewrite ruby version, rspec used in automatic test, rubycop used as static analysis tool I will omit the introduction method. There are personal apps with rspec and rubocop installed on GitHub below, so I will explain using this as an example. https://github.com/EL93019205/stuctive.git rails uses 5.2.3 and ruby uses 2.5.1.

There is an article that introduces the procedure for installing the plugin, but no plugin is required except the default one. ~~ The plugin is generally old and not very useful ~~

Now let's create a new job.

スクリーンショット 2020-09-22 12.57.22.jpg

Job name is rails It must be in English for the convenience of. This time, set it to "stuctive_auto_test".

The job type should be Freestyle Project Build.

スクリーンショット 2020-09-22 14.32.57.jpg

Click OK when you are done. スクリーンショット 2020-09-22 13.00.03.jpg

Since there is an item called source code control, I will use Git. Then, the following screen will be displayed.

スクリーンショット 2020-09-22 13.03.48.jpg

GitHub in the repository URL Now, what should I enter in the repository URL? The answer is on GitHub.

Click the green button Code from the URL below (the URL to the repository of my personal app), and click the icon to the right of the part where the URL is written. This icon represents a copy. Since it is a public repository, I think anyone can copy and use it. https://github.com/EL93019205/stuctive スクリーンショット 2020-09-22 13.04.55.jpg

Then paste "https://github.com/EL93019205/stuctive.git" into the repository URL as shown below. スクリーンショット 2020-09-22 13.08.35.jpg

This is the only source code control setting. You don't have to play with anything else.

Did you think, "What? That's it?" Yes, that's it.

If you have secure settings, you may need to set credentials, and if the branch to be automated tested is other than master, you may need to change the branch to build, but my personal app is in the public repository. And since the branch you want to do automated testing is master, you don't have to mess with it.

This setting clones the rails application to your workspace.

Click Save Once. スクリーンショット 2020-09-22 13.16.03.jpg

Open the workspace. スクリーンショット 2020-09-22 13.16.48.jpg

You can see that the workspace does not exist.

スクリーンショット 2020-09-22 13.16.59.jpg

As I wrote above, the workspace will not be created unless the build is executed once. So click Run Build.

スクリーンショット 2020-09-22 13.18.27.jpg

You can see that the build history has turned blue.

スクリーンショット 2020-09-22 13.18.55.jpg

Open the workspace again.

スクリーンショット 2020-09-22 13.20.12.jpg

It is as follows. If you've ever written a rails application, you're probably familiar with all the files.

スクリーンショット 2020-09-22 14.51.40.jpg

This data is exactly the same as the data on GitHub below. https://github.com/EL93019205/stuctive スクリーンショット 2020-09-22 13.21.25.jpg

In this way, it's super easy to copy a set of rails application data on GitHub to your Jenkins workspace.

All that remains is to execute the automated test command with this retrieved data in the directory containing the Gemfile. The command of the automatic test is completely different depending on the version of rails, but since we are using rails 5.2.3 this time, it can be executed with the following command.

bundle exec rspec
bundle exec rubocop

Let's do this.

Open the settings. スクリーンショット 2020-09-22 13.28.01.jpg

Scroll to the bottom and paste the command above into running the shell. This command runs in the workspace directory. In other words, it will be executed in the directory where the Gemfile is located. Click Save when you are done.

スクリーンショット 2020-09-22 13.28.38.jpg

Then run the build.

スクリーンショット 2020-09-22 13.30.24.jpg

Ah, the build history turned red. What was the cause? スクリーンショット 2020-09-22 13.30.41.jpg

Click ● </ font> # 2 to open the console output.

You can see the following error log at the bottom.

スクリーンショット 2020-09-22 13.31.59.jpg

This means you need a bundle install, right? Since it is an expected operation, I dared to make an error.

So go back to the settings and add bundle install.

スクリーンショット 2020-09-22 13.34.26.jpg

Save and build again

スクリーンショット 2020-09-22 13.35.37.jpg

I get an error again. Click ● </ font> # 3 to open the console output.

スクリーンショット 2020-09-22 13.36.07.jpg

The version of ruby is 2.6.3, but since it is 2.5.1 in Gemfile, I get angry that I can't bundle install.

スクリーンショット 2020-09-22 13.36.46.jpg

I think this is the part where such an error may or may not occur depending on the person. You may also get different errors. After all, the environment differs greatly depending on the PC.

It is a bad idea to install rbenv or try to install Homebrew here. I'm addicted to the quagmire. I was addicted to it.

As a result of various investigations, it seems that the environment variable PATH is overwritten when the Jenkins build is executed.

Since the environment variables for Jenkins are made to be usable, it seems that it is possible.

So, let's make it so that the environment variable PATH is overwritten with the PATH of the local PC after executing the build.

To check the environment variable PATH, execute the following command on the mac terminal. This command is a command to get a list of all environment variables with the printenv command, connect them with | based on the result, and display only the lines containing PATH with the grep command.

printenv | grep PATH

Then the value of the environment variable PATH will be displayed. スクリーンショット 2020-09-22 13.45.08.jpg

This varies from person to person, but Jenkins needs to return this PATH.

Therefore, rewrite as follows.

export PATH="Paste the contents of the PATH you copied earlier"
bundle exec rspec
bundle exec rubocop

In my case, it looks like this: Please read the PATH as it varies from person to person.

スクリーンショット 2020-09-22 14.56.33.jpg

Now save and click Run Build again.

スクリーンショット 2020-09-22 14.59.44.jpg

The build was successful!

Let's open the console output from ● </ font> # 4 and look at the log.

First of all, you can see that the bundle install log is displayed. スクリーンショット 2020-09-22 15.04.50.jpg

Next, you can see that the bundle exec rspec is logged.

スクリーンショット 2020-09-22 15.06.17.jpg

Finally, you can see that the bundle exec rspec is logged.

スクリーンショット 2020-09-22 15.06.34.jpg

You can now create a job that automatically tests your rails application by running a build.

Here, "A shell script for branching whether to use ● </ font> or ● </ font> depending on the execution result of rubocop or rspec. You may have wondered, "Do I have to create it?"

In conclusion, it is not necessary. The bundle exec rspec and bundle exec rubocop commands return an error code if there is an error, and Jenkins looks at the error code and either ● </ font> or ● </ font>. This is convenient!

If you don't understand shell scripts, shells, commands such as grep and printenv, and the meaning of environment variables, you lack knowledge of Linux, so you should study with the following books. ~~ I also have a refreshing Linux, so I'm thinking of buying this book and studying. ~~ https://www.amazon.co.jp/dp/B072K1NH76/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1 スクリーンショット 2020-09-22 13.13.37.jpg

That's all for this time.

You can make it run an automated test by pushing it to the topic branch of a remote repository using a webhook, but I'd like to introduce that on another day.

Auto-deploy rails applications with Jenkins

Attention! Even if you copy the procedure as it is, it cannot be automatically deployed here. Because I own the master.key for the rails application. Also, my personal app is already built so that it can be deployed to the production environment with a single command with Capistrano. The method is not described. It's difficult, but I'll show you how to build the production environment below. https://drive.google.com/drive/folders/14F4AmgnK0n022uYLatlQbvuPIGLGLFgt

I will show you only how to do it. It's super easy to do with Jenkins.

Click Create New Job.

スクリーンショット 2020-09-22 15.22.17.jpg

Set the job name to "stuctive_auto_deploy" and the job type to Freestyle Project and click OK.

スクリーンショット 2020-09-22 15.22.50.jpg

スクリーンショット 2020-09-22 15.22.53.jpg

Set the source code control to Git and the repository URL to https://github.com/EL93019205/stuctive.git (my personal app).

Select Run Shell from Add Build Procedure for Build and make the shell script look like this:

スクリーンショット 2020-09-22 15.43.10.jpg

There are three things I'm doing. -Overwrite Jenkins PATH with local PC PATH (same as automatic test) -Copy master.key from where master.key is to the workspace and get it -Execute Capistrano's automatic deployment command

Click Save and then click Run Build.

スクリーンショット 2020-09-22 15.34.31.jpg

After executing the build, the build history will be ● </ font> # 1.

スクリーンショット 2020-09-22 15.34.05.jpg

Click ● </ font> # 1 and click Console Output to see the auto-deploy log.

スクリーンショット 2020-09-22 15.35.51.jpg

You can now auto-deploy your rails application in Jenkins with a build run.

Perhaps you can use a webhook to make the automatic deployment run when the master branch of the remote repository is updated, but I'm still studying that, so I'd like to introduce it on another day.

At the end

If you find something difficult to understand, please leave it in the comments. Thank you for reading to the end.

Recommended Posts