DEV Community

Cover image for Building Jenkins Pipeline Free style
Back2Basics
Back2Basics

Posted on

Building Jenkins Pipeline Free style

In this blog I will be discussing on working on simple Free-Style project

  • Seletct Free style project
  • Project Name: Generate ASCII Artwork
  • Select General Configuration.
  • Description: Generate ASCII Artwork using Cowsay library and AdviceSlip Rest API
  • Build Environment -- Add timestamps to the Console Output
  • Build Steps -- Execute Shell
# See the list of available environment variables
# Build a message by invoking ADVICESLIP API

curl -s https://api.adviceslip.com/advice > advice.json
cat advice.json

# Test to make sure the advice message has more than 5 words.
cat advice.json | jq -r .slip.advice > advice.message
[ $(wc -w < advice.message) -gt 5 ] && echo "Advice has more than 5 words" || (echo "Advice $(cat advice.message) has 5 words")

# Deploy
sudo apt-get install cowsay -y
echo $PATH
export PATH="$PATH:/usr/games:/usr/local/games
echo $PATH
cat advice.message | cowsay -f $(ls /usr/share/cowsay/cows | shuf -n 1)
Enter fullscreen mode Exit fullscreen mode
  • Save and Apply: A work space will be allocated for this pipeline.
  • Build the pipeline
  • Check the console output of log info

Image description

  • The pipeline fails because the jenkins is not able to access the system environment variables. Jenkins runs as an isolated environment with jenkins user.

Image description

  • This can be resolved by envs place after the installation of cowsay.
echo $PATH
export PATH="$PATH:/usr/games:/usr/local/games
echo $PATH
Enter fullscreen mode Exit fullscreen mode

If the multiple jenkins jobs need these envs as dependencies pass them in the environment key-value. For this freestyle project.

Image description

In the workspace the file downloaded was stored.

Image description

Chained Freestyle Project

The previous shell script has three stages if take them as

  1. Build
  2. Test
  3. Deploy So, lets seperate these into each freestyle job. ### Build job
# Build a message by invoking ADVICESLIP API

curl -s https://api.adviceslip.com/advice > advice.json
cat advice.json
Enter fullscreen mode Exit fullscreen mode

Test Job

It will fail if manually triggered.
Because the build job has seperate workspace and test job has different.

# Test to make sure the advice message has more than 5 words.
cat advice.json | jq -r .slip.advice > advice.message
[ $(wc -w < advice.message) -gt 5 ] && echo "Advice has more than 5 words" || (echo "Advice $(cat advice.message) has 5 words")

Enter fullscreen mode Exit fullscreen mode

Now we chain the build job and test jobs.
Trigger only the build jobs success then test job. Under the build job configuration. Add the test job name in build job post actions save and apply.

Image description
lets trigger the build job then look at the test job. Yes now they are linked even though the test job failed. Now Look at Plugins. Some plugins info are below.

Image description

Where plugin store and their format, priority.

Image description

Installing plugins

To install plugins go to ManageJenkins > Plugin

  • Updates
  • Available plugins
  • installed plugins

Lets install Copy Artifact for this project.For documentation Copy Artifact

in the build job enable the artifacts copyt to test-job and enable the archive advice.json file. Save and apply.

Image description

Image description

In the test job add the build step as first build step for getting the artifacts. To move the build steps use drag-and-drop.

Image description

Not the build and test jobs are working fine.

Image description

Deploy job

Create deploy job and configure the build step with below and add the prior step as copy artifacts from test-job only if the testjob is stable. Also give permissions to copy artifacts from the test job.

# Deploy
sudo apt-get install cowsay -y
cat advice.message | cowsay -f $(ls /usr/share/cowsay/cows | shuf -n 1)
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

If you faced any issues with the deploy-job then try to replicate same as test job configuration.

Image description

Finally the three jobs are chained. Now to visualize the chanied jobs
install the Yet another build visualizer then goto any of the chained project.

Image description

Disadvantages of Freestyle pipeline:

  • start the jobs and run systemctl stop jenkins. It will stop the jobs running. Try 'systemctl start jenkins` check the jobs restarted where they failed. Freestyle project pipelines would not auto-restart.

Lets try to add figerprint to the advice.json and advice.message file. Jenkins will create a MD5 checksum to track the jobs used the file, it only store the checksum not the file itself. Configure it as a post action save and apply.

Image description

Now in the build-job page verify the finger print added.
Image description

Jenkins Pipeline projects.

  • Declarative
  • Scripted - most control (require groovy knowledge)

Image description

Image description

Follow me for more content on DevOps.

DevOps #Jenkins #AWS #PlatformEngineering

Top comments (0)