Skip to content

Workflow Examples

Cyril Rohr edited this page Jul 7, 2023 · 12 revisions

Workflow file with master branch always on

# .github/workflows/pullpreview.yml
name: PullPreview
on:
  push:
    branches: [master]
  pull_request:
    types: [labeled, unlabeled, synchronize, closed, reopened]

concurrency: ${{ github.ref }}

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    timeout-minutes: 30
    permissions:
      contents: read # to fetch code (actions/checkout)
      deployments: write # to delete deployments
      pull-requests: write # to remove labels
      statuses: write # to create commit status
    steps:
    - uses: actions/checkout@v2
    - uses: pullpreview/action@v5
      with:
        # Those GitHub users will have SSH access to the servers
        admins: crohr,other-github-user
        # A staging environment will always exist for the master branch
        always_on: master
        # Use the cidrs option to restrict access to the live environments to specific IP ranges
        cidrs: "0.0.0.0/0"
        # PullPreview will use those 2 files when running docker-compose up
        compose_files: docker-compose.yml,docker-compose.staging.yml
        # The preview URL will target this port
        default_port: 80
        # Use a 512MB RAM instance type instead of the default 2GB
        instance_type: nano_2_0
        # Ports to open on the server
        ports: 80,5432
      env:
        AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}"
        AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}"
        AWS_REGION: "us-east-1"

Workflow file with .env generated from GitHub Secrets

This example demonstrates how to use GitHub Secrets to generate a custom .env file for use in your docker-compose YAML file:

# .github/workflows/pullpreview.yml
name: PullPreview
on:
  pull_request:
    types: [labeled, unlabeled, synchronize, closed, reopened]

concurrency: ${{ github.ref }}

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    timeout-minutes: 30
    permissions:
      contents: read # to fetch code (actions/checkout)
      deployments: write # to delete deployments
      pull-requests: write # to remove labels
      statuses: write # to create commit status
    steps:
    - uses: actions/checkout@v2
    - name: Generate env file
      env:
        SECRET1: ${{ secrets.SECRET1 }}
        SECRET2: ${{ secrets.SECRET2 }}
      run: |
        echo "VALUE1=$SECRET1" >> .env
        echo "VALUE2=$SECRET2" >> .env
    - uses: pullpreview/action@v5
      env:
        AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}"
        AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}"

Workflow file with multiple Compose files

Here we specify 2 compose files to be used, to take advantage of the Compose extension mechanism (cf. official Docker documentation). The docker-compose.staging.yml file could contain additional environment variables or different port mappings for instance.

# .github/workflows/pullpreview.yml
name: PullPreview
on:
  pull_request:
    types: [labeled, unlabeled, synchronize, closed, reopened]

concurrency: ${{ github.ref }}

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    timeout-minutes: 30
    permissions:
      contents: read # to fetch code (actions/checkout)
      deployments: write # to delete deployments
      pull-requests: write # to remove labels
      statuses: write # to create commit status
    steps:
    - uses: actions/checkout@v2
    - uses: pullpreview/action@v5
      with:
        compose_files: "docker-compose.yml,docker-compose.staging.yml"
      env:
        AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}"
        AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}"