Ficus

Ficus

Ficus 不知道记录些什么?随缘
github

# Building, Packaging, and Deploying with Jenkins on Windows (Freestyle Method)

Building, Packaging, and Deploying with Jenkins on Windows (Freestyle Method)#

`

@[TOC](Table of Contents)


Introduction#

To quickly get started with Jenkins, there aren't many cloud servers available, so Windows is used as the build server, while another Tencent Cloud server is used for deployment.


1. Installation#

Note: Previously, Jenkins was installed in Docker Desktop, but there was an issue: how to use Windows Docker commands within Jenkins in Docker. (The Docker command files in Windows are different from those in Linux)

  1. Download Jenkins from the official website
    https://www.jenkins.io/
    You can proceed with the default installation, and during the selection of the Installation Path, you can install it on another drive.

P.S.
It is important to note that Jenkins' default working path is under the C drive: C:\Users\Username\AppData\Local
If you change it to another drive, you need to find the jenkins.xml file under the path selected during installation and modify it as follows: Note the changes for the three D drives:

  <env name="JENKINS_HOME" value="D:\java\jenkins-win\home\Jenkins\.jenkins"/>
  <arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "D:\java\jenkins-win\jenkins.war" --httpPort=9091 --webroot="D:\java\jenkins-win\home\Jenkins\war"</arguments>
  <pidfile>D:\java\jenkins-win\home\Jenkins\jenkins.pid</pidfile>

Complete configuration

<!--
The MIT License

Copyright (c) 2004-2017, Sun Microsystems, Inc., Kohsuke Kawaguchi, Oleg Nenashev, and other Jenkins contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!--
  Windows service definition for Jenkins.

  To uninstall, run "jenkins.exe stop" to stop the service, then "jenkins.exe uninstall" to uninstall the service.
  Both commands don't produce any output if the execution is successful. 
-->
<service>
  <id>jenkins</id>
  <name>Jenkins</name>
  <description>This service runs Jenkins automation server.</description>
  <env name="JENKINS_HOME" value="D:\java\jenkins-win\home\Jenkins\.jenkins"/>
  <!--
    if you'd like to run Jenkins with a specific version of Java, specify a full path to java.exe.
    The following value assumes that you have java in your PATH.
  -->
  <executable>D:\java\jdk\jdk-11.0.4\bin\java.exe</executable>
  <arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "D:\java\jenkins-win\jenkins.war" --httpPort=9091 --webroot="D:\java\jenkins-win\home\Jenkins\war"</arguments>
  <!--
    interactive flag causes the empty black Java window to be displayed.
    I'm still debugging this.
  <interactive />
  -->
  <logmode>rotate</logmode>

  <onfailure action="restart"/>
  
  <!-- 
    In the case WinSW gets terminated and leaks the process, we want to abort
    these runaway JAR processes on startup to prevent corruption of JENKINS_HOME.
    So this extension is enabled by default.
  -->
  <extensions>
    <!-- This is a sample configuration for the RunawayProcessKiller extension. -->
    <extension enabled="true" className="winsw.Plugins.RunawayProcessKiller.RunawayProcessKillerExtension" id="killOnStartup">
      <pidfile>D:\java\jenkins-win\home\Jenkins\jenkins.pid</pidfile>
      <stopTimeout>10000</stopTimeout>
      <stopParentFirst>false</stopParentFirst>
    </extension>
  </extensions>
  
  <!-- See the referenced examples for more options -->
  
</service>

Finally, restart the Jenkins service

2. Install Common Plugins#

1. Publish Over SSH (Remote Push)#

3. System Settings#

The settings location is as follows
Manage Jenkins -> System Configuration -> Configure System

  1. Configure SSH Servers

image

4. Global Configuration#

Settings location
Manage Jenkins -> System Configuration -> Global Tool Configuration

  1. Maven settings configuration:
    It can also be installed automatically

image

  1. JDK configuration
    It can also be installed automatically

image

  1. Maven configuration
    It can also be installed automatically

image

5. Configure Credentials#

image

Here, you mainly need to add the Git login username and password, and the login password for the remote host.

6. Build, Package, and Deploy Microservices#

Basic idea: Jenkins on Windows pulls the project from the repository and builds it, generating a JAR package; then the JAR package and other files required for packaging, such as Dockerfile, are pushed to the remote deployment server, where the remote server will handle Docker packaging and deployment.
No pipeline is used here, and commands are not written into a bat (which is shell in Linux) script file.
Project structure:
image

  1. Create a new item and select Freestyle project

You can write any description.
1.1 Jenkins Environment Variable Configuration
Check the parameter configuration, Jenkins will pass this parameter to the shell or bat command.

Here, the container name, image name, Spring Boot runtime environment, and other variables are mainly configured:
Container name

image

Image name

image

Spring Boot active runtime environment (configurable)

image

Active environment for Spring Boot in the container

image

1.2 JDK Selection:

image

1.3 Source Code Management:

image

1.4 Build Top-Level Maven Goals

image

image

Explanation:
clean install: Clean and install to the local repository
-pl Specify the module to be packaged
-am Compile the modules that are dependent on -pl as well (this needs to be added, otherwise it will not find all the incoming modules)
pom: Specify the path where the top-level pom is located (the path where the Maven execution command is located); ${WORKSPACE}/pom.xml is the outermost path of the Git project
For example, the project structure:
A-cloud:
B-gateway
pom.xml
C-common
pom.xml

At this point, ${WORKSPACE}=the path where A-cloud is located
-pl B-gateway: Specify B-gateway under A-cloud as the packaging module

-am will also build and package the dependent modules. (At this point, it will look for the pom under A-cloud)

1.5 Add Build Step, Execute Local Host's Bat Command:

Here, the image is first cleaned, and then the Docker image is packaged:

image

Here, the container is first deleted, and then run:

image

The key point is how to push the generated JAR package and the corresponding Dockerfile to the remote host, and let the remote host build and run Docker.

1.6 Post-Build Actions:
Use the option to push to the remote.

image

Dockerfile:

FROM openjdk:11.0.14.1-slim-buster

MAINTAINER FICUS
WORKDIR myapp
VOLUME /tmp
ARG JAR_FILE=./target/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 7070
#ENTRYPOINT ["sh","-c","java -jar -Dspring.profiles.active=dev2  app.jar "]
ENTRYPOINT ["sh","-c","java -jar app.jar --spring.profiles.active=dev2"]

#CMD executes after ENTRY POINT
#CMD [ "redis-server" ]

P.S.
/ficus-cloud can also be replaced with ${WORKSPACE}
source files: The local files that need to be pushed
remote prefix: Remove the path prefix. (This is not filled in, so the remote will create the ficus-gateway/target directory, the ficus-gateway directory)

remote directory: The base path where the files are pushed, this path is appended to the previously set working path
i.e.: /root/mytarget/ficus-cloud

cd mytarget/ficus-cloud: Switch to the project directory (the command's default position is the user's home directory).

1.7 Run

image

7. Build, Package, and Deploy Vue (TODO)#

TODO

Summary#

The build is done using Jenkins on Windows, with remote Linux handling packaging and deployment. The freestyle method of Jenkins is used, and the related bat (which is shell in Linux) commands are not written into files.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.