banner
uyoung

uyoung

twitter

Jenkins Deployment of Go Applications

golang

Installing Docker Plugin for Jenkins#

If you haven't installed Jenkins, you can refer to my previous article Deploying Jenkins with Docker

Since we are packaging and deploying the application with Docker, there is no need to install the Go environment (one less step).

Go to the left side menu and select Manage Jenkins -> Manage Plugins -> Available -> Search for Docker

Check the box to install Docker Pipeline

After installation, restart Jenkins

Creating a New Task#

Go back to the homepage and select "New Item"

Choose Freestyle project

Enter the project name

Editing the Task#

Source Code Management#

Under Source Code Management, select the Git option

Repository URL: Enter your Git project address or absolute path
Credentials: Enter the credentials for Git (not required for public projects)
Branches to build: Enter the branch name you want to monitor (default is master, but GitHub now uses main as the default branch, so be careful not to make a mistake)

Build Triggers#

Other task triggers, scheduled builds, polling GitHub projects, GitHub Hook
For now, you can leave everything unchecked and manually run and debug if needed.

Build Environment#

No additional settings required, this is the most error-prone area.

Build#

I will use shell commands for packaging and deployment in this example.

Since my Jenkins can directly access the host's Docker, the application will generate an image directly after packaging.

IMAGES_NAME='go-qqbot'
CONTAINER_NAME='qqbot1'

echo "IMAGES_ID: $GIT_COMMIT"

docker build -t ${IMAGES_NAME} .
docker stop ${CONTAINER_NAME} || true
docker rm ${CONTAINER_NAME} || true
docker run -d --network botnetwork --name ${CONTAINER_NAME} ${IMAGES_NAME}
docker rmi $(docker images | grep "none" | awk '{print $3}')

echo "Deployment completed!"

Here is my Dockerfile:


FROM golang:1.15-alpine AS builder

RUN go env -w GO111MODULE=auto \
  && go env -w CGO_ENABLED=0 \
  && go env -w GOPROXY=https://mirrors.tencent.com/go/,https://goproxy.cn,direct

WORKDIR /build

COPY ./ .

RUN set -ex \
    && cd /build \
    && go build -ldflags "-s -w -extldflags '-static'" -o botqq

FROM alpine:latest

COPY --from=builder /build/botqq /usr/bin/botqq
RUN chmod +x /usr/bin/botqq

WORKDIR /data

ENTRYPOINT [ "/usr/bin/botqq" ]

In this example, I am using the golang:1.15-alpine image to package the application, and then copying the packaged files to the alpine:latest image to run.

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