我正在尝试创建我的 Golang 项目的 Docker 映像,并通过 Jenkins 声明式管道将其上传到 Docker Hub。
我能够构建我的项目并运行我的所有测试。
我的Jenkinsfile是这样的:
#!/usr/bin/env groovy
// The above line is used to trigger correct syntax highlighting.
pipeline {
agent { docker { image 'golang' } }
stages {
stage('Build') {
steps {
// Create our project directory.
sh 'cd ${GOPATH}/src'
sh 'mkdir -p ${GOPATH}/src/MY_PROJECT_DIRECTORY'
// Copy all files in our Jenkins workspace to our project directory.
sh 'cp -r ${WORKSPACE}/* ${GOPATH}/src/MY_PROJECT_DIRECTORY'
// Copy all files in our "vendor" folder to our "src" folder.
sh 'cp -r ${WORKSPACE}/vendor/* ${GOPATH}/src'
// Build the app.
sh 'go build'
}
}
// Each "sh" line (shell command) is a step,
// so if anything fails, the pipeline stops.
stage('Test') {
steps {
// Remove cached test results.
sh 'go clean -cache'
// Run Unit Tests.
sh 'go test ./... -v'
}
}
}
}
我的Dockerfile(如果需要)如下:
# Make a golang container from the "golang alpine" docker image from Docker Hub.
FROM golang:1.11.2-alpine3.8
# Expose our desired port.
EXPOSE 9000
# Create the proper directory.
RUN mkdir -p $GOPATH/src/MY_PROJECT_DIRECTORY
# Copy app to the proper directory for building.
ADD . $GOPATH/src/MY_PROJECT_DIRECTORY
# Set the work directory.
WORKDIR $GOPATH/src/MY_PROJECT_DIRECTORY
# Run CMD commands.
RUN go get -d -v ./...
RUN go install -v ./...
# Provide defaults when running the container.
# These will be executed after the entrypoint.
# For example, if you ran docker run <image>,
# then the commands and parameters specified by CMD would be executed.
CMD ["MY_PROJECT"]
智慧大石
相关分类