Sunday, 21 November 2021

Run Jenkins pipeline for local git repository

Step one: global tool config to add git excutable

  • Go to Dashboard
  • Click Manage Jenkins
  • Click Global Tool Configuration
  • In Git Section add git executable path. For me: /usr/bin/git
  • Click apply

Step two: Create a new item

  • Go to Dashboard
  • Click New Item
  • Give name
  • Click Pipeline
  • Click OK
  • In Pipeline Section, select Pipeline Script from SCM from drop down for Definition field
  • Choose Git from dropdown for SCM
  • Pu file:///home/leo2019/play_jenkins in repository Url field
  • Click apply

Step three: Create a Jenkinsfile

In the root directory of git repository, create a file called Jenkinsfile. For this example, the file is put in /home/leo2019/play_jenkins/

The following is the content of my test Jenkinsfile

#!/usr/bin/env groovy
pipeline {
  agent any
  stages {
    stage('Stage 1') {
      steps {
        echo 'Hello world!'
        println "Great!"
        sleep(30)
      }
    }
  }
}

Step four: build

Some console output


Started by user unknown or anonymous
Obtained Jenkinsfile from git file:///home/leo2019/play_jenkins
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/My test for blog
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning repository file:///home/leo2019/play_jenkins
 > /usr/bin/git init /var/lib/jenkins/workspace/My test for blog # timeout=10
Fetching upstream changes from file:///home/leo2019/play_jenkins
 > /usr/bin/git --version # timeout=10
 > git --version # 'git version 2.17.1'
 > /usr/bin/git fetch --tags --progress -- file:///home/leo2019/play_jenkins +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /usr/bin/git config remote.origin.url file:///home/leo2019/play_jenkins # timeout=10
 > /usr/bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
 > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 743c558e082f3fe9944db4ecd969f6ffc007aaeb (refs/remotes/origin/master)
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f 743c558e082f3fe9944db4ecd969f6ffc007aaeb # timeout=10
Commit message: "greate"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Stage 1)
[Pipeline] echo
Hello world!
[Pipeline] echo
Great!
[Pipeline] sleep
Sleeping for 30 sec
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Tuesday, 9 November 2021

Which version of alpine linux package will be installed?

Assume you have this docker file. You may wonder what version of bash will be installed.


FROM alpine:3.14
RUN apk --update add apache2 bash

The pakage version depends on alpine version. We can go to Alpine Linux packages to find out.

Monday, 8 November 2021

MongoDB

Install MongoDB in Ubuntu.

start stop server

1. start
sudo systemctl start mongod

2. check status
sudo systemctl status mongod

3. stop
 sudo systemctl stop mongod

4. restart
sudo systemctl restart mongod

5. use mongosh
mongosh

Find connect uri

mongosh

//should see something like the below
Connecting to:mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000

Use node.js to connect to MongoDB

Install node.js MongoDB driver

npm install mongodb --save

Connect

//put it in index.js
const { MongoClient } = require("mongodb");

// Connection URI
const uri = "mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000";

// Create a new MongoClient
const client = new MongoClient(uri);

//database name
const dbName = 'leo_test_db';

async function run() {
  try {
    // Connect the client to the server
    await client.connect();

    // Establish and verify connection
    await client.db("admin").command({ ping: 1 });
    console.log("Connected successfully to server");
    
    const db = client.db(dbName);
    const collection = db.collection('books');

    const insertResult = await collection.insertMany([{'java':2005}, {'PHP':2010}]);
    console.log("insert result", insertResult)

    } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

//run it
node index.js

//output Connected successfully to server

Basic CRUD operations

//insert one
const insertResult = await collection.insertOne({'go':2021});

//insert multiple items
const insertResult = await collection.insertMany([{'java':2005}, {'PHP':2010}]);

mongo shell reference

//open shell
mongosh
//show databases
show dbs

Create a new database

mongosh

//want to create db called newswatcherdb. This command will work
//even we do not have this db yet
use newswatcherdb

//need to add something into db to create db
db.user.insert({name:"leo"})

//new created db in the list
show dbs

//show collections in db
use leo_test_db
show collections //get books
db.books.find() // will show items in collection

Some useful links