0

I am setting up a Jenkins pipeline to deploy a PHP application. The application is using composer, so I am running composer install -o in the script to ensure that all dependencies are there. The test setup also ensures that the vendor/autoload.php is generated (it's in the phpunit.xml bootstrap config)

My scripts are based on https://modess.io/jenkins-php/ and http://jenkins-php.org/

My issue is that the vendor folder is not included, and the generated config.inc.php is not included.

The Jenkins log shows that my deployment line sh "cp -rp ${SOURCE_DIR}/* ${DEPLOY_DIR}" is being expanded into cp -rp src/globals.template.inc.php src/index.php src/phpinfo.php /usr/nasShare/htdocs/sometest which is not including the mentioned files. (in fact those are the only files in the src directory in SCM. Looking in the working directory on the jenkins server, the files are generated... )

Jenkinsfile

#!groovy
pipeline {
    agent any
    environment {
        SOURCE_DIR="src"
        TEMPLATE_FILE="globals.template.inc.php"
        CONFIG_FILE="globals.inc.php"
    }

    stages {
        stage ('Testing'){
            steps {
                    echo "Running ant clean"
                    sh 'ant clean'
                    echo "running composer"
                    sh "composer install -o -d ${SOURCE_DIR}"
                    echo ""
                    sh 'ant quick-build'
            }
        }

        stage ('Staging'){
            steps {
                echo "Building config file"
                script {
                     def inptext = readFile file: "${SOURCE_DIR}/${TEMPLATE_FILE}" 
                     inptext = inptext.replaceAll(~/¤GIT_BRANCH¤/, "${GIT_BRANCH_NAME}")
                     inptext = inptext.replaceAll(~/¤GIT_COMMIT¤/, "${sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()}")
                     inptext = inptext.replaceAll(~/¤GIT_TAG¤/, "${sh(returnStdout: true, script: "git -C . describe --tags").trim()}")
                     writeFile file: "${SOURCE_DIR}/${CONFIG_FILE}", text: inptext
                }

            }
        }

        stage ('Remote Deploy'){
            agent any
            when{
                //https://stackoverflow.com/a/44231270/1725871
                environment name: 'DEPLOY_TYPE', value: 'remote'
            }
            steps {
                echo "Deploying via SSH  on ${SSH_SERVER_NAME}:${DEPLOY_DIR}"
                //TODO: rename backup file
                sh "ssh ${SSH_USERNAME}@${SSH_SERVER_NAME} tar -cvpzf BACKUP_FNAME ${DEPLOY_DIR}/* "
                sh "ssh ${SSH_USERNAME}@${SSH_SERVER_NAME} rm -R ${DEPLOY_DIR}/*"
                sh "scp -rp ${SOURCE_DIR}/* ${SSH_USERNAME}@${SSH_SERVER_NAME}:${DEPLOY_DIR}"
                //TODO: delete backup on success
            }
        }

        stage ('Local Deploy'){
            agent any
            when{
                environment name: 'DEPLOY_TYPE', value: 'local'
            }
            steps {
                echo "Deploying to ${DEPLOY_DIR} "
                //TODO: backup existing files
                sh "rm -R ${DEPLOY_DIR}/*"
                sh "cp -rp ${SOURCE_DIR}/* ${DEPLOY_DIR}"
            }
        }
    }
}
JoSSte
  • 2,953
  • 6
  • 34
  • 54

1 Answers1

0

I found the error of my way. Adding ${WORKSPACE}/ to my SOURCE_DIR variable solved it. now all the expected files are being copied.

Working environment from jenkinsfile

environment {
    SOURCE_DIR="${WORKSPACE}/src"
    TEMPLATE_FILE="globals.template.inc.php"
    CONFIG_FILE="globals.inc.php"
}
JoSSte
  • 2,953
  • 6
  • 34
  • 54