1. Background
Gitlab is an open source version control system that can realize code warehouse management, version control, collaborative development and other functions. Golang is a compiled, high-performance language that has attracted much attention from developers because of its excellent concurrency performance and high development efficiency.
In actual use, we may need to deploy golang applications to Gitlab for automated construction and continuous integration. This article will introduce how to deploy golang applications on Gitlab.
2. Preparation work
1. Install Gitlab
First you need to install Gitlab. You can refer to the installation tutorial provided by Gitlab official website for installation.
2. Install golang
Secondly, you need to install golang. You can directly download the latest installation package from the official website for installation. After the installation is complete, you also need to set environment variables.
3. Create a golang project
1. Create a project
Create a new golang project in Gitlab and select "Create a project". Enter the project name, description and other information, select the default settings, and there will be a git warehouse address after the creation is successful.
2. Initialize the project
Enter the local golang project directory and execute the following command to initialize the project into a git warehouse:
git init
Associate the project to the Gitlab warehouse address:
git remote add origin [Gitlab 仓库地址]
Submit the project file to the Gitlab repository:
git add . git commit -m "Initial commit" git push -u origin master
After the submission is completed, we will successfully upload the golang project to the Gitlab repository.
3. Configure the gitlab-ci.yml file
1. Create the file
Create a .gitlab-ci.yml file in the root directory of the golang project.
2. Modify the configuration
Write the following configuration in the .gitlab-ci.yml file:
image: docker/compose:1.18.0 services: - docker:dind before_script: - apk update && apk add --no-cache curl git - curl https://glide.sh/get | sh stages: - build - deploy build: stage: build script: - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com - docker pull registry.gitlab.com/xxxxxxxxx/golang:latest || true - docker build --cache-from registry.gitlab.com/xxxxxxxxx/golang:latest -t registry.gitlab.com/xxxxxxxxx/golang:$CI_BUILD_TAG --pull . - docker push registry.gitlab.com/xxxxxxxxx/golang:$CI_BUILD_TAG deploy: stage: deploy script: - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com - docker pull registry.gitlab.com/xxxxxxxxx/golang:$CI_BUILD_TAG || true - docker run -d --name appname -p 8080:8080 registry.gitlab.com/xxxxxxxxx/golang:$CI_BUILD_TAG
The xxx in the configuration file needs to be replaced with your own Gitlab warehouse address.
Among them, the command in before_script is to install the glide dependency tool in the Docker container.
Build and deploy represent the two phases of build and deployment respectively. The build phase is mainly used to build Docker images, and the deploy phase is mainly used to deploy the built Docker images to the server.
4. Build and deploy golang applications
1. Build Docker image
Execute the following command in the project root directory to build the image:
docker build -t golang-app .
2. Deploy the application
Execute the following command to deploy the Docker image to the server:
docker run -d --name my-golang-app -p 8080:8080 golang-app
So far, we have successfully deployed the golang application to Gitlab. Through Gitlab's CD/CI function, we can realize automated construction and continuous integration, improve development efficiency, and deploy and update conveniently and quickly.
The above is the detailed content of gitlab deploy golang. For more information, please follow other related articles on the PHP Chinese website!