|
| 1 | +# Continuous deployment |
| 2 | + |
| 3 | +As highlighted previously, continuous integration and continuous delivery/deployment (or CI/CD) are core to DevOps. They're all about incorporating new code into your existing code base (CI) and pushing the necessary artifacts to where they need to be (CD). And as we've learned, automation is key to success with DevOps as a whole, and CI/CD in particular. Let's explore the last step here, how to implement a CD flow to deploy your application. |
| 4 | + |
| 5 | +## Scenario |
| 6 | + |
| 7 | +Everything's ready to go! The initial application has been built. You've made the desired changes. You created the environment in the cloud to host the app. Now it's time to deploy! The shelter is excited to see their site go live. Let's create the workflow to deploy the application whenever new changes are pushed to `main`. |
| 8 | + |
| 9 | +## Creating the workflow |
| 10 | + |
| 11 | +In the [prior exercise](./7-create-environment.md) you created the environment on Azure and an account for management, and configured the repository with the necessary information to interact with said environment. As a result, you only need to define a new workflow to deploy the application. Let's use the [Azure Container Apps action](https://github.com/marketplace/actions/azure-container-apps-build-and-deploy) from the marketplace to deploy the project. |
| 12 | + |
| 13 | +1. Return to your codespace, or reopen it by navigating to your repository and selecting **Code** > **Codespaces** and the name of your codespace. |
| 14 | +1. If the **Terminal** window isn't already open, open it by pressing <kbd>Ctl</kbd> - <kbd>`</kbd> on your keyboard. |
| 15 | +1. Switch to the `main` branch, pull any changes currently on the server to your codespace, and create a new branch by entering the following command in the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac): |
| 16 | + |
| 17 | + ```bash |
| 18 | + git checkout main |
| 19 | + git pull |
| 20 | + git checkout -b add-deploy-workflow |
| 21 | + ``` |
| 22 | + |
| 23 | +1. In the **Explorer** pane, open the **.github** > **workflows** folder. |
| 24 | +1. Create a new file in the **workflows** folder named **deploy-to-azure.yml**. |
| 25 | +1. Define the workflow by pasting the following code into **deploy-to-azure.yml**: |
| 26 | + |
| 27 | + ```yml |
| 28 | + name: Deploy to Azure |
| 29 | + on: |
| 30 | + push: |
| 31 | + branches: |
| 32 | + - main |
| 33 | +
|
| 34 | + jobs: |
| 35 | + deploy: |
| 36 | + runs-on: ubuntu-latest |
| 37 | + steps: |
| 38 | + - name: Checkout code |
| 39 | + uses: actions/checkout@v3 |
| 40 | +
|
| 41 | + - name: Log in to Azure |
| 42 | + uses: azure/login@v1 |
| 43 | + with: |
| 44 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 45 | +
|
| 46 | + - name: Build and deploy Container App |
| 47 | + uses: azure/container-apps-deploy-action@v1 |
| 48 | + with: |
| 49 | + appSourcePath: ${{ github.workspace }}/src |
| 50 | + acrName: ${{ secrets.AZURE_CONTAINER_REGISTRY }} |
| 51 | + resourceGroup: ${{ secrets.AZURE_RG }} |
| 52 | + containerAppName: ${{ secrets.AZURE_CONTAINER_APP }} |
| 53 | + containerAppEnvironment: ${{ secrets.AZURE_CONTAINER_APP_ENVIRONMENT }} |
| 54 | + ``` |
| 55 | + |
| 56 | + The workflow is set to run when code is pushed (or merged) into `main`. The deployment action is configured with the information it requires, using the naming defined in the creation workflow and the prefix you configured. |
| 57 | + |
| 58 | +1. Stage, commit and push all changes to the repository by entering the following command in the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac): |
| 59 | + |
| 60 | + ```bash |
| 61 | + git add . |
| 62 | + git commit -m "Created deployment workflow" |
| 63 | + git push -u origin add-deploy-workflow |
| 64 | + ``` |
| 65 | + |
| 66 | +1. Obtain the number for the issue you created for implementing automated deployment by entering the following command in the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac): |
| 67 | + |
| 68 | + ```bash |
| 69 | + gh issue list |
| 70 | + ``` |
| 71 | + |
| 72 | +1. Create a pull request (PR) for the newly created branch referencing the issue, replacing <ISSUE_NUMBER> with the issue you obtained in the prior step by entering the following command in the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac): |
| 73 | + |
| 74 | + ```bash |
| 75 | + gh pr create -t "Add deployment workflow" -b "Resolves #<ISSUE_NUMBER>" |
| 76 | + ``` |
| 77 | + |
| 78 | +1. Merge the PR you just created by entering the following command, replacing <PR_NUMBER> with the newly generated PR number, in the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac): |
| 79 | + |
| 80 | + ```bash |
| 81 | + gh pr merge <PR_NUMBER> |
| 82 | + ``` |
| 83 | + |
| 84 | + > **IMPORTANT:** Normally you would go through a standard review flow before merging a PR. Because we're working through a set of exercises as part of a workshop we're going to shortcut a couple of steps. |
| 85 | + |
| 86 | +And that's it! Your workflow will now run! |
| 87 | +
|
| 88 | +## Monitor the workflow and open the site |
| 89 | +
|
| 90 | +Because merging the workflow's [YML](https://en.wikipedia.org/wiki/YAML) into `main` counts as a push to `main`, the workflow automatically runs. Let's track the progress and navigate to the site! |
| 91 | +
|
| 92 | +> **IMPORTANT:** It may take a couple of minutes for the workflow to appear and for it to run. If the workflow doesn't appear on the Actions tab initially, wait a few seconds and refresh the page. |
| 93 | + |
| 94 | +1. Open your repository and select the **Actions** tab. |
| 95 | +1. Select **Deploy to Azure** to open the information about this workflow. |
| 96 | +1. Select the **Build and deploy Container App** step. Information about the deployment will be logged. |
| 97 | +1. When the workflow completes, all sections will automatically collapse. |
| 98 | +1. Reopen the **Build and deploy Container App** step. Note the URL displayed at the bottom, which will resemble the following: **some-url-here**. |
| 99 | +1. Navigate to the URL. Interact with your newly deployed website! |
| 100 | + |
| 101 | +## Summary and next steps! |
| 102 | + |
| 103 | +Congratulations!! You have now explored the core components to DevOps and how GitHub can support your development lifecycle. You started by creating a repository, then enabled settings to secure your code. You created issues to track your work, created an environment in which to code, and enabled testing for continuous integration. You modified code and explored the GitHub flow. And finally you deployed your application to the cloud. Using these skills, you can continue to build and grow your knowledge of DevOps. |
| 104 | + |
| 105 | +## Resources |
0 commit comments