10 June 2018

Integrating Jenkins with GitHub

In this exercise, we are going to integrate Github with Jenkins using webhook that triggers an event
every time you commit a change in your code residing in your GitHub repository to invoke a job in your Jenkins.

To start with, you should be having the following in place.

a. A Github account with a code repository 
b. Jenkins up and running
c. Github plugin for integration with Jenkins

First, we going to create a webhook from our code repository by navigating

GitHub > your-code-repository > settings > webhooks > Add webhook 

add Payload URL - https://jenkinsUrl:8080/github-webhook/
Content type - choose application/json
Secret - you can leave this field blank
Which events would you like to trigger this webhook? - Select - Just the push event
Check the Active box - this will deliver the first payload to test the provided URL
Add webhook

Now come to Jenkins are create a freestyle project by navigating

Jenkins > new Item > item name > Freestyle project > OK

select Github project, and give project URL - https://github.com/punitporwal07/aws-codedeploy-linux/
for Source code management, select git and give repository URL - https://github.com/punitporwal07/aws-codedeploy-linux.git
Keep Branch Specifier as - */master
Under Build Triggers - check GitHub hook trigger for GITScm polling
Save

that's it. Now make a code commit in your git repo, the webhook associated with your repository detects the change and trigger a payload to your Jenkins job which you created in the above step.

you will be able to see the console output of your build as below









--

11 May 2018

CLI instruction to work with git


For those who are still struggling with committing and updating their codes or got bored with SVN due to its tedious branching and complicated merging model, Git is the best practice with branches to a certain commit. Git allows you to create, delete, and change a branch anytime without affecting the commits. it works on the principle of Distributed version control

go to https://github.com/new ( provided you have a git account)

GitHub workflow

To work on a repository, this is the workflow GitHub recommends:
  • Create a branch  > non-master
  • Switch branch   > git checkout non-master
  • Make changes  > Work on the branch locally
  • Create a pull request  > git pull 
  • Address review comments
  • Merge your pull request
  • Delete your branch
for more info refer to GitHub's official doc - document

start setting up your git repository 


Git global setup
git config --global user.name "punitporwal07" git config --global user.email "punitporwal07@gmail.com"
 Get a new repository
 git clone https://gitlab.com/punitporwal/repo.git
 cd repo
 git add README.md
 git commit -m "Commit Message"
 git push -u origin master/main

 


 Branching  Use branching to add new features isolated from the working project.

  Create a new branch for you to work on

  Add a branch called maintainence 

 git clone https://github.com/punitporwal07/Myrepo.git
 cd Myrepo
 git checkout -b maintainence  // this will add a new branch called maintainence
 git branch     // this will list all the branches
 touch test.html
 git status     // nothing to add
 git add -A     // this will add html file to stage directory
 git commit -m 'added a maintainence page'
 git push origin maintainence
 git status     // file pushed to maintainence branch, which will be reflected in GUI

Click - Compare and Pull request 


 Update and merge 

Step 1: Clone the repository or update your local repository with the latest changes.

git pull origin main

Step 2: Switch to the base branch of the pull request.
git checkout main

Step 3: Merge the head branch into the base branch.
git merge maintainance

Step 4: Push the changes.
git push -u origin main



 Push a folder to a new Git Repo-

 cd existing_folder
 git config --global user.name "punitporwal07"
 git config --global user.email "punitporwal07@gmail.com"
git init -b main
// Initialize the local directory as a Git repository git add . // Add files in local repository and stages them for commit
 git status          // verfiy all the files
 git commit -m "pushing the data"
 git branch -M main
 git remote add origin https://gitlab.com/punitporwal/repo.git
 git push -u origin main



 
 Push a folder to an existing Git Repo-

 cd existing_folder
 git init
 git pull https://gitlab.com/punitporwal/repo.git
 git add .           // Add files in local repository and stages them for commit
 git status          // verfiy all the files
 git commit -m "pushing the data"
 git remote add origin https://gitlab.com/punitporwal/repo.git
 git push -u origin main




 To unstage a file, use 'git reset -- HEAD YOUR-FILE'.
 git reset -- excludeFile.txt // excludeFile.txt will not be pushed
 git add -u
 git status
 git remote -v  // verifies the remote URL
 git commit -m "Initial commit"
 git push -u origin master

 
Push an existing Git repository cd existing_repo git remote rename origin old-origin git remote add origin https://gitlab.com/punitporwal/repo.git git push -u origin --all git push -u origin --tags


    sample showing push to git


Distributed version control mechanism

known issue

Git push error pre-receive hook declined
[remote rejected] master -> master (pre-receive hook declined)

solution:
GitLab by default marks master branch as protected (See Protecting your code /why). 

Open your project > Settings > Repository and go to "Protected branches", find "master" branch into the list and click "Unprotect" and try again.


For version 8.11 and above: how to protect

Generating your personal access token

navigate to git profile > settings > Developer settings > Personal access tokens > Generate new token  New personal access token  "nameit"  select scope Generate token 
copy token value

use this token at any place where needed

for example in openshift:
$ oc create secret generic git-secret \
--from-literal=username=punitporwal07 \
--from-literal=password=4f41599f5f99e84e7cc9f552ad6c731ce0573a6a