From b51c2bfbb643667cdf4ee0a1d805ad3ab114488c Mon Sep 17 00:00:00 2001 From: Heikki-Jussi Niemi Date: Thu, 11 Jul 2024 23:15:47 +0300 Subject: [PATCH] Use workflow_run for deployment (#19) * Use artifact instead of release for automation --- .github/workflows/deploy-release.yml | 48 +++++++++++++++++++--------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/.github/workflows/deploy-release.yml b/.github/workflows/deploy-release.yml index 5dd6e76..458f98a 100644 --- a/.github/workflows/deploy-release.yml +++ b/.github/workflows/deploy-release.yml @@ -4,12 +4,14 @@ name: Production deployment on: workflow_dispatch: inputs: - tag: - description: 'Tag to deploy' - default: 'latest' + run_id: + description: 'Workflow run ID' - release: - types: [released] + workflow_run: + workflows: [Build and Test code] + types: + - completed + branches: [main] permissions: id-token: write @@ -24,16 +26,34 @@ jobs: env: RESOURCE_GROUP: ${{ secrets.PRODUCTION_RESOURCE_GROUP }} APP_NAME: ${{ secrets.PRODUCTION_APP_NAME }} - TAG: ${{ github.event_name != 'release' && 'latest' || format('tags/{0}', github.event.release.tag_name) }} + RUN_ID: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.id || inputs.run_id }} steps: - - - name: Fetch release asset - uses: dsaltares/fetch-gh-release-asset@master + - uses: actions/checkout@v4 + + - name: 'Download artifact' + uses: actions/github-script@v7 with: - repo: ${{ github.repository }} - version: ${{ env.TAG }} - file: 'publish.zip' + script: | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: process.env.RUN_ID + }); + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { + return artifact.name == "DiscordImageSender" + })[0]; + let download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + let fs = require('fs'); + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/DiscordImageSender.zip`, Buffer.from(download.data)); + + - name: 'Unzip artifact' + run: unzip DiscordImageSender.zip - name: Login via Az module uses: azure/login@v2 @@ -42,12 +62,10 @@ jobs: tenant-id: ${{ secrets.AZURE_TENANT_ID }} subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} enable-AzPSSession: true - - - uses: actions/checkout@v4 - name: Deploy binaries uses: azure/powershell@v2 with: azPSVersion: "latest" inlineScript: | - ./scripts/Deploy-FunctionApp.ps1 -ResourceGroup $env:RESOURCE_GROUP -AppName $env:APP_NAME -ZipFile publish.zip + ./scripts/Deploy-FunctionApp.ps1 -ResourceGroup $env:RESOURCE_GROUP -AppName $env:APP_NAME -ZipFile "$($env:GITHUB_WORKSPACE)/publish.zip"