Skip to content

Commit

Permalink
Merge pull request #37 from deadlydog/HaveCiTriggerCd
Browse files Browse the repository at this point in the history
ci: Have build explicitly trigger the deploy to prevent empty deployment runs
  • Loading branch information
deadlydog authored Oct 2, 2023
2 parents af7abc4 + 7531bb6 commit 2c18099
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/build-and-test-powershell-module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ on:
required: false
type: string
default: ''
deploy:
description: 'Deploy the build artifacts. Only has effect when not building the main branch.'
required: false
type: boolean
default: false

env:
powerShellModuleName: 'tiPS' # Must match the name in the deployment workflow.
Expand Down Expand Up @@ -261,3 +266,10 @@ jobs:
with:
name: ${{ env.deployFilesArtifactName }}
path: ${{ env.deployFilesArtifactDirectoryPath }}

trigger-deployment:
needs: build-and-test
# Only trigger a deployment if this build is for a push (not a PR) and is for the default branch (main).
if: inputs.deploy || (github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch))
uses: ./.github/workflows/deploy-powershell-module.yml
secrets: inherit
44 changes: 9 additions & 35 deletions .github/workflows/deploy-powershell-module.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
name: deploy

on:
workflow_run:
workflows: [build]
types: [completed]
branches: [main]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
workflowRunId:
description: 'The build workflow run ID containing the artifacts to use. The run ID can be found in the URL of the build workflow run.'
type: number
required: true
workflow_call:

env:
powerShellModuleName: 'tiPS' # Must match the name in the build workflow.
prereleaseModuleArtifactName: 'PrereleaseModuleArtifact' # Must match the name in the build workflow.
stableModuleArtifactName: 'StableModuleArtifact' # Must match the name in the build workflow.
deployFilesArtifactName: 'DeployFilesArtifact' # Must match the name in the build workflow.
artifactsDirectoryPath: './artifacts'
workflowRunId: ${{ github.event_name == 'workflow_dispatch' && inputs.workflowRunId || github.event.workflow_run.id }} # Ternary operator to use input value if manually triggered, otherwise use the workflow_run.id value.

jobs:
publish-prerelease-module:
# Only run the deployment if manually triggered, or the build workflow succeeded.
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
outputs:
prereleaseVersionNumber: ${{ steps.output-version-number.outputs.prereleaseVersionNumber }}
steps:
- name: Download prerelease module artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
- name: Download prerelease module artifact
uses: actions/download-artifact@v3
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.prereleaseModuleArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true

- name: Publish prerelease PowerShell module
shell: pwsh
Expand Down Expand Up @@ -82,13 +66,11 @@ jobs:
Write-Output "Installing the module '$moduleName' prerelease version '$prereleaseVersionNumber' from the PowerShell Gallery."
Install-Module -Name $moduleName -AllowPrerelease -RequiredVersion $prereleaseVersionNumber -Force -Scope CurrentUser -Repository PSGallery -ErrorAction Stop -Verbose
- name: Download deploy files artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
- name: Download deploy files artifact
uses: actions/download-artifact@v3
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.deployFilesArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true

- name: Run smoke tests
shell: pwsh
Expand Down Expand Up @@ -126,12 +108,10 @@ jobs:
Install-Module -Name $moduleName -AllowPrerelease -RequiredVersion $prereleaseVersionNumber -Force -Scope CurrentUser -Repository PSGallery -ErrorAction Stop -Verbose
- name: Download deploy files artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
uses: actions/download-artifact@v3
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.deployFilesArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true

- name: Run smoke tests
shell: powershell
Expand Down Expand Up @@ -159,12 +139,10 @@ jobs:
stableVersionNumber: ${{ steps.output-version-number.outputs.StableVersionNumber }}
steps:
- name: Download stable module artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
uses: actions/download-artifact@v3
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.stableModuleArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true

- name: Publish stable PowerShell module
shell: pwsh
Expand Down Expand Up @@ -209,12 +187,10 @@ jobs:
Install-Module -Name $moduleName -RequiredVersion $stableVersionNumber -Force -Scope CurrentUser -Repository PSGallery -ErrorAction Stop -Verbose
- name: Download deploy files artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
uses: actions/download-artifact@v3
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.deployFilesArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true

- name: Run smoke tests
shell: pwsh
Expand Down Expand Up @@ -252,12 +228,10 @@ jobs:
Install-Module -Name $moduleName -RequiredVersion $stableVersionNumber -Force -Scope CurrentUser -Repository PSGallery -ErrorAction Stop -Verbose
- name: Download deploy files artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
uses: actions/download-artifact@v3
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.deployFilesArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true

- name: Run smoke tests
shell: powershell
Expand Down

0 comments on commit 2c18099

Please sign in to comment.