Methods and steps of gogs + Jenkins + docker automated deployment of. NETCORE

Time:2021-10-20

Environmental description

  • Tencent cloud lightweight server, configuration1c 2g 6mb, the system isubuntu 20.14, docker and Jenkins are on this server,
  • Qunhui 218 + one. Gogs is on this server.

Docker installation

Uninstall old docker


sudo apt-get remove docker docker-engine docker.io containerd runc

Update the apt package index and install the package to allow apt to use the repository over HTTPS


sudo apt-get update

sudo apt-get install \
  apt-transport-https \
  ca-certificates \
  curl \
  gnupg \
  lsb-release

Add docker’s official GPG key


curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Install docker


sudo apt-get install docker-ce docker-ce-cli containerd.io

Verify that docker is installed correctly by running Hello world. Output Hello from docker! Indicates that the installation was successful


sudo docker run hello-world

Different system installation methods can be consultedOfficial installation instructions for docker

Gogs installation

Gogs is a lightweight and easy to build self-service git service developed in go language. Gogs is selected mainly because it is much lighter than the internal temporary utilization rate of gitlab. Gogs will greatly reduce system consumption and occupy 100MB of memory when running. My gogs runs on Qunhui. The effect is the same. They are hosted in docker

Pull gogs image


sudo docker pull gogs/gogs

Create gogs file mount path


mkdir -p /var/gogs

The startup container 6022 is HTTPS and 6080 is http. You can use thedocker psCommand to see if it started successfully.


docker run -d --name=my_gogs -p 6022:22 -p 6080:3000 -v /var/gogs:/data gogs/gogs

Pass after container startuphttp://xxxxxxxx:6080Initial configuration of gogs

For database type, it is recommended to select SQLite3 from Linux, which is enough to support a team of about a dozen people. Domain name fill in the domain name or IP address of the server where gogs is located. The HTTP port number is consistent with the internal port of the container. Use the URL to fill in the domain name port or IP port to access gogs. Click Install and register a new user to log in

After gogs installation, create a demo warehouse for later use

Gogs official documents

Hosting. NETCORE services in docker

Create a new webapi project

Add a dockerfile file to the project and simply configure it

#Specify dependent version
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim 
WORKDIR /app

COPY . /publish
WORKDIR /publish
#Set the exposed port of docker container
EXPOSE 80
#Set time zone
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' >/etc/timezone 
#Program entry
ENTRYPOINT ["dotnet", "DemoWebApi.dll"]

Publish the API service. If there is no dockerfile file in the published file, you need to manually modify the project file. After publishing successfully, copy the published file to the folder specified by the server


 <ItemGroup>
        <None Update="Dockerfile">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
    </ItemGroup>

Enter the root path of the publishing file just copied by the server and executedocker build -t {REPOSITORY}:{TAG} .The command generates a docker image file through dockerfile. Repository is the name of the image and tag is the label. for exampledocker build -t demo:v1 .

adoptdocker imagesCommand to view all generated mirrors

After the image is generated successfully, you can create and run the container through the image. implementdocker run --name demoapi -dp 5009:80/tcp demo:v1Command to create and run a container
-d: Run the container in the background and return the container ID;
-p: Specify the port mapping. The format is: host port: container port. The port of the container is the port where your program starts. It is recommended to write it directly in the project.
–Name: container name

After execution, you candocker psTo view the status of all running containers, you need to view all containers that can be useddocker ps -acommand

Use postman to test whether the deployment is successful

List some common docker commands

  • Docker restart {container ID}#Restart container
  • Docker start {container ID}#Start container
  • Docker attach {container ID}#In this way, entering and exiting the container will cause the container to exit, and the attach can let the user see the standard output of the container
  • Docker attach {container ID} -- sig proxy = false#Adding parameters will not cause synchronous exit
  • Docker exec - it {container ID} / bin / Bash#To enter the container, you need to execute the command in the container, and you need to use the exec command
  • Docker logs {container ID} - F#Trace log output – f trace log output
  • Docker RM - F {container ID}#Delete stopped containers
  • docker rmi {REPOSITORY:TAG}#Deletes the specified mirror
  • docker image prune#Delete the suspended image, that is, the image that is not referenced by the container

Here, the service has been hosted in docker, but each release requires building a new image, then stopping the old container and creating a new container, which virtually increases the workload. Jenkins can do this for us

Jenkins installation

Jenkins relies on Java, so you need to install Java SDK. Here, select java8


sudo apt-get install openjdk-8-jdk

Install the LTS version of Jenkins


wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > \
    /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

View running statussystemctl status jenkinsNormally, the following contents will be output

The default port of Jenkins is 8080. After successful installation, it passes thehttp://xxxx:8080You can access it. In the first step, you need to unlock it first. The administrator password will be output after the installation is successful. You can also use the commandcat /var/lib/jenkins/secrets/initialAdminPassword

After entering the password, enter the initialization page and select the recommended plug-in to install

The plug-in installation is progressing. It will take some time

After that, create a management account and log in. Enter system management, select plug-in management and searchgogsInstall after plug-in.

After installation, you need to restart Jenkins and visit the linkhttp://xxxx:8080/restartClick restart after or executeservice jenkins restartrestart

To facilitate the execution of the script, Jenkins needs to run as root and edit the filevim /etc/sysconfig/jenkinsorvim /etc/default/jenkinscancelJENKINS_USERComment and set the value toJENKINS_USER="root"Modify folder permissions after


chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins

Jenkins common operations

  • start-upservice jenkins start
  • restartservice jenkins restartOr visithttp://xxxx:8080/restart
  • stop itservice jenkins stopOr visithttp://xxxx:8080/exit
  • Reload profilehttp://xxxx:8080/reload

Using Jenkins automation

Because you need to build the project on the server, you need to install the. NETCORE environment. You can seeMicrosoft official documentsSelf installation

Push the new project to the gogs warehouse and click warehouse advanced settings

Select the web hook and add the web hook of the new gogs

Configure the web hook. The push address is preceded by the access link of Jenkins, and the job name behind can be defined by yourself. Select to trigger the hook only when pushing, or select the event by yourself

After adding, click Manage web hook again, select the newly created hook, and click test push to verify whether it is normal. If the exception thrown is a job that is not defined, it means that the hook is normal. If it is other exception, you can check whether the push address is correct and whether the gogs plug-in on Jenkins is installed correctly

Go back to the Jenkins page and create a new task. The task name is the job name in the previous gogs. Choose to build a free style software project

Add the warehouse configuration. The repository URL is your warehouse address. Click add your warehouse credential information, and finally specify the branch of the operation

Select the shell script to execute during construction. The script can also be placed on the server and called here. For convenience, it is written here directly

#Judge whether the demo image exists
docker images | grep demo &> /dev/null
if [ $? -ne 0 ]
then
   #No, no processing
	echo "not existed demo"
else
    #If the image exists, the container is considered to be running by default
	echo "existed demo"
   	#Stop deleting containers and mirrors
    docker stop demoapi
    docker rm -f demoapi
    docker rmi demo:v1
fi

#After rebuilding, generate the image and run the container
cd DemoWebApi/
#Publish to specified path
dotnet publish -c Release -o /publish
#Start the container after entering the path to generate an image
cd /publish
docker build -t demo:v1 .
docker run --name demoapi -dp 5009:80/tcp demo:v1

After saving, click build now to verify. If the build fails, you can view the detailed build process and error information in the console output

Go back to the management web hook page of gogs and push again. After success, Jenkins will automatically build and publish, and then automatically publish as long as push to the master branch

summary

They simply recorded the whole process of their tossing. Jenkins and docker still have many functions, which should be understood slowly in the process of use.

This is the end of this article on the methods and steps of gogs + Jenkins + docker automated deployment. NETCORE. For more information about docker automated deployment. NETCORE, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!