-
Notifications
You must be signed in to change notification settings - Fork 0
Jenkins
https://www.youtube.com/watch?v=pMO26j2OUME&list=PLy7NrYWoggjw_LIiDK1LXdNN82uYuuuiC
jenkins-docker-app
- Jenkins Container Setup
- Create Multibranch Pipeline with Git Repo
- Types of Credentials
- Jenkinsfile
Code -> Commit-> Git Repo-> Trigger build -> Jenkinsfile:test, build, deploy
Run Jenkins in Docker Container
1.1. Find a Jenkins image in dockerhub
The image 'jenkins/jenkins' should be used.
- Expose port 8080 -- By default runs on that port
- Expose port 50000 -- Master/Slaver Communication
- Run in detached mode -- run container in the background
- Bind named volume -- persist data of Jenkins, bind host directory and container directory
- image version -- latest (jenkins/jenkins:lts)
.
docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
docker ps
docker logs your_ContainerID
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
3adc1a26a8fd4128a816ca8ba01da498
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
IlLya 1234 Illya K ilhaifa@yandex.ru Jenkins URL http://localhost:8080/ Save and Finish
- Freestyle: simple, single tasks e.g. run tests
- Pipeline: whole delivery cycle e.g. test|build|... for a single branch
- Multibranch Pipeline : like pipeline for multiple branches
Updates Available plugins Installed plugins Advanced settings Download progress
Name: "my-pipeline"
Branch Source = Project you want to build
Add Source -> Git
Project Repository - Specify the URL of this remote repository. This uses the same syntax as your git clone command HTTPS.
https://github.com/ILLYAKO/jenkins-docker-app.git
Credentials - > choose created credentials for git repo.
Behaviours -> Add ->
Behaviors: Discover branches
Filter by name (with regular expression) -> .* (default means all branches)
Build Configuration
Mode -> by Jenkins(default)
Script Path -> Jenkinsfile(default)
Save
"Credentials" Plugin to store and manage them centrally
Dashboards -> Manage Jenkins -> Manage Credentials -> Credentials -> Jenkins System(Store) -> global(Domains) -> Add Credentials
Kinds:
-Username with password
-GitHub App
-SSH Username with private key
-Secret file
-Secret text
-Certificate
Credential Scopes:
-System: Only available on Jenkins server NOT for Jenkins jobs
-Global: Everywhere accessible
-Project: Limited to project, ONLY with multibranch pipeline (on project page)
ID = Reference for your credentials
Kind: User with password
Scope: Global (Jenkins, nodes, items, all child items, etc)
Username: global
Password: 1234
ID: global
Kind: User with password
Scope: System (Jenkins and nodes only)
Username: system
Password: 1234
ID: system
kind: username with password
Username: my@email.com
password: *******
ID: demo-app-git-credentials
Go Dashboard -> my-pipeline -> Configuration -> Branch Sources -> Behaviours -> Add
Behaviors: Discover branches
Filter by name(with regular expression):^dev|master|feature.*$
- Scripted
-first syntax
-Groovy engine
-Advanced scripting capabilities, high flexibility
-difficult to start
- Declarative -recent addition -easier to get started, but not that powerful -pre-defined structure
-'pipeline' must be top-level -'agent' -where to execute -'stages' -where the 'work' happens -'stage' and 'steps' -'step' -where execute a command in the Jenkins server
pipeline {
agent any
stages {
stage("build") {
steps {
echo 'building the application...'
}
}
stage("test") {
steps {
echo 'testing the application...'
}
}
stage("deploy") {
steps {
echo 'deploying the application...'
}
}
}
}
Dashboard -> my-pipeline -> Scan Multibranch Pipeline Now
Dashboard -> my-pipeline -> Scan Multibranch Pipeline Log
Project configuration must be done in the project pipeline or Jenkinsfile (NOT in the branch)
Dashboard->my-pieline->branch->Build History(choose last)-> Replay -> Main Script-> Change -> Run
...
steps {
echo 'building the application...'
script{
def test = 2+2 > 3 ? 'cool' : 'not good'
echo test
}
}
...
Dashboard->my-pieline->branch->Build History(choose dropdown menu) -> Restart from Stage -> Chose Stage -> Run
Git Integration
-Push Notification
-Polling for Changes
commit -> Git Repo -> trigger build
2 ways to trigger a build
-Push Notification: Version Control notifies Jenkins on a new commit(more efficient)
-Polling: Jenkins polls in regular intervals
Trigger build:
-Install the Jenkins plugin based on your Version Control System
-Configure Repository Server Hostname
-Access Token and Credential in Dashboard->Manage Jenkins->Configure System->GitHub->Add GitHub Server
Developer->commits->Git Repo->pushes to->Jenkins Webhook url->Jenkins lets me trigger that build for you
Dashboard->my-pipeline->Configuration->Scan Multibranch Pipeline Triggers->Interval(1 hour)->Save Goof strategy to configure both as a backup plan
Scripted pipeline. Pipeline as a code -scripted pipeline -is created in your repository
Pipeline Syntax
- Scripted
-first syntax
-Groovy engine
-Advanced scripting capabilities, high flexibility
-difficult to start
- Declarative -recent addition -easier to get started, but not that powerful -pre-defined structure
-'pipeline' must be top-level -'agent' -where to execute -'stages' -where the 'work' happens -'stage' and 'steps' -'step' -where execute a command in the Jenkins server
pipeline {
agent any
stages {
stage("build") {
steps {
echo 'building the application...'
}
}
stage("test") {
steps {
echo 'testing the application...'
}
}
stage("deploy") {
steps {
echo 'deploying the application...'
}
}
}
}
Execute some logic AFTER all stages executed Conditions:
-always -success -failure Build Status or Build Status Changes
pipeline {
agent any
stages {
...
}
post {
always{}
success{}
failure{}
}
}
The "When condition" directs if steps will be executed
...
stage("build") {
when {
expression {
BRANCH_NAME == 'dev' && CODE_CHANGE == true
}
}
steps {
echo 'testing the application...'
}
}
stage("test") {
when {
expression {
BRANCH_NAME == 'dev' || BRANCH_NAME == 'master'
}
}
steps {
echo 'testing the application...'
}
}
...
Jenkins variable located here http://localhost:8080/env-vars.html/ Double quotes must use.
pipeline {
agent any
environment {
NEW_VERSION = '1.3.0'
}
stages {
stage("build") {
steps {
echo "building the application version ${NEW_VERSION}"
}
...
- Define Credentials in Jenkins GUI (Global Domain)
- "credentials("credentialId")" binds the credentials to your env variable
- For that you need a "Credentials Binding" Plugin
pipeline {
agent any
environment {
NEW_VERSION = '1.3.0'
SERVER_CREDENTIAS = credentials('server-credentials') // credentialID from Jenkins server
}
....
...
stage("deploy") {
steps {
echo 'deploying the application...'
withCredentials([
usernamePassord(credentials: 'server-credentials', usernameVariable: USER, passwordVariable: PWD)
]){
sh "some script ${USER} ${PWD}"
}
}
}
...
-Credentials -Credentials Binding
Access build tools for your projects
Only 3 build tools are available: gradle, maven, jdk
the tool should be preinstalled in the Jenkins server Dashboard -> Manage Jankins -> Global Tool Configuration
pipeline {
agent any
tools{
maven 'Maven'
}
...
Types of Parameter:
-string (name, defaultValue, description)
-choice (name, choices, description)
-booleanParam (name, defaultValue, description)
pipeline {
agent any
parameters {
string(name: 'VERSION', defaultValue: '', description: 'version to deploy on prod')
choice(name: 'VERSION', choices: ['1.1.0', '1.2.0', '1.3.0'])
booleanParam(name: 'executeTests', defaultValue: true)
}
...
when {
expression {
params.executeTests == true
}
}
...