Published on

ARTS 第3周

Authors

Algorithm

Longest Substring Without Repeating Characters

package org.nocoder.leetcode.solution;

import java.util.HashSet;

/**
 * 3. Longest Substring Without Repeating Characters
 * Given a string, find the length of the longest substring without repeating characters.
 * Examples:
 * Given "abcabcbb", the answer is "abc", which the length is 3.
 * Given "bbbbb", the answer is "b", with the length of 1.
 * Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
 *
 * @author jason
 * @date 18/7/23.
 */
public class LongestSubstringWithoutRepeatingCharacters {
    public static int lengthOfLongestSubstring(String s) {
        int res = 0, left = 0, right = 0;
        HashSet<Character> t = new HashSet<>();
        while (right < s.length()) {
            if (!t.contains(s.charAt(right))) {
                t.add(s.charAt(right++));
                res = Math.max(res, t.size());
            } else {
                t.remove(s.charAt(left++));
            }
        }
        return res;
    }

    public static void main(String[] args) {
        String s1 = "abcabcbb";
        // expect: 3, actual: 3
        System.out.println(lengthOfLongestSubstring(s1));

        String s2 = "pwwkew";
        // expect: 3, actual: 3
        System.out.println(lengthOfLongestSubstring(s2));
    }
}

Review

Top 10 Best Practices for Jenkins Pipeline Plugin

https://www.cloudbees.com/blog/top-10-best-practices-jenkins-pipeline-plugin

  • Use the real Jenkins Pipeline
  • Develop your pipeline as code
  • All work within a stage
  • All material work within a node
  • Work you can within a parallel step
  • Acquire nodes within parallel steps
  • Use input within a node block
  • Wrap your inputs in a timeout
  • Don’t set environment variables with env global variable
  • Prefer stashing files to archiving

Tip

CI & CD with Jenkins pipline

node {
   stage('Preparation') {
      git credentialsId: 'gitlab', url: 'https://gitlab.com/yangjinlong86/springboot-docker.git'
   }

   stage('Maven Package'){
       def mvnHome = tool name: 'maven3', type: 'maven'
       def mvnCMD = "${mvnHome}/bin/mvn"
       sh "${mvnCMD} clean package"
   }

   stage('Docker Build Image'){
       sh "docker build -t yangjinlong/springboot-docker:1.0.0 ."
   }

   stage('Docker Push Image'){
       withCredentials([string(credentialsId: 'docker-hub-password', variable: 'dockerHubPassword')]) {
           sh "docker login -u yangjinlong -p ${dockerHubPassword}"
       }
       sh "docker push yangjinlong/springboot-docker:1.0.0"
   }

   stage('Docker Run Container on Test Server'){
       def dockerRun = 'docker run -p 9901:9901 --name dockerapp -d yangjinlong/springboot-docker:1.0.0'
       sshagent(['test-server']) {
            sh "ssh -o StrictHostKeyChecking=no jason@192.168.0.111 ${dockerRun}"
        }
   }
   
}
  • 在jenkins的系统设置增加JAVA_HOME变量
  • pipline语法,带变量的命令必须使用双引号 sh "${mvnCMD} clean package"
  • docker build没有权限问题,将jenkins用户加入docker用户组
  • 绑定加密变量 withCredentials: Bind credentials to variables
  • 使用 ssh agent plugin

Share

这周基本上一直在搞自动化,折腾了gitlab,docker,nexus3,jenkins,最后选用了pipline来做CI 和CD, 完工之后基本和下图一致,大概说明一下步骤:

1.Developer 将代码 push 到Gitlab;

2.Jenkins 从Gitlab pull 最新的代码,通过checkstyle,findbugs,pmd进行代码质量检查,生成检查报告;

3.Jenkins 使用 Maven 命令进行构建;

4.Jenkins 使用 Docker 命令进行镜像构建,并推送到 nexus3 docker 私有仓库;

5.Jenkins 使用ssh命令登录到服务器,并使用Docker命令拉取镜像并启动。