|
#!/usr/bin/env groovy |
|
|
|
//This probably should not be used. Use the Jenkinsfile.declarative |
|
|
|
//---The CI itself------------------------------------------------------------ |
|
node() { |
|
// Checks out the project |
|
stage('Checkout') { |
|
checkout scm |
|
} |
|
|
|
// Prints some debugging info |
|
stage('Debug') { |
|
//Print out env information (handy for debugging/testing) |
|
sh(name: "Output env info for debugging", |
|
script: "env | sort", |
|
) |
|
sh(name: "Output branch information for debugging", |
|
script: "git branch -vv", |
|
) |
|
} |
|
|
|
// Areas |
|
gitlabBuilds(builds: ['Test', 'Feedback', 'Package']) { |
|
|
|
// Test |
|
stage('Test') { |
|
gitlabCommitStatus("Test") { |
|
// Custom |
|
sh(name: "Copy dist files", |
|
script: "rm -f build/docker-compose/app/config/*.* && cp build/docker-compose/app/config/dist/*.* build/docker-compose/app/config/", |
|
) |
|
|
|
//Build the development container |
|
sh(name: "Docker Development Image - Build", |
|
script: "docker-compose build", |
|
) |
|
|
|
//Build the development container |
|
sh(name: "Ant build.xml ci-check in container", |
|
script: "docker-compose run --no-deps --rm app ant ci-check", |
|
) |
|
|
|
//Make sure everything is down and cleanup |
|
//old networks - '--rm' does not do this :( and the eventually fill up |
|
sh(name: "Docker stop all and cleanup", |
|
script: """ |
|
bash -c ' |
|
docker-compose stop \ |
|
&& docker-compose down \ |
|
&& docker-compose kill \ |
|
&& docker-compose rm \ |
|
&& docker network prune -f |
|
' |
|
""" |
|
) |
|
} |
|
} |
|
|
|
// If there is a merge request say that it passed |
|
// Update anything with deployment version information (E.g. Continuous deployment) |
|
stage('Feedback') { |
|
gitlabCommitStatus("Feedback") { |
|
// Gitlab PR? |
|
if (env.gitlabMergeRequestId) { |
|
//Add a comment that it passed |
|
addGitLabMRComment(comment: 'Looks good to me!') |
|
|
|
//Merge the PR? (not sure if this is right) |
|
acceptGitLabMR(mergeCommitMessage: 'Passed CI, so merging!') |
|
|
|
//Add echo |
|
echo 'Is Gitlab merge request' |
|
} else { |
|
echo 'Skip, as is not a Gitlab merge request' |
|
} |
|
} |
|
} |
|
|
|
// Publish (build production, push image, deploy) |
|
stage('Package') { |
|
gitlabCommitStatus("Package") { |
|
// Should we use continuous deployment / Push production image |
|
if (env.gitlabTargetBranch.contains('refs/tags/v') || env.gitlabBranch.contains('hotfix/') || env.gitlabSourceBranch == 'develop' || env.BRANCH_NAME == 'develop') { |
|
//Build an push production image to the registry |
|
//Note: The tail below, trims the first 'v' off the version |
|
sh( name: "Build Docker Image and Push", |
|
script: """ |
|
bash -c ' |
|
source .env; |
|
|
|
APPLICATION_VERSION=`git describe --tags --always HEAD | tail -c +2`; |
|
APPLICATION_VERSION_STRING=`git describe --tags --always HEAD`; |
|
APPLICATION_VERSION_HASH=`git rev-parse HEAD`; |
|
APPLICATION_BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"`; |
|
IMAGE_TAG_SPECIFIC="\${REGISTRY_URI}/\${APPLICATION_CODE}:\${APPLICATION_VERSION}"; |
|
IMAGE_TAG_LATEST="\${REGISTRY_URI}/\${APPLICATION_CODE}:latest"; |
|
|
|
docker build \ |
|
--tag "\${IMAGE_TAG_SPECIFIC}" \ |
|
--tag "\${IMAGE_TAG_LATEST}" \ |
|
--build-arg "APPLICATION_DOCKER_FROM_IMAGE=\${APPLICATION_DOCKER_FROM_IMAGE}" \ |
|
--build-arg "APPLICATION_CODE=\${APPLICATION_CODE}" \ |
|
--build-arg "APPLICATION_NAME=\${APPLICATION_NAME}" \ |
|
--build-arg "APPLICATION_DESCRIPTION=\${APPLICATION_DESCRIPTION}" \ |
|
--build-arg "APPLICATION_VENDOR_NAME=\${APPLICATION_VENDOR_NAME}" \ |
|
--build-arg "APPLICATION_VERSION=\${APPLICATION_VERSION}" \ |
|
--build-arg "APPLICATION_VERSION_HASH=\${APPLICATION_VERSION_HASH}" \ |
|
--build-arg "APPLICATION_VERSION_STRING=\${APPLICATION_VERSION_STRING}" \ |
|
--build-arg "APPLICATION_BUILD_DATE=\${APPLICATION_BUILD_DATE}" \ |
|
--build-arg "APPLICATION_PHP_VERSION=\${APPLICATION_PHP_VERSION}" \ |
|
--build-arg "APPLICATION_COMPOSER_INSTALL_FLAGS=--no-dev" \ |
|
--pull \ |
|
--file Dockerfile \ |
|
. \ |
|
&& docker push \${IMAGE_TAG_SPECIFIC} \ |
|
&& docker push \${IMAGE_TAG_LATEST} |
|
' |
|
""" |
|
) |
|
|
|
//We could do a push to a version application here. If only one was around :) |
|
} else { |
|
echo "Skip, as is not a production branch" |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|