Build and deploy to a PHP web app

Azure Pipelines

A web app is a lightweight way to host a web application. In this step-by-step guide, you'll learn how to create a pipeline that continuously builds and deploys your PHP app. Your team can then automatically build each commit in GitHub, and if you want, automatically deploy the change to an Azure App Service. You can select different PHP versions.

Prerequisites

  • A GitHub account, where you can create a repository. If you don't have one, you can create one for free.

  • An Azure DevOps organization. If you don't have one, you can create one for free. (An Azure DevOps organization is different from your GitHub organization. You can give your DevOps organization and your GitHub organization the same name if you want alignment between them.)

    If your team already has one, then make sure you're an administrator of the Azure DevOps project that you want to use.

  • An ability to run pipelines on Microsoft-hosted agents. You can either purchase a parallel job or you can request a free tier. To request a free tier, follow the instructions in this article. Please note that it may take us 2-3 business days to grant the free tier.

  • An Azure account. If you don't have one, you can create one for free.

    Tip

    If you're new at this, the easiest way to get started is to use the same email address as the owner of both the Azure Pipelines organization and the Azure subscription.

Get the code

If you already have an app in GitHub that you want to deploy, you can try creating a pipeline for that code.

However, if you are a new user, then you might get a better start by using our sample code. In that case, fork this repo in GitHub:

https://github.com/Azure-Samples/php-docs-hello-world

Create an Azure App Service

Sign in to the Azure Portal, and then select the Cloud Shell button in the upper-right corner.

Create an Azure App Service on Linux.

# Create a resource group
az group create --location westus --name myapp-rg

# Create an app service plan of type Linux
az appservice plan create -g myapp-rg -n myapp-service-plan --is-linux

# Create an App Service from the plan with PHP as the runtime
az webapp create -g myapp-rg -p myapp-service-plan -n my-app-name --runtime "PHP|7.0"

Sign in to Azure Pipelines and connect to Azure

Sign in to Azure Pipelines. After you sign in, your browser goes to https://dev.azure.com/my-organization-name and displays your Azure DevOps dashboard.

Within your selected organization, create a project. If you don't have any projects in your organization, you see a Create a project to get started screen. Otherwise, select the Create Project button in the upper-right corner of the dashboard.

Now create the service connection:

  1. From your project dashboard, select Project settings on the bottom left.

  2. On the settings page, select Pipelines > Service connections, select New service connection, and then select Azure Resource Manager.

  3. Select Service principal (automatic) and Next.

  4. The Add an Azure Resource Manager service connection dialog box appears.

    • Scope Select Subscription.
    • Subscription Select your Azure portal subscription.
    • Resource Group Select the resource group you created earlier.
    • Service connection name Type a name and then copy and paste it into a text file so you can use it later.
    • Select Grant access permission to all pipelines.

    New service connection dialog box

Tip

If you need to create a connection to an Azure subscription that's owned by someone else, see Create an Azure Resource Manager service connection with an existing service principal.

Create the pipeline

  1. Sign in to your Azure DevOps organization and navigate to your project.

  2. Go to Pipelines, and then select New pipeline.

  3. Walk through the steps of the wizard by first selecting GitHub as the location of your source code.

  4. You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.

  5. When the list of repositories appears, select your repository.

  6. You might be redirected to GitHub to install the Azure Pipelines app. If so, select Approve & install.

When the Configure tab appears, select PHP. Your new pipeline appears.

  1. Take a look at the pipeline to see what it does.

  2. After you've looked at what the pipeline does, select Save and run to see the pipeline in action.

  3. Select Save and run, after which you're prompted for a commit message because Azure Pipelines added the azure-pipelines.yml file to your repository. After editing the message, select Save and run again.

You just created and ran a pipeline that we automatically created for you, because your code appeared to be a good match for the PHP Azure Pipelines template.

Edit the pipeline

After the pipeline has run, select the vertical ellipses in the upper-right corner of the window and then select Edit pipeline.

Set some variables for your deployment

# at the top of your YAML file 
# set some variables that you'll need when you deploy
variables:
  # the name of the service connection that you created above
  serviceConnectionToAzure: name-of-your-service-connection
  # the name of your web app here is the same one you used above
  # when you created the web app using the Azure CLI
  appName: my-app-name
  # the name of your functionApp is what you provided as
  # stagingDirectory in pom.xml
  functionAppName: 'javafunctions'
# ...

Deploy to your app service

# add these as the last steps (below all the other `task` items under `steps`)
# to deploy to your app service
- task: ArchiveFiles@1
  displayName: Archive files
  inputs:
    rootFolder: $(System.DefaultWorkingDirectory)
    includeRootFolder: false
    archiveType: zip

- task: PublishBuildArtifacts@1
  displayName: Publish Artifact
  inputs:
    PathtoPublish: $(build.artifactstagingdirectory)

- task: AzureWebApp@1
  displayName: Azure Web App Deploy
  inputs:
    azureSubscription: $(serviceConnectionToAzure)
    appType: webAppLinux
    appName: $(appName)
    package: $(build.artifactstagingdirectory)/**/*.zip

Run the pipeline and check out your site

You're now ready to save your changes and try it out!

  1. Select Save in the upper-right corner of the editor.

  2. In the dialog box that appears, add a Commit message such as add deployment to our pipeline, and then select Save.

  3. In the pipeline editor, select Run.

When the Build #nnnnnnnn.n has been queued message appears, select the number link to see your pipeline in action.

After the pipeline has run, check out your site!

https://my-app-name.azurewebsites.net/

Clean up resources

Whenever you're done with the resources you created above, you can use the following command to delete them:

az group delete --name myapp-rg

Type y when prompted.