Jenkins Golang Declarative Pipeline: Build Docker Image and Push to Docker Hub
I'm trying to create a Docker image of my Golang project and upload it to Docker Hub via Jenkins Declarative Pipeline.
I am able to build my project and run all my tests.
My Jenkinsfile
is as follows:
#!/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'
}
}
}
}
My Dockerfile
(if needed) is as follows:
# 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"]
I am able to get my Docker Hub credentials via:
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
DOCKER_CREDENTIALS = credentials('MY_JENKINS_CREDENTIAL_ID')
}
For reference, here is what I got to work in my Freestyle job:
Execute Shell:
# Log in to Docker.
sudo docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
# Make a Docker image for our app.
cd /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_DIRECTORY/
sudo docker build -t MY-PROJECT-img .
# Tag our Docker image.
sudo docker tag MY-PROJECT-img $DOCKER_USERNAME/MY-PROJECT-img
# Push our Docker image to Docker Hub.
sudo docker push $DOCKER_USERNAME/MY-PROJECT-img
# Log out of Docker.
docker logout
NOTE: I have given Jenkins sudo privileges by doing the following:
Open the sudoers file for editing:
sudo visudo -f /etc/sudoers
Press "i" to go into INSERT mode.
Paste the following:
jenkins ALL=(ALL) NOPASSWD: ALL
Press "ESC" to exit INSERT mode.
To Save, enter:
:w
To Quit, enter:
:q
I believe this should be possible by adding a new stage
and agent
in my Declarative Pipeline, but I haven't found anything online.
docker go jenkins jenkins-pipeline
add a comment |
I'm trying to create a Docker image of my Golang project and upload it to Docker Hub via Jenkins Declarative Pipeline.
I am able to build my project and run all my tests.
My Jenkinsfile
is as follows:
#!/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'
}
}
}
}
My Dockerfile
(if needed) is as follows:
# 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"]
I am able to get my Docker Hub credentials via:
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
DOCKER_CREDENTIALS = credentials('MY_JENKINS_CREDENTIAL_ID')
}
For reference, here is what I got to work in my Freestyle job:
Execute Shell:
# Log in to Docker.
sudo docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
# Make a Docker image for our app.
cd /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_DIRECTORY/
sudo docker build -t MY-PROJECT-img .
# Tag our Docker image.
sudo docker tag MY-PROJECT-img $DOCKER_USERNAME/MY-PROJECT-img
# Push our Docker image to Docker Hub.
sudo docker push $DOCKER_USERNAME/MY-PROJECT-img
# Log out of Docker.
docker logout
NOTE: I have given Jenkins sudo privileges by doing the following:
Open the sudoers file for editing:
sudo visudo -f /etc/sudoers
Press "i" to go into INSERT mode.
Paste the following:
jenkins ALL=(ALL) NOPASSWD: ALL
Press "ESC" to exit INSERT mode.
To Save, enter:
:w
To Quit, enter:
:q
I believe this should be possible by adding a new stage
and agent
in my Declarative Pipeline, but I haven't found anything online.
docker go jenkins jenkins-pipeline
add a comment |
I'm trying to create a Docker image of my Golang project and upload it to Docker Hub via Jenkins Declarative Pipeline.
I am able to build my project and run all my tests.
My Jenkinsfile
is as follows:
#!/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'
}
}
}
}
My Dockerfile
(if needed) is as follows:
# 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"]
I am able to get my Docker Hub credentials via:
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
DOCKER_CREDENTIALS = credentials('MY_JENKINS_CREDENTIAL_ID')
}
For reference, here is what I got to work in my Freestyle job:
Execute Shell:
# Log in to Docker.
sudo docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
# Make a Docker image for our app.
cd /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_DIRECTORY/
sudo docker build -t MY-PROJECT-img .
# Tag our Docker image.
sudo docker tag MY-PROJECT-img $DOCKER_USERNAME/MY-PROJECT-img
# Push our Docker image to Docker Hub.
sudo docker push $DOCKER_USERNAME/MY-PROJECT-img
# Log out of Docker.
docker logout
NOTE: I have given Jenkins sudo privileges by doing the following:
Open the sudoers file for editing:
sudo visudo -f /etc/sudoers
Press "i" to go into INSERT mode.
Paste the following:
jenkins ALL=(ALL) NOPASSWD: ALL
Press "ESC" to exit INSERT mode.
To Save, enter:
:w
To Quit, enter:
:q
I believe this should be possible by adding a new stage
and agent
in my Declarative Pipeline, but I haven't found anything online.
docker go jenkins jenkins-pipeline
I'm trying to create a Docker image of my Golang project and upload it to Docker Hub via Jenkins Declarative Pipeline.
I am able to build my project and run all my tests.
My Jenkinsfile
is as follows:
#!/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'
}
}
}
}
My Dockerfile
(if needed) is as follows:
# 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"]
I am able to get my Docker Hub credentials via:
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
DOCKER_CREDENTIALS = credentials('MY_JENKINS_CREDENTIAL_ID')
}
For reference, here is what I got to work in my Freestyle job:
Execute Shell:
# Log in to Docker.
sudo docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
# Make a Docker image for our app.
cd /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_DIRECTORY/
sudo docker build -t MY-PROJECT-img .
# Tag our Docker image.
sudo docker tag MY-PROJECT-img $DOCKER_USERNAME/MY-PROJECT-img
# Push our Docker image to Docker Hub.
sudo docker push $DOCKER_USERNAME/MY-PROJECT-img
# Log out of Docker.
docker logout
NOTE: I have given Jenkins sudo privileges by doing the following:
Open the sudoers file for editing:
sudo visudo -f /etc/sudoers
Press "i" to go into INSERT mode.
Paste the following:
jenkins ALL=(ALL) NOPASSWD: ALL
Press "ESC" to exit INSERT mode.
To Save, enter:
:w
To Quit, enter:
:q
I believe this should be possible by adding a new stage
and agent
in my Declarative Pipeline, but I haven't found anything online.
docker go jenkins jenkins-pipeline
docker go jenkins jenkins-pipeline
asked Nov 12 '18 at 14:43
Floating Sunfish
129112
129112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I figured it out.
My updated Jenkinsfile
is as follows:
#!/usr/bin/env groovy
// The above line is used to trigger correct syntax highlighting.
pipeline {
// Lets Jenkins use Docker for us later.
agent any
// If anything fails, the whole Pipeline stops.
stages {
stage('Build & Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
}
}
stage('Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
// Remove cached test results.
sh 'go clean -cache'
// Run Unit Tests.
sh 'go test ./... -v -short'
}
}
stage('Docker') {
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
// (NOTE 2: You can't print credentials in the pipeline for security reasons.)
DOCKER_CREDENTIALS = credentials('my-docker-credentials-id')
}
steps {
// Use a scripted pipeline.
script {
node {
def app
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = docker.build("${env.DOCKER_CREDENTIALS_USR}/my-project-img")
}
stage('Push image') {
// Use the Credential ID of the Docker Hub Credentials we added to Jenkins.
docker.withRegistry('https://registry.hub.docker.com', 'my-docker-credentials-id') {
// Push image and tag it with our build number for versioning purposes.
app.push("${env.BUILD_NUMBER}")
// Push the same image and tag it as the latest version (appears at the top of our version list).
app.push("latest")
}
}
}
}
}
}
}
post {
always {
// Clean up our workspace.
deleteDir()
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264540%2fjenkins-golang-declarative-pipeline-build-docker-image-and-push-to-docker-hub%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I figured it out.
My updated Jenkinsfile
is as follows:
#!/usr/bin/env groovy
// The above line is used to trigger correct syntax highlighting.
pipeline {
// Lets Jenkins use Docker for us later.
agent any
// If anything fails, the whole Pipeline stops.
stages {
stage('Build & Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
}
}
stage('Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
// Remove cached test results.
sh 'go clean -cache'
// Run Unit Tests.
sh 'go test ./... -v -short'
}
}
stage('Docker') {
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
// (NOTE 2: You can't print credentials in the pipeline for security reasons.)
DOCKER_CREDENTIALS = credentials('my-docker-credentials-id')
}
steps {
// Use a scripted pipeline.
script {
node {
def app
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = docker.build("${env.DOCKER_CREDENTIALS_USR}/my-project-img")
}
stage('Push image') {
// Use the Credential ID of the Docker Hub Credentials we added to Jenkins.
docker.withRegistry('https://registry.hub.docker.com', 'my-docker-credentials-id') {
// Push image and tag it with our build number for versioning purposes.
app.push("${env.BUILD_NUMBER}")
// Push the same image and tag it as the latest version (appears at the top of our version list).
app.push("latest")
}
}
}
}
}
}
}
post {
always {
// Clean up our workspace.
deleteDir()
}
}
}
add a comment |
I figured it out.
My updated Jenkinsfile
is as follows:
#!/usr/bin/env groovy
// The above line is used to trigger correct syntax highlighting.
pipeline {
// Lets Jenkins use Docker for us later.
agent any
// If anything fails, the whole Pipeline stops.
stages {
stage('Build & Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
}
}
stage('Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
// Remove cached test results.
sh 'go clean -cache'
// Run Unit Tests.
sh 'go test ./... -v -short'
}
}
stage('Docker') {
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
// (NOTE 2: You can't print credentials in the pipeline for security reasons.)
DOCKER_CREDENTIALS = credentials('my-docker-credentials-id')
}
steps {
// Use a scripted pipeline.
script {
node {
def app
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = docker.build("${env.DOCKER_CREDENTIALS_USR}/my-project-img")
}
stage('Push image') {
// Use the Credential ID of the Docker Hub Credentials we added to Jenkins.
docker.withRegistry('https://registry.hub.docker.com', 'my-docker-credentials-id') {
// Push image and tag it with our build number for versioning purposes.
app.push("${env.BUILD_NUMBER}")
// Push the same image and tag it as the latest version (appears at the top of our version list).
app.push("latest")
}
}
}
}
}
}
}
post {
always {
// Clean up our workspace.
deleteDir()
}
}
}
add a comment |
I figured it out.
My updated Jenkinsfile
is as follows:
#!/usr/bin/env groovy
// The above line is used to trigger correct syntax highlighting.
pipeline {
// Lets Jenkins use Docker for us later.
agent any
// If anything fails, the whole Pipeline stops.
stages {
stage('Build & Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
}
}
stage('Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
// Remove cached test results.
sh 'go clean -cache'
// Run Unit Tests.
sh 'go test ./... -v -short'
}
}
stage('Docker') {
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
// (NOTE 2: You can't print credentials in the pipeline for security reasons.)
DOCKER_CREDENTIALS = credentials('my-docker-credentials-id')
}
steps {
// Use a scripted pipeline.
script {
node {
def app
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = docker.build("${env.DOCKER_CREDENTIALS_USR}/my-project-img")
}
stage('Push image') {
// Use the Credential ID of the Docker Hub Credentials we added to Jenkins.
docker.withRegistry('https://registry.hub.docker.com', 'my-docker-credentials-id') {
// Push image and tag it with our build number for versioning purposes.
app.push("${env.BUILD_NUMBER}")
// Push the same image and tag it as the latest version (appears at the top of our version list).
app.push("latest")
}
}
}
}
}
}
}
post {
always {
// Clean up our workspace.
deleteDir()
}
}
}
I figured it out.
My updated Jenkinsfile
is as follows:
#!/usr/bin/env groovy
// The above line is used to trigger correct syntax highlighting.
pipeline {
// Lets Jenkins use Docker for us later.
agent any
// If anything fails, the whole Pipeline stops.
stages {
stage('Build & Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
}
}
stage('Test') {
// Use golang.
agent { docker { image 'golang' } }
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'
// Remove cached test results.
sh 'go clean -cache'
// Run Unit Tests.
sh 'go test ./... -v -short'
}
}
stage('Docker') {
environment {
// Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".
// (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)
// The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.
// (NOTE 2: You can't print credentials in the pipeline for security reasons.)
DOCKER_CREDENTIALS = credentials('my-docker-credentials-id')
}
steps {
// Use a scripted pipeline.
script {
node {
def app
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = docker.build("${env.DOCKER_CREDENTIALS_USR}/my-project-img")
}
stage('Push image') {
// Use the Credential ID of the Docker Hub Credentials we added to Jenkins.
docker.withRegistry('https://registry.hub.docker.com', 'my-docker-credentials-id') {
// Push image and tag it with our build number for versioning purposes.
app.push("${env.BUILD_NUMBER}")
// Push the same image and tag it as the latest version (appears at the top of our version list).
app.push("latest")
}
}
}
}
}
}
}
post {
always {
// Clean up our workspace.
deleteDir()
}
}
}
answered Nov 13 '18 at 7:01
Floating Sunfish
129112
129112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264540%2fjenkins-golang-declarative-pipeline-build-docker-image-and-push-to-docker-hub%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown