Scripted and declarative pipeline for text artifacts

Lab1:

Youtube Link : Navigation for lab

  1. Node Block: The node block specifies that the pipeline should be executed on a Jenkins agent (node). It defines the environment where the pipeline stages will run.
  2. Stage: Create build output:
    • sh "mkdir -p output": This shell command creates a directory named “output” if it doesn’t already exist. The -p option ensures that the command doesn’t throw an error if the directory already exists.
    • writeFile file: "output/usefulfile.txt", text: "This file is useful, need to archive it.": This step writes some content into a file named “usefulfile.txt” in the “output” directory.
    • writeFile file: "output/uselessfile.md", text: "This file is useless, no need to archive it.": Similarly, this step writes content into a file named “uselessfile.md” in the “output” directory.
  3. Stage: Archive build output:
    • archiveArtifacts artifacts: 'output/*.txt', excludes: 'output/*.md': In this stage, you archive the build output artifacts. Specifically:
      • artifacts: 'output/*.txt': This specifies that you want to archive all files with the “.txt” extension in the “output” directory.
      • excludes: 'output/*.md': This specifies that you want to exclude all files with the “.md” extension from archiving.
node {
stage('Create build output') {
// Make the output directory.
   sh "mkdir -p output"

// Write a useful file, which is needed to be archived.
   writeFile file: "output/usefulfile.txt", text: "This file is useful, need to archive it."

// Write a useless file, which is not needed to be archived.
   writeFile file: "output/uselessfile.md", text: "This file is useless, no need to archive it."
}

stage('Archive build output') {
// Archive the build output artifacts.
    archiveArtifacts artifacts: 'output/*.txt', excludes: 'output/*.md'
  }
}

Convert same code to declarative block

Lab2:

  1. Agent Block: The agent block specifies that this pipeline can be executed on any available agent in your Jenkins environment. The keyword any means Jenkins will allocate any available agent to run this pipeline.
  2. Stages:a. Stage: Create build output:
    • steps: This block defines the steps that should be executed within this stage.
    • script: The script block allows you to run arbitrary shell scripts within the pipeline. Inside this block, the following steps are performed:
      • sh "mkdir -p output": This shell command creates a directory named “output” if it doesn’t already exist. The -p option ensures that the command doesn’t throw an error if the directory already exists.
      • writeFile file: "output/usefulfile.txt", text: "This file is useful, need to archive it.": This step writes some content into a file named “usefulfile.txt” in the “output” directory.
      • writeFile file: "output/uselessfile.md", text: "This file is useless, no need to archive it.": Similarly, this step writes content into a file named “uselessfile.md” in the “output” directory.

    b. Stage: Archive build output:

    • steps: This block defines the steps that should be executed within this stage.
    • archiveArtifacts: This is a built-in Jenkins step for archiving files. In this step:
      • artifacts: 'output/usefulfile.txt': It specifies that you want to archive only the file “usefulfile.txt” located in the “output” directory.
      • excludes: 'output/*.md': It specifies that you want to exclude any files with the “.md” extension from archiving. This means that “uselessfile.md” will not be included in the archived artifacts.
      • followSymlinks: false: This option is set to false, meaning that symbolic links (symlinks) will not be followed when archiving artifacts.
pipeline {
agent any
 stages {
  stage('Create build output') {
   steps {
    script {
     // Make the output directory.
     sh "mkdir -p output"
     // Write a useful file, which is needed to be archived.
     writeFile file: "output/usefulfile.txt", text: "This file is useful, need to archive it."
    // Write a useless file, which is not needed to be archived.
     writeFile file: "output/uselessfile.md", text: "This file is useless, no need to archive it."
     }
    }
   }
   stage('Archive build output') {
    steps {
    // Archive the build output artifacts.
     archiveArtifacts artifacts: 'output/*.txt', excludes: 'output/*.md'
     }
   }
  }
}

Try : Go to pipeline add one more stage after archive build output,:

name: Interactive input

steps: Add input(Check syntax in pipeline)