Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #242

Merged
merged 42 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
11cd906
updates to ci-cd tooling
pcarney8 Nov 20, 2018
5e935a4
delete a lot of things, adding in some site.yml, adding in secrets, k…
pcarney8 Nov 20, 2018
dc8f07d
update site.yml
pcarney8 Nov 20, 2018
b09ee8e
update todos
pcarney8 Nov 20, 2018
e125569
more todos on the params
pcarney8 Nov 20, 2018
dc32dad
first pass at Jenkinsfile
pcarney8 Nov 20, 2018
3275878
revert Jenkinsfile to match master
pcarney8 Nov 30, 2018
1c77da8
cleanup params, make all dynamic except for the jenkins-slaves
pcarney8 Dec 3, 2018
8a4ac55
missed one in last commit
pcarney8 Dec 3, 2018
3bbadb7
Merge branch 'master' of github.com:rht-labs/labs-ci-cd into cleanup
pcarney8 Dec 3, 2018
52db494
cleanup readme, move running with docker to docs directory, update re…
pcarney8 Dec 12, 2018
ed07f4a
remove slave tests, update vars for ci-for-ci
pcarney8 Dec 18, 2018
a342f6c
add create action to the projectrequests
pcarney8 Dec 18, 2018
79fe493
hardcode for testing
pcarney8 Dec 18, 2018
2f1adf3
update toolbox params
pcarney8 Dec 18, 2018
b66df88
fix params, add hoverfly deployment
pcarney8 Dec 18, 2018
9b2652e
fix namespace variable for hoverfly deploy
pcarney8 Dec 18, 2018
14caa48
updates
pcarney8 Dec 18, 2018
d68ed06
add todos, move nexus role up
pcarney8 Dec 20, 2018
acdccc3
remove the colon in the session timeout and the jenkins url param
pcarney8 Dec 20, 2018
eed4d72
move to my branch for ci
pcarney8 Dec 20, 2018
d49071c
some comments, and update to use redhat-cop for configure nexus role …
pcarney8 Dec 20, 2018
9551c39
remove my name on namespace
pcarney8 Dec 20, 2018
6875555
remove shared lib secret
pcarney8 Dec 20, 2018
6738a20
comment out slack and shared lib configurations
pcarney8 Dec 20, 2018
5c55789
update jenkinsfile
pcarney8 Dec 20, 2018
f655647
checking remotes
pcarney8 Dec 20, 2018
e707c81
operating from my branch
pcarney8 Dec 20, 2018
a31da8d
remove redundancy
pcarney8 Dec 20, 2018
f10723c
move things around
pcarney8 Dec 20, 2018
29b791d
add jenkins config
pcarney8 Dec 20, 2018
3777d55
remove the directory thing
pcarney8 Dec 20, 2018
00af730
use webhook for ci build
pcarney8 Jan 2, 2019
ab80c0d
get info from github instead of using git commands
pcarney8 Jan 2, 2019
28bf334
try again
pcarney8 Jan 2, 2019
bc95fa4
statuses url
pcarney8 Jan 2, 2019
dc85442
updates
pcarney8 Jan 2, 2019
40f730d
hardcode cleanup to verify it works still
pcarney8 Jan 2, 2019
dfa24e4
update python tag, cleanup jenkinsfile comments and increase timeout,…
pcarney8 Jan 2, 2019
5aac019
fix duplicate stage name
pcarney8 Jan 2, 2019
b7595cc
remove unneeded variables and secrets
pcarney8 Jan 4, 2019
da9ce12
update for when it gets merged and tagged
pcarney8 Jan 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
openshift-applier
roles/*
!roles/configure-nexus
*.pyc
.idea
*.iml
Expand Down
210 changes: 38 additions & 172 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ def notifyGitHub(state) {
sh "curl -u ${env.USER_PASS} -d '${state}' -H 'Content-Type: application/json' -X POST ${env.PR_STATUS_URI}"
}

def getGitHubPullRequest() {
def output = sh(returnStdout: true, script: "curl -u ${env.USER_PASS} -H 'Content-Type: application/json' -X GET ${env.PR_URI}")

def json = readJSON text: output

return json
}

def clearProjects(){
sh """
oc delete project $PR_CI_CD_PROJECT_NAME || rc=\$?
Expand All @@ -25,7 +33,7 @@ pipeline {
}

environment {
// GLobal Vars
// Global Vars
JOB_NAME = "${JOB_NAME}".replace("/", "-")
GIT_SSL_NO_VERIFY = true
URL_TO_TEST = "https://google.com"
Expand All @@ -42,8 +50,7 @@ pipeline {
}

stages {
// prepare environment and ask user for the PR-ID
stage("prepare environment") {
stage("Prepare Environment") {
agent {
node {
label "master"
Expand All @@ -54,7 +61,8 @@ pipeline {
script {
env.OCP_API_SERVER = "${env.OPENSHIFT_API_URL}"
env.OCP_TOKEN = readFile('/var/run/secrets/kubernetes.io/serviceaccount/token').trim()
// taken from original j-file

// Get the PR ID
timeout(time: 1, unit: 'HOURS') {
env.PR_ID = input(
id: 'userInput', message: 'Which PR # do you want to test?', parameters: [
Expand All @@ -65,31 +73,34 @@ pipeline {
}
}

// env.PR_GITHUB_TOKEN = sh (returnStdout: true, script : 'oc get secret labs-robot-github-oauth-token --template=\'{{.data.password}}\' | base64 -d')
env.PR_GITHUB_TOKEN = new String("oc get secret labs-robot-github-oauth-token --template='{{.data.password}}'".execute().text.minus("'").minus("'").decodeBase64())
env.PR_CI_CD_PROJECT_NAME = "labs-ci-cd-pr-${env.PR_ID}"
env.PR_DEV_PROJECT_NAME = "labs-dev-pr-${env.PR_ID}"
env.PR_TEST_PROJECT_NAME = "labs-test-pr-${env.PR_ID}"

env.PR_GITHUB_TOKEN = new String("oc get secret labs-robot-github-oauth-token --template='{{.data.password}}'".execute().text.minus("'").minus("'").decodeBase64())
if (env.PR_GITHUB_TOKEN == null || env.PR_GITHUB_TOKEN == ""){
error('PR_GITHUB_TOKEN cannot be null or empty')
}
env.USER_PASS = "${env.PR_GITHUB_USERNAME}:${env.PR_GITHUB_TOKEN}"

env.PR_BRANCH = "pull/${env.PR_ID}/head"
env.PR_URI = "https://api.github.com/repos/rht-labs/labs-ci-cd/pulls/${env.PR_ID}"
env.PR_STATUS_URI = getGitHubPullRequest().statuses_url

echo env.PR_STATUS_URI
}
}
}

// Clear any old or existing projects from the cluster to ensure a clean slate to test against
stage('clear existing projects') {
stage("Clear Existing Projects") {
steps {
echo "Removing old PR projects if they exist"
echo "Removing old PR projects if they exist to ensure a clean slate to test against"
clearProjects()
}
}

// Merge PR to labs robot branch
// uses sequential stages so same slave / workspace is preserved i.e. no need for stash
// https://jenkins.io/blog/2018/07/02/whats-new-declarative-piepline-13x-sequential-stages/
stage ('spin up shared ansible slave') {
// Uses sequential stages so same slave / workspace is preserved i.e. no need for stash link: https://jenkins.io/blog/2018/07/02/whats-new-declarative-piepline-13x-sequential-stages/
stage ("Spin up shared ansible slave") {
agent {
node {
label "jenkins-slave-ansible"
Expand All @@ -99,38 +110,10 @@ pipeline {
expression { return env.PR_ID }
}
stages {
stage("merge PR") {
stage("Merge PR") {

steps {
echo "Configuring Git, cloning labs-ci-cd & pushing revision to labs-robot/labs-ci-cd.git"
sh """
git config --global user.email "labs.robot@gmail.com"
git config --global user.name "Labs Robot"
mkdir $HOME/.ssh
oc get secret labs-robot-ssh-privatekey --template=\'{{index .data \"ssh-privatekey\"}}\' | base64 -d >> $HOME/.ssh/id_rsa
chmod 0600 $HOME/.ssh/id_rsa
echo -e \"Host github.com\n\tStrictHostKeyChecking no\n\" >> $HOME/.ssh/config

git clone https://github.com/rht-labs/labs-ci-cd.git
cd labs-ci-cd
git remote add ci git@github.com:labs-robot/labs-ci-cd.git
git fetch origin pull/${env.PR_ID}/head:pr
git checkout pr
git rev-parse HEAD
"""

echo "Pushing build state to the PR"
dir('labs-ci-cd') {
script {
// set the vars
env.COMMIT_SHA = sh(returnStdout: true, script: "git rev-parse HEAD")
// env.COMMIT_SHA = "git rev-parse HEAD".execute().text.minus("'").minus("'")
if (env.COMMIT_SHA == null || env.COMMIT_SHA == ""){
error('could not get COMMIT_SHA')
}
env.PR_STATUS_URI = "https://api.github.com/repos/rht-labs/labs-ci-cd/statuses/${env.COMMIT_SHA}"
}
}

notifyGitHub('''{
"state": "pending",
Expand All @@ -139,28 +122,21 @@ pipeline {
}''')

sh """
cd labs-ci-cd
git config --global user.email "labs.robot@gmail.com"
git config --global user.name "Labs Robot"
git checkout master
git fetch origin pull/${env.PR_ID}/head:pr
git fetch origin ${env.PR_BRANCH}:pr
git merge pr --ff
git push ci master:pr-${env.PR_ID} -f
"""
}
}

// Apply ansible inventory of ci-cd and it's ci-fo-ci-cd
stage("apply inventory") {
stage("Apply ci-for-ci Inventory") {
steps {

echo "Applying inventory"
dir('labs-ci-cd') {
// each its own line to that in blue ocean UI they show seperately
sh "ansible-galaxy install -r requirements.yml --roles-path=roles"
sh "ansible-playbook ci-playbook.yml -i inventory/ -e \"target=bootstrap project_name_postfix=-pr-${env.PR_ID} scm_ref=pr-${env.PR_ID}\""
sh "ansible-playbook ci-playbook.yml -i inventory/ -e \"target=tools project_name_postfix=-pr-${env.PR_ID} scm_ref=pr-${env.PR_ID}\""
sh "ansible-playbook ci-playbook.yml -i inventory/ -e \"target=ci-for-labs project_name_postfix=-pr-${env.PR_ID} scm_ref=pr-${env.PR_ID}\""
sh "ansible-playbook ci-playbook.yml -i inventory/ -e \"target=apps project_name_postfix=-pr-${env.PR_ID} scm_ref=pr-${env.PR_ID}\""
}
// each its own line to that in blue ocean UI they show seperately
sh "ansible-galaxy install -r requirements.yml --roles-path=roles"
sh "ansible-playbook site.yml -e ci_cd_namespace=${env.PR_CI_CD_PROJECT_NAME} -e dev_namespace=${env.PR_DEV_PROJECT_NAME} -e test_namespace=${env.PR_TEST_PROJECT_NAME} -e role=admin"

}
// Post can be used both on individual stages and for the entire build.
Expand All @@ -184,69 +160,6 @@ pipeline {
}
}

// Run a start-build of the tests/slave/Jenkinsfile in the newly created Jenkins namespace
// it contains a simple Jenkinsfile that starts each slave in parallel
// and verifies the type of on it is eg that the npm slave has npm on the path
stage("test slaves") {
steps {
notifyGitHub('''{
"state": "pending",
"description": "test are running...",
"context": "Jenkins Slave Tests"
}''')

node('master') {
script {
openshift.withCluster() {
openshift.withProject("${env.PR_CI_CD_PROJECT_NAME}") {
// Let's timeout after 10 minutes
timeout(10) {
echo "Trigger builds of all the jenkins-slaves"
def buildConfigs = openshift.selector('bc')
buildConfigs.withEach {
if (it.name().contains('jenkins-slave')) {
it.startBuild()
}
}

echo "Running test-slaves-pipeline and verifying it's been successful"
def testSlavesPipeline = openshift.selector('bc/test-slaves-pipeline')

// First, clean-out any old 'test-slaves-pipeline' jobs, then start a new one
testSlavesPipeline.related('builds').delete()
testSlavesPipeline.startBuild()

def pipelineBuild = testSlavesPipeline.related('builds')
pipelineBuild.untilEach(1) {
return it.object().status.phase == "Complete"
}
}
}
}
}
}
}
// Post can be used both on individual stages and for the entire build.
post {
success {
notifyGitHub('''{
"state": "success",
"description": "job completed :)",
"context": "Jenkins Slave Tests"
}''')
}
failure {
notifyGitHub('''{
"state": "failure",
"description": "job failed :(",
"context": "Jenkins Slave Tests"
}''')
}
}
}

// Validate the CI Builds & Deployments and App Deployments have deployed
// correctly and come alive as expected.
stage("Verifying CI Builds") {
steps {
notifyGitHub('''{
Expand All @@ -260,7 +173,6 @@ pipeline {
script {
openshift.withCluster() {
openshift.withProject("${env.PR_CI_CD_PROJECT_NAME}") {
// Let's timeout after 5 minutes
timeout(5) {
def pipelineBuildConfigs = openshift.selector('bc', [ type:'pipeline'])
def imageBuildConfigs = openshift.selector('bc', [ type:'image'])
Expand Down Expand Up @@ -306,6 +218,7 @@ pipeline {
}
}
}

stage("Verifying CI Deploys") {
steps {
notifyGitHub('''{
Expand All @@ -319,8 +232,7 @@ pipeline {
script {
openshift.withCluster() {
openshift.withProject("${env.PR_CI_CD_PROJECT_NAME}") {
// Let's timeout after 5 minutes
timeout(5) {
timeout(10) {
def deploymentConfigs = openshift.selector('dc')
deploymentConfigs.withEach {
echo "Checking ${env.PR_CI_CD_PROJECT_NAME}:${it.name()}"
Expand Down Expand Up @@ -352,62 +264,16 @@ pipeline {
}
}
}
stage("Verifying App Deploys") {
steps {
notifyGitHub('''{
"state": "pending",
"description": " job is running...",
"context": "App Deploys"
}''')

node('master') {
echo "Verifying the App Deploys (JAVA) have completed successfully"
script {
openshift.withCluster() {
openshift.withProject("${env.PR_DEV_PROJECT_NAME}") {
// Let's timeout after 10 minutes
timeout(10) {
def deploymentConfigs = openshift.selector('dc')
deploymentConfigs.withEach {
echo "Checking ${env.PR_DEV_PROJECT_NAME}:${it.name()}"
// this will wait until the desired replicas are available
// - or be terminated at timeout
it.rollout().status()
}
}
}
}
}
}
}
// Post can be used both on individual stages and for the entire build.
post {
success {
notifyGitHub('''{
"state": "success",
"description": "job completed :)",
"context": "App Deploys"
}''')
}
failure {
notifyGitHub('''{
"state": "failure",
"description": "job failed :(",
"context": "App Deploys"
}''')
}
}
}

// Clear any old or existing projects from the cluster to ensure a clean slate to test against
stage('Cleaning up CI projects created') {
stage("Clear Existing Projects on Successful Job") {
steps {
echo "Removing created PR projects"
echo "Removing old PR projects if they exist to ensure a clean slate to test against"
clearProjects()
}
}

}
// global post hook
// global post hook
post {
success {
notifyGitHub('''{
Expand Down
Loading