commit c66504bebdc10de80384496c92b0128310df39c7 Author: domsgit Date: Sat Jul 25 10:48:47 2020 +0800 first commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..50a57fa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM golang:1.8.0-alpine + +ADD . /go/src/app + +WORKDIR /go/src/app + +RUN GOOS=linux GOARCH=386 go build -v -o /go/src/app/jenkins-app + +CMD ["./jenkins-app"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..bef9efa --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,57 @@ +node('haimaxy-jnlp') { + stage('Clone') { + echo "1.Clone Stage" + // git url: "https://github.com/cnych/jenkins-demo.git" + // checkout scm + script { + build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() + + // if (env.BRANCH_NAME != 'master') { + // build_tag = "${env.BRANCH_NAME}-${build_tag}" + // } + } + } + stage('Test') { + echo "2.Test Stage" + } + stage('Build') { + echo "3.Build Docker Image Stage" + sh "docker build -t 120.78.76.88/cnych/jenkins-demo:${build_tag} ." + } + stage('Push') { + echo "4.Push Docker Image Stage" + withCredentials([usernamePassword(credentialsId: 'qniao-harbor-88', passwordVariable: 'qniao-harbor-88Password', usernameVariable: 'qniao-harbor-88User')]) { + sh "docker login -u ${qniao-harbor-88User} -p ${qniao-harbor-88Password} 120.78.76.88" + sh "docker push 120.78.76.88/cnych/jenkins-demo:${build_tag}" + } + } + stage('YAML') { + echo "5. Change YAML File Stage" + def userInput = input( + id: 'userInput', + message: 'Choose a deploy environment', + parameters: [ + [ + $class: 'ChoiceParameterDefinition', + choices: "Dev\nQA\nProd", + name: 'Env' + ] + ] + ) + echo "This is a deploy step to ${userInput.Env}" + sh "sed -i 's//${build_tag}/' k8s.yaml" + sh "sed -i 's//${env.BRANCH_NAME}/' k8s.yaml" + } + stage('Deploy') { + echo "6. Deploy Stage" + if (userInput.Env == "Dev") { + // deploy dev stuff + } else if (userInput.Env == "QA"){ + // deploy qa stuff + } else { + // deploy prod stuff + // input "确认要部署线上环境吗?" + } + sh "kubectl apply -f k8s.yaml" + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..8e41bcb --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# jenkins-demo +some demo project for jenkins diff --git a/k8s.yaml b/k8s.yaml new file mode 100644 index 0000000..c11d8a8 --- /dev/null +++ b/k8s.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: jenkins-demo + namespace: default +spec: + selector: + matchLabels: + app: jenkins-demo + template: + metadata: + labels: + app: jenkins-demo + spec: + containers: + - image: cnych/jenkins-demo: + imagePullPolicy: IfNotPresent + name: jenkins-demo + env: + - name: branch + value: diff --git a/main.go b/main.go new file mode 100644 index 0000000..7be26fd --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +// Import the fmt for formatting strings +// Import os so we can read environment variables from the system +import ( + "fmt" + "os" +) + +func main() { + fmt.Println("Hello, Kubernetes!I'm from Jenkins CI!") + fmt.Println("BRANCH_NAME:", os.Getenv("branch")) +}