Skip to content

Latest commit

 

History

History
227 lines (167 loc) · 7.11 KB

how-to-deploy-github-action.md

File metadata and controls

227 lines (167 loc) · 7.11 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic ms.custom
Quickstart: Connect with GitHub Actions
Use Azure Database for PostgreSQL - Flexible Server from a GitHub Actions workflow.
nachoalonsoportillo
ialonso
maghan
05/21/2024
azure-database-postgresql
flexible-server
quickstart
github-actions-azure
mode-other
devx-track-azurecli

Quickstart: Use GitHub Actions to connect to Azure Database for PostgreSQL - Flexible Server

[!INCLUDE applies-to-postgresql-flexible-server]

Get started with GitHub Actions by using a workflow to deploy database updates to Azure Database for PostgreSQL flexible server.

Prerequisites

You need:

Workflow file overview

A GitHub Actions workflow is defined by a YAML (.yml) file in the /.github/workflows/ path in your repository. This definition contains the various steps and parameters that make up the workflow.

The file has two sections:

Section Tasks
Authentication 1. Generate deployment credentials.
Deploy 1. Deploy the database.

Generate deployment credentials

[!INCLUDE include]

Copy the Azure Database for PostgreSQL flexible server connection string

In the Azure portal, go to your Azure Database for PostgreSQL flexible server instance and from the resource menu, under Settings, select Connect. In that page, use the Database name combo box to select the name of the database you want to connect to. Expand the Connect from your app section, and copy ADO.NET connection string, and replace the placeholder value {your_password} with your actual password. The connection string looks similar to this.

Server={servername.postgres.database.azure.com};Database={your_database};Port=5432;User Id={adminusername};Password={your_password};Ssl Mode=Require;

You use the connection string as a GitHub secret.

Configure the GitHub secrets

[!INCLUDE include]

Add your workflow

  1. Go to Actions for your GitHub repository.

  2. Select Set up your workflow yourself.

  3. Delete everything after the on: section of your workflow file. For example, your remaining workflow may look like this.

    name: CI
    
    on:
    push:
        branches: [ main ]
    pull_request:
        branches: [ main ]
  4. Rename your workflow PostgreSQL for GitHub Actions and add the checkout and sign in actions. These actions check out your site code and authenticate with Azure using the GitHub secret(s) you created earlier.

    name: PostgreSQL for GitHub Actions
    
    on:
    push:
        branches: [ main ]
    pull_request:
        branches: [ main ]
    
    jobs:
    build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v1
        - uses: azure/login@v1
        with:
            creds: ${{ secrets.AZURE_CREDENTIALS }}
    name: PostgreSQL for GitHub Actions
    
    on:
    push:
        branches: [ main ]
    pull_request:
        branches: [ main ]
    
    jobs:
    build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v1
        - uses: azure/login@v1
        with:
            client-id: ${{ secrets.AZURE_CLIENT_ID }}
            tenant-id: ${{ secrets.AZURE_TENANT_ID }}
            subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

  5. Use the Azure PostgreSQL Deploy action to connect to your Azure Database for PostgreSQL flexible server instance. Replace POSTGRESQL_SERVER_NAME with the name of your server. You should have an Azure Database for PostgreSQL flexible server data file named data.sql at the root level of your repository.

     - uses: azure/postgresql@v1
       with:
        connection-string: ${{ secrets.AZURE_POSTGRESQL_CONNECTION_STRING }}
        server-name: POSTGRESQL_SERVER_NAME
        plsql-file: './data.sql'
  6. Complete your workflow by adding an action to sign out of Azure. Here's the completed workflow. The file appears in the .github/workflows folder of your repository.

    name: PostgreSQL for GitHub Actions
    
    on:
    push:
        branches: [ main ]
    pull_request:
        branches: [ main ]
    
    
    jobs:
    build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v1
        - uses: azure/login@v1
        with:
            client-id: ${{ secrets.AZURE_CREDENTIALS }}
    
    - uses: azure/postgresql@v1
      with:
        server-name: POSTGRESQL_SERVER_NAME
        connection-string: ${{ secrets.AZURE_POSTGRESQL_CONNECTION_STRING }}
        plsql-file: './data.sql'
    
        # Azure logout
    - name: logout
      run: |
         az logout
    name: PostgreSQL for GitHub Actions
    
    on:
    push:
        branches: [ main ]
    pull_request:
        branches: [ main ]
    
    
    jobs:
    build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v1
        - uses: azure/login@v1
        with:
            client-id: ${{ secrets.AZURE_CLIENT_ID }}
            tenant-id: ${{ secrets.AZURE_TENANT_ID }}
            subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
    
    - uses: azure/postgresql@v1
      with:
        server-name: POSTGRESQL_SERVER_NAME
        connection-string: ${{ secrets.AZURE_POSTGRESQL_CONNECTION_STRING }}
        plsql-file: './data.sql'
    
        # Azure logout
    - name: logout
      run: |
         az logout

Review your deployment

  1. Go to Actions for your GitHub repository.

  2. Open the first result to see detailed logs of your workflow's run.

    :::image type="content" source="media/how-to-deploy-github-action/gitbub-action-postgres-success.png" alt-text="Log of GitHub Actions run." lightbox="media/how-to-deploy-github-action/gitbub-action-postgres-success.png":::

Clean up resources

When your Azure Database for PostgreSQL flexible server database and repository are no longer needed, clean up the resources you deployed by deleting the resource group and your GitHub repository.

Next steps

[!div class="nextstepaction"] Learn about Azure and GitHub integration


> [!div class="nextstepaction"] > [Learn how to connect to the server](connect-csharp.md)