Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######### LAB 2 ########
- mvn --version
- mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
- cd my-app
- mvn compile
- mvn package
- java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
- ######### LAB 3 #########
- gradle -v
- mkdir gradle_project
- cd gradle_project
- gradle init --type java-application
- // if error after above command:
- JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/
- PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
- export JAVA_HOME
- gradle build
- gradle run
- //Open build.gradle and add below code:
- task hello {
- doLast {
- println 'Hello Gradle!'
- }
- }
- gradle hello
- ######### LAB 4 #########
- mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
- cd my-app
- mvn compile
- mvn package
- // if error in above commands:
- // if pom.xml file does not have property tag then type this code BELOW url tag and ABOVE dependencies tag:
- <properties>
- <maven.compiler.source>1.6</maven.compiler.source>
- <maven.compiler.targer>1.6</maven.compiler.target>
- </properties>
- // OR if it has property tag, then change 17 to 11 in property tag
- // run mvn compile and mvn package commands again and the error should be resolved
- gradle init --type pom
- // if error after above command then run below 3 commands:
- JAVA_HOME=/usr/bin/jvm/java-11-openjdk-amd64
- export PATH=$JAVA_HOME/bin:$PATH
- export JAVA_HOME
- // in build.gradle file:
- //add line:
- apply plugin: 'application'
- //change 1.5 to 1.6 such that:
- sourceCompatibility = 1.6
- targetCompatibility = 1.6
- //at the end add below code:
- mainClassName = 'com.mycompany.app.App'
- gradle build
- gradle run
- ######### LAB 6 #########
- mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
- cd my-app/
- mvn package
- // login to github account, click on Create Repository
- // Give the repo a name (lab-6-devops), click the "Add ReadMe file" and confirm
- git config --global user.email "[email protected]"
- git config --global user.name "stelister"
- git init
- git add pom.xml
- git commit -m "commit maven file"
- ssh-keygen -t ed25519 -C "[email protected]"
- cat /home/student/.ssh/id_ed25519.pub
- // go to github Settings > SSH & GPG Keys and click on "New ssh key"
- // give a title and paste the output of the cat command under 'Key', then click on 'Add SSH key'
- git remote add origin [email protected]:stelister/lab-6-devops
- git config --global push.autoSetupRemote true
- git push origin main
- // if error after above command:
- git pull --rebase origin main
- // now retry:
- git push origin main
- sudo systemctl start jenkins
- sudo systemctl status jenkins
- // open jenkins by navigating to 'localhost:8080' in browser
- // login: username: admin
- sudo cat var/lib/jenkins/secrets/initialAdminPassword
- // paste password
- // in dashboard, click on "New Item", enter Item name and select "Pipelin", click "Ok"
- // scroll down and type the following script:
- pipeline
- {
- agent any
- stages
- {
- stage('Checkout')
- {
- steps
- {
- git branch: 'main', url: 'https://github.com/stelister/lab-6-devops.git';
- }
- }
- stage('Build')
- {
- steps
- {
- sh 'mvn clean package'
- }
- }
- stage('Test')
- {
- steps
- {
- sh 'mvn test'
- }
- }
- }
- }
- // click on 'Save', from the dashboard click on 'Build Now'
- // then click on the build and go to 'Console Output'
- ######### LAB - 7 #########
- make a folder called "lab7" in the home folder
- open this folder
- create these new files using gedit in this folder-
- hosts.ini
- playbook.yml
- playbook2.yml
- create another folder in this folder called "library"
- go to the "library" folder and create a new file in the "library" folder called-
- hello_world.py
- ---------
- In "hosts.ini" file paste below code using gedit-
- [local]
- localhost ansible_connection=local
- ---------
- In "playbook.yml" file paste below code using gedit-
- - name: 1st Playbook
- hosts: local
- connection: local
- tasks:
- - name: Print simple message
- debug:
- msg: "Hello, welcome to Ansible..."
- ---------
- In "playbook2.yml" file paste below code using gedit-
- - name: Test custom module
- hosts: localhost
- gather_facts: no
- tasks:
- - name: Say Hello
- hello_world:
- name: "Ansible"
- register: result
- - name: Show message
- debug:
- msg: "{{result.message}}"
- ---------
- In the "hello_world.py" file paste the below code using gedit-
- #!/usr/bin/python
- from ansible.module_utils.basic import AnsibleModule
- def run_module():
- module_args=dict(name=dict(type='str',required=True))
- module=AnsibleModule(argument_spec=module_args)
- result={"changed":False, "message":f"Hello, {module.params['name']}!"}
- module.exit_json(**result)
- if __name__=="__main__":
- run_module()
- ---------
- Now, right-click open terminal inside "lab7" folder and paste and enter below commands one-by-one for output:
- $ ansible all -i hosts.ini --list-hosts
- $ ansible all -i hosts.ini -m ping
- $ ansible-playbook -i hosts.ini playbook.yml
- $ chmod +x library/hello_world.py
- $ ansible-playbook -i hosts.ini playbook2.yml
- ######### LAB 8 #########
- // Repeat the following steps from Lab-6:
- mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
- cd my-app/
- mvn package
- // login to github account, click on Create Repository
- // Give the repo a name (devops-lab-10), click the "Add ReadMe file" and confirm
- git config --global user.email "[email protected]"
- git config --global user.name "stelister"
- git init
- git add pom.xml
- git commit -m "commit maven file"
- ssh-keygen -t ed25519 -C "[email protected]"
- cat /home/student/.ssh/id_ed25519.pub
- // go to github Settings > SSH & GPG Keys and click on "New ssh key"
- // give a title and paste the output of the cat command under 'Key', then click on 'Add SSH key'
- git remote add origin [email protected]:stelister/devops-lab-10
- git config --global push.autoSetupRemote true
- git push origin main
- // if error after above command:
- git branch -M main
- // repeat
- git push origin main
- // if error still persists:
- git pull --rebase origin main
- // now retry:
- git push origin main
- // NOW, create an empty jar file:
- gedit t.jar
- sudo systemctl start jenkins
- sudo systemctl status jenkins
- // open jenkins by navigating to 'localhost:8080' in browser
- // login: username: admin
- sudo cat /var/lib/jenkins/secrets/initialAdminPassword
- // paste password
- // in dashboard, click on "New Item", enter Item name and select "Pipeline", click "Ok"
- // scroll down and type the following script:
- pipeline
- {
- agent any
- stages
- {
- stage("Checkout")
- {
- steps
- {
- git branch: 'main', url : 'https://github.com/stelister/devops-lab-10.git'
- }
- }
- stage("Build")
- {
- steps
- {
- sh 'mvn clean package'
- }
- }
- stage("Test")
- {
- steps
- {
- sh 'mvn test'
- }
- }
- stage("Archive Artifacts")
- {
- steps
- {
- archiveArtifacts artifacts: '**/target/*.jar', allowEmptyArchive: true
- }
- }
- stage("Deploy")
- {
- steps
- {
- sh """
- export ANSIBLE_HOST_KEY_CHECKING=False
- ansible-playbook -i hosts.ini mydeploy.yml --extra-vars='ansible_become_pass=exam@cse'
- """
- }
- }
- }
- }
- // click on "Save"
- // Click on "Build Now"
- // !! You must click build now, and let your 1st build fail purposely to proceed further
- //create hosts.ini:
- sudo nano /var/lib/jenkins/workspace/<Your pipeline name>/hosts.ini
- //code inside hosts.ini:
- [local]
- localhost ansible_connection=local
- // Save and exit nano::: Ctrl + o > Press enter > Ctrl + x
- //Create a playbook mydeploy.yml by executing the following command:
- sudo nano /var/lib/jenkins/workspace/<Your pipeline name>/mydeploy.yml
- //Paste below code inside mydeploy.yml:
- ---
- - name: Deploy Artifact to Localhost
- hosts: localhost
- tasks:
- - name: Copy the artifact to the target location
- become: true
- become_user: student
- become_method: su
- copy:
- src: "/var/lib/jenkins/workspace/< Your pipeline name >/target/my-app-1.0-SNAPSHOT.jar"
- dest: "/home/student/t.jar"
- // Save and exit nano::: Ctrl + o > Press enter > Ctrl + x
- // go to jenkins and click "Build Now" again
- // Click on “Stages” in the Jenkins Dashboard to see the final result showing all the 5 stages in green tick.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement