Skip to content

Deploy to environment #698

Deploy to environment

Deploy to environment #698

name: Deploy to environment
on:
push:
branches:
- main
workflow_dispatch:
inputs:
environment:
type: environment
description: "Choose an environment to deploy to"
required: true
concurrency:
group: ${{ github.workflow }}-${{ github.event.inputs.environment }}
env:
DOCKER_IMAGE: complete-app
AZ_CLI_VER: 2.45.0
jobs:
set-env:
name: Determine environment
runs-on: ubuntu-22.04
outputs:
environment: ${{ steps.var.outputs.environment }}
branch: ${{ steps.var.outputs.branch }}
checked-out-sha: ${{ steps.var.outputs.checked-out-sha }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- id: var
run: |
GIT_REF=${{ github.ref }}
GIT_BRANCH=${GIT_REF##*/}
INPUT=${{ github.event.inputs.environment }}
ENVIRONMENT=${INPUT:-"development"}
CHECKED_OUT_SHA="$(git log -1 '--format=format:%H')"
TIME="$(date)"
echo "environment=${ENVIRONMENT,,}" >> $GITHUB_OUTPUT
echo "branch=$GIT_BRANCH" >> $GITHUB_OUTPUT
echo "checked-out-sha=${CHECKED_OUT_SHA}" >> $GITHUB_OUTPUT
echo "date=${TIME}" >> $GITHUB_OUTPUT
build-and-push-image:
name: Build and push to ACR
needs: set-env
runs-on: ubuntu-22.04
environment: ${{ needs.set-env.outputs.environment }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Azure Container Registry login
uses: docker/login-action@v3
with:
username: ${{ secrets.AZURE_ACR_CLIENTID }}
password: ${{ secrets.AZURE_ACR_SECRET }}
registry: ${{ secrets.AZURE_ACR_URL }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
build-args: |
RAILS_ENV=development
CURRENT_GIT_SHA="${{ needs.set-env.outputs.checked-out-sha }}"
TIME_OF_BUILD="${{ needs.set-env.outputs.date }}"
tags: |
${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:${{ needs.set-env.outputs.branch }}
${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:sha-${{ needs.set-env.outputs.checked-out-sha }}
${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:latest
push: true
cache-from: type=gha
deploy-image:
name: Deploy '${{ needs.set-env.outputs.branch }}' to ${{ needs.set-env.outputs.environment }}
needs: [build-and-push-image, set-env]
runs-on: ubuntu-22.04
environment: ${{ needs.set-env.outputs.environment }}
steps:
- name: Azure login with ACA credentials
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_ACA_CREDENTIALS }}
- name: Update Azure Container Apps Revision
uses: azure/CLI@v2
with:
azcliversion: ${{ env.AZ_CLI_VER }}
inlineScript: |
az config set extension.use_dynamic_install=yes_without_prompt
az containerapp update \
--name ${{ secrets.AZURE_ACA_NAME }} \
--resource-group ${{ secrets.AZURE_ACA_RESOURCE_GROUP }} \
--image ${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:sha-${{ needs.set-env.outputs.checked-out-sha }} \
--output none
- name: Azure login with ACA Worker credentials
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_ACA_WORKER_CREDENTIALS }}
- name: Update Azure Container Apps Worker Revision
uses: azure/CLI@v2
with:
azcliversion: ${{ env.AZ_CLI_VER }}
inlineScript: |
az config set extension.use_dynamic_install=yes_without_prompt
az containerapp update \
--name ${{ secrets.AZURE_ACA_NAME }}-worker \
--resource-group ${{ secrets.AZURE_ACA_RESOURCE_GROUP }} \
--image ${{ secrets.AZURE_ACR_URL }}/${{ env.DOCKER_IMAGE }}:sha-${{ needs.set-env.outputs.checked-out-sha }} \
--output none