-
Notifications
You must be signed in to change notification settings - Fork 6
199 lines (178 loc) · 9.02 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
name: Build and Publish Module
on:
push: # Only trigger the workflow if there is a push
branches: [
main, # This will build as well as release (Ideally this should only happen as soon as a PR from develop branch is merged.)
develop, # This will only build but not release - see the if condition inside the "release" job below.
]
workflow_dispatch: #Enables the possibility to trigger the workflow manually from GitHub Actions in the repository
jobs:
# Configure skip-duplicate actions
pre_job:
# continue-on-error: true # Uncomment once integration is finished
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@master
with:
# All of these options are optional, so you can remove them if you are happy with the defaults
concurrent_skipping: "never"
skip_after_successful_duplicate: "false"
paths_ignore: '["**.md", ".gitignore", ".prettierrc", "**/Docs/**", "assets/**", "**.yml"]'
do_not_skip: '["workflow_dispatch", "schedule"]'
# 1st Job -- Building the module, skip job if should_skip is true
build:
needs: pre_job
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
runs-on: ubuntu-latest
steps:
# Checkout the main branch
- uses: actions/checkout@main
# Setting up required powershell modules
- name: 🔧 Set required PowerShell modules
id: psmodulecache
uses: potatoqualitee/psmodulecache@v1
with:
modules-to-cache: Pester, PSScriptAnalyzer, InvokeBuild, platyPS, PSReadLine
# Setting up the powershell module cache
- name: 🔧 Setup PowerShell module cache
id: cacher
uses: actions/cache@v2
with:
path: ${{ steps.psmodulecache.outputs.modulepath }}
key: ${{ steps.psmodulecache.outputs.keygen }}
# Installing the required powershell module, if not cached
- name: 🔧 Install required PowerShell modules
if: steps.cacher.outputs.cache-hit != 'true'
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module ${{ steps.psmodulecache.outputs.needed }} -ErrorAction Stop
#! Hardcoded module name here and stored as env variable, used by this job only. This step is duplicated in the next job.
# Running a powershell command to save the module name as an Environment Variable
- name: 🔧 Set Module Name
id: set_module_name
shell: pwsh
run: |
Write-Host $Env:GITHUB_REF
$ModuleName="PoshCodex"
echo "MODULE_NAME=$ModuleName" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
# Running the InvokeBuild Module Invoke-Build command
# WITH BUMP VERSION, FOR BOTH BRANCHES.
# Version is bumped regardless of branch, but publish to gallery will only happen if the commit reaches the main branch.
# And if it does reach the main branch, it means builds have passed anyway.
# So this workflow should be fine.
- name: ⚒️ Invoke Build, bump versions and set env.MODULE_VERSION variable
shell: pwsh
run: |
Invoke-Build -File ./${{ env.MODULE_NAME }}/build.ps1 -BumpVersion -Verbose
$moduleversion=(Test-ModuleManifest -Path ".\${{ env.MODULE_NAME }}\Source\${{ env.MODULE_NAME }}.psd1").Version.toString()
echo "Setting Module version environment variable: $moduleversion"
echo "MODULE_VERSION=$moduleversion" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
echo "Bumping Scoop manifest version to $moduleversion"
(Get-Content .\poshcodex.json) -replace '"version": "([0-9.]{5,})"', "`"version`": `"$moduleversion`"" | Out-File .\poshcodex.json
# Pushing the changes from InvokeBuild to the main branch
# DON'T COMMIT THE VERSION BUMP IF IT'S DEVELOP BRANCH.
- name: 📌 Push changes to Git Repository
if: github.ref == 'refs/heads/main'
run: |
git config --global user.name 'rishi255'
git config --global user.email 'rishikeshrachchh@gmail.com'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
git add .
if [ $(git status --porcelain | wc -l) -ge 0 ]; then
echo "Working tree not clean! Displaying git status:"
git status
echo "Committing new version bump to v${{ env.MODULE_VERSION }}!"
git commit -am "💎 Bump to v${{ env.MODULE_VERSION }}"
git push
fi
echo "Getting module number from GitHub env variables: "
echo "MODULE_VERSION = ${{ env.MODULE_VERSION }}"
# DON'T PUSH THE TAG IF IT'S DEVELOP BRANCH.
- name: 🔖 Tag version
id: tag
if: github.ref == 'refs/heads/main'
uses: anothrNick/github-tag-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CUSTOM_TAG: v${{ env.MODULE_VERSION }}
- name: ⚙️ Check created tag
id: check_tagged
run: |
echo "Github ref: ${{ github.ref }}, New tag: '${{ steps.tag.outputs.new_tag }}', tag: '${{ steps.tag.outputs.tag }}'"
echo "::set-output name=tagged::${{ (steps.tag.outputs.new_tag == steps.tag.outputs.tag) }}"
echo "tagged = ${{ (steps.tag.outputs.new_tag == steps.tag.outputs.tag) }}"
tag_with_v=${{ steps.tag.outputs.new_tag }}
echo "::set-output name=tag_without_v::${tag_with_v:1}"
echo "Tag without v: ${tag_with_v:1}"
# Uploads the build powershell module as an artifact
- name: 📌 Upload Build Artifact
uses: actions/upload-artifact@v4
with:
name: module-artifact # Naming the powershell module artifact
path: ./${{ env.MODULE_NAME }}/Output/ # Saving the powershell module artifact to the path ./PoshCodex/Output/
- name: debug
working-directory: ./${{ env.MODULE_NAME }}/Output/
run: |
echo "Current directory:"
pwd
echo "Stuff in curr dir:"
ls
echo "Stuff in 2 directories up:"
ls ../..
outputs:
tagged: ${{ steps.check_tagged.outputs.tagged }}
tag_without_v: ${{ steps.check_tagged.outputs.tag_without_v }}
tag: ${{ steps.tag.outputs.tag }}
# 2nd Job -- Releasing the module, skip job if should_skip is true
release:
needs: [pre_job, build]
# Only run the "release" job if current branch is main, AND should_skip is NOT 'true'
# This will break if 'pull_request' is added to the workflow triggers, since PRs don't have a current branch before they are merged.
if: github.ref == 'refs/heads/main' && needs.pre_job.outputs.should_skip != 'true'
runs-on: ubuntu-latest
steps:
# Check out the main branch
- uses: actions/checkout@main
# Downloads the powershell module build artifact made in the build step
- name: 📩 Download build artifact
uses: actions/download-artifact@v4
with:
name: module-artifact # Name of the powershell module artifact
path: ./Artifact/ # Downloads the module to the path ./Artifact/
#! Hardcoded module name here as can't use env variable from previous job without complicating code
# Running a powershell command to save the module name as an Environment Variable
- name: 🔧 Set Module Name
run: |
Write-Host $Env:GITHUB_REF
$ModuleName="PoshCodex"
echo "MODULE_NAME=$ModuleName" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
echo "needs.build.outputs.tagged = ${{ needs.build.outputs.tagged }}"
shell: pwsh
# Publishing the module to powershell gallery
- name: ⬆️ Publish to Powershell Gallery
uses: pcgeek86/publish-powershell-module-action@v20
with:
modulePath: ./Artifact/${{ env.MODULE_NAME }} # Using the environment variable to find the module name
NuGetApiKey: ${{ secrets.NUGETAPIKEY }} # Using the NugetAPI key set in GitHub Secrets
- name: ⛓️ Make tarball and zipball for GitHub Release
working-directory: ./Artifact/${{ env.MODULE_NAME }}/${{ needs.build.outputs.tag_without_v }}
run: |
tar -cvf PoshCodex.tar .
zip -r PoshCodex.zip . -x *.tar
mv PoshCodex.tar ../../../PoshCodex.tar
mv PoshCodex.zip ../../../PoshCodex.zip
# Since this only runs on the main branch, it means that a tag was created right after commit in the build step.
# That tag is published as a release on GitHub.
# Release name defaults to tag name.
- name: 🛎️ Create GitHub Release
if: ${{ needs.build.outputs.tagged == 'true' }}
uses: ncipollo/release-action@v1
with:
artifacts: "PoshCodex.zip,PoshCodex.tar"
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ needs.build.outputs.tag }}