I copied it myself, PASTed it myself, packed it myself. You got to pay me more for this hard work, boss

Bring it 🤞

After typing code and packing it yourself. That’s too much work. Now automated build tools are very convenient, using Jenkins to create an online automated build can save a lot of time. This article covers everything from tool installation environment installation, scripting and finally server deployment. It is so easy to follow the first step of automation.

directory

  1. Download and install Jenkins
  2. Initialize Jenkins
  3. Create an automated build of git repositories
  4. The server installs various environments as well as adding automatic packaging to build flutter APK scripts
  5. Open the package results for download using the Nginx server
  6. Enterprise wechat, nailing or feishu notification compilation and deployment results

First, download and install Jenkins

My alicloud ECS server was installed with centos 6 system.

An episode:

I just tried to install Jenkins on centos 6 server, but the Ali cloud mirror source of yum no longer maintains centos 6. Since there is nothing in it, we will reinstall the ECS server to Centos8.

What do you need? :

Start by searching the Linux Jenkins installation tutorial. Installing Jenkins depends on other operating environments, and the pull code also needs the corresponding Git tool. The libraries to be installed include the following:

  1. Java
  2. git
  3. Jenkins

Install the Java

Before installing the openjdk, check whether the system has the openJDK and type the Java command directly in the terminal. If you have the Java environment, it will output a string of things. If not, it will tell you that the command cannot be found. We used Yum to install Java

#1 Update the yum source
#2 Check the Java libraries in YUM
#The installation java11
sudo yum update
sudo yum list java*
#Installing other versions of Java is error-proneSudo yum install Java -- 1.8.0 comes with its develCopy the code

Java is successfully installed in usr/lib/ Java, which is in the environment variable by default.

Install git

Jenkins will definitely need this tool to pull git repository code

yum install git
Copy the code

Install Jenkins

The following commands are executed in sequence: first download Jenkins dependencies and add them to the yum repository; then install Jenkins via yum.

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install jenkins
Copy the code

With these three commands, the installation is theoretically successful.

Configure and initialize Jenkins

List Jenkins related contents:

  • /usr/lib/jenkins/: Jenkins installation directory, where the WAR package will be placed
  • /etc/sysconfig/jenkins: Jenkins profile, “port”, “JENKINS_HOME” etc can be configured here.
  • /var/lib/jenkins/: The default JENKINS_HOME.
  • /var/log/jenkins/jenkins.log: Jenkins log file.

If we configure Jenkins build warehouse, the code will automatically pull to/var/lib/Jenkins/workspace directory.

Example Change the Jenkins user to root

Modify the vim /etc/sysconfig/jenkins file via vim. Find JENKINS_USER and modify it as follows.

$JENKINS_USER="root"
Copy the code

Modify the port

The default Jenkins port is 8080, but it may already be occupied by another application. If you want to change it, find $JENKINS_PORT in the configuration file above and change it to the corresponding port.

Start the Jenkins

java -jar /usr/lib/jenkins/jenkins.war 
Copy the code

Just access the address after startup. For example, http://127.0.0.1:8080. Of course, ali cloud server needs to configure security groups and open port 8080 TCP inbound. There is an initialization process after the visit and we recommend installing the Jenkins plugin. Just follow the instructions along the way. When we’re done, we can go to the desk.

idea

Create a project

So let’s go ahead and create a new itemChoose the freestyle projectThe new project will be visible in all after completion.

Configuration items

Next, enter the project and click Configure to configure the associated Git repository, access credentials, and build scriptsChoose Git for source management and SSH or user name and password for credentials.This completes the git repository configuration. At build time, the code is automatically pulled in/var/lib/jenkins/In addition, you need to have appropriate build scripts for your project type, and you need to install the corresponding environment on the server. I’m going to try to build a Flutter project, starting with a Flutter example,web or Android or ios.

Fourth, build the FLUTTER project

As developers we know that we need to install the FLUTTER environment, and the same goes for building flutter on the server. After many failed attempts, I finally managed to install flutter using Snap and indirectly install the FVM tool. We tried to install FVM, a multi-version management tool for Flutter. I installed it easily on MacOS, so I plan to install it on centos as well.

Brew failed to install FVM

Note: I tried to install FVM on centos with BREW

  1. Brew installation failed for user root. Procedure
  2. Individual user failed to install brew download. There was a problem accessing the Github domain name
  3. Failed to install using imageAnother active Homebrew update process is already in progress
  4. And all kinds of other failures ~ a lot, all kinds of network problems system problems

Finally, abandon brew installation and prepare to install FVM using DART installation PUB

Failed to install DART on centos. Procedure

Finally, find the official centos installation documentation, which is done by compiling the DART. In the end, it failed. A lot of holes in the middle.

  1. Googlesource.com cannot be accessed when obtaining devToolgiteeClone warehouse solution
  2. Dart source code fetch is also a variety of network error

Finally, after various struggles, I decided to give up directly installing the DART. I planned to install the Flutter and save the country with its own DART curve.

Install the flutter on centos

Officials told us to install the Flutter using SNAP.

sudo snap install flutter --classic
Copy the code

Of course we didn’t install Snap, so look for the Snap installation tutorial to install and finally succeed!!

$ sudo dnf install epel-release
$ sudo dnf upgrade
$ sudo yum install snapd
$ sudo systemctl enable --now snapd.socket
$ sudo ln -s /var/lib/snapd/snap /snap
Copy the code

Finally, the Flutter was successfully installed

Install the FVM

Snap has environment variables directly after installing the Flutter. Dart Pub Global Activate FVM install the flutter Doctor installation environment. Finally, configure the environment variables

export PATH="$PATH":"$HOME/.pub-cache/bin"
Copy the code

Next, use FVM to configure the security flutter environment using FVM Global 2.5.0-5.2. Pre to configure the global FLUTTER environment.

Since a flutter was installed, you need to delete the previous flutter environment variables and set ~/ FVM /default/bin to the new flutter environment variable

The android SDK configuration

Select the folder you want to install (I’ll call it BASE_PATH) and install the SDK with Flutter using the following command: Install SDK

cd $BASE_DIR mkdir android-sdk cd android-sdk wget https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip unzip commandlinetools-linux-6200805_latest.zip ./tools/bin/sdkmanager --sdk_root=$(pwd) "build-tools; 28.0.3 "" emulator" platform "tools" "platforms; android-28" "tools"Copy the code

Packaging apk

In the Flutter default project, packaging is easy, just need the Flutter build apk. This command is written in Jenkins’ build script.

Everything was fine and should have gone well, but during compilation, the server froze and could not be packaged for dozens of minutes. The performance of the server simply cannot withstand android APK compilation. The next best thing is to compile the Web.

Five, use the Nginx server to place the packaged results

  1. In accordance with theThe official tutorialInstall nginx. Run after configuring the sourceyum install nginx
  2. Run nginxsudo systemctl start nginx
  3. Set nginx to start automaticallysudo systemctl enable nginx
  4. You can see it by visiting the domain namenginxThe page

Here is the default path for Nginx:

  • Nginx configuration path: /etc/nginx/
  • PID directory: /var/run/nginx.pid
  • Error log: /var/log/nginx/error.log
  • Access log: / var/log/nginx/access. Log
  • The default site directory is /usr/share/nginx/html

Configure nginx: vim /etc/nginx.conf

# port listen 8080; Server_name localhost; # root /data; }Copy the code

Nginx -t = nginx -t = nginx -t = nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Copy the code

Then execute nginx -s reload to update the nginx configuration file. After compiling, you just need to transfer the static pages to the web server folder. Everything is perfect.

Six, enterprise wechat, nail nail or fly book notice results

Search for their respective Webhook methods. I use fly book notification, directly add group robots in fly book, get webhook link to make post request.

Build scripts and deliverables for the final configuration

My final build shell script looks like this:

#Set up your environment variables
export PATH=$PATH:/root/fvm/default/bin
export ANDROID_SDK_ROOT=/usr/local/android-sdk-linux
#Compile the product
flutter build web --web-renderer canvaskit --no-sound-null-safety --release 
#Delete old sites
rm -rf /usr/share/nginx/html/web
#Deploy the sitemv build/web/ /usr/share/nginx/html/ curl -X POST -H "Content-Type: Application /json" -d '{"msg_type":"text","content":{"text":" http://www.guuguo.top/ "}} 'https://open.feishu.cn/open-apis/bot/v2/hook/ * * * * *Copy the code

When compiled, flutter Web is automatically compiled, placed under static resource servers for external Internet access, and submitted to coding. Web pages can be hosted in Tencent Cloud via coding for external access.

As a result, visit http://47.92.80.168/web/ to see the deployed page. The first is Ali Cloud Nginx server, the second is Tencent cloud free hosting.

The end

Finally, limited by the performance of the server, I failed to implement android APK packaging, which was a pity, but I also learned some new things and completed the packaging and publishing work of the Web.

Later not afraid of electricity, you can take your computer when an automated server

The end of the flower is topless topless topless topless topless topless