Skip to content

Instantly share code, notes, and snippets.

@jeremysells
Last active March 6, 2019 15:41
Show Gist options
  • Save jeremysells/a5218eba05449e5fc7f106070b383764 to your computer and use it in GitHub Desktop.
Save jeremysells/a5218eba05449e5fc7f106070b383764 to your computer and use it in GitHub Desktop.
Jenkinsfile Docker Composer PHP example
// This was a test for Jenkins running Docker containers
stage('Build') {
agent {
docker {
image 'composer:latest'
// If you use SSH cloning you need this. TODO - look at tokens for private repos and http clone
args '-v /etc/passwd:/etc/passwd:ro'
// args '-e HOME -v $HOME/.ssh:$HOME/.ssh:ro -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro'
}
}
steps {
// Test ssh connection (if you are using ssh clone, you need this) - This is to make a connection issue obvious
sh 'ssh -T [email protected]'
// Download dependencies
sh "composer install --ignore-platform-reqs --no-scripts --no-dev --no-interaction --no-progress"
// Dump the autoloader (for performance)
sh "composer dump-autoload --optimize --no-interaction"
}
}
stage('Package') {
when {
anyOf {
branch 'develop'
branch 'master'
}
}
agent {
docker {
image 'docker:latest'
args '-e HOME'
}
}
steps {
script {
//Build an push production image to the registry
sh("""
source .env \
&& docker build \
--tag "\${REGISTRY_URI}/\${APPLICATION_CODE}:$version" \
--tag "\${REGISTRY_URI}/\${APPLICATION_CODE}: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=$version" \
--build-arg "APPLICATION_VERSION_HASH=$versionHash" \
--build-arg "APPLICATION_VERSION_STRING=$versionString" \
--build-arg "APPLICATION_BUILD_DATE=$buildDate" \
--build-arg "APPLICATION_PHP_VERSION=\${APPLICATION_PHP_VERSION}" \
--pull \
--file Dockerfile \
.
""")
sh("source .env && docker push \${REGISTRY_URI}/\${APPLICATION_CODE}:latest")
sh("source .env && docker push \${REGISTRY_URI}/\${APPLICATION_CODE}:$version")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment