-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
177 lines (161 loc) · 7.22 KB
/
action.yml
File metadata and controls
177 lines (161 loc) · 7.22 KB
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
name: 'Github Release and Slack Notification'
description: 'Sends Slack Notification for your GitHub Release with Changelog attached.'
inputs:
projectName:
description: 'Name of the project'
required: true
sendSlackNotification:
description: 'Send Slack Notification'
required: false
default: 'true'
slackChannel:
description: 'Slack Channel to send the notification'
required: false
default: 'production_releases'
sendRollbackStartedSlackNotification:
description: 'Send Rollback Started Slack Notification'
required: false
default: 'false'
sendRollbackCompletedSlackNotification:
description: 'Send Rollback Started Slack Notification'
required: false
default: 'false'
versionRollbackTo:
description: 'Version Rollback to'
required: false
permissions:
contents: read
id-token: write
runs:
using: "composite"
steps:
- name: Set new version name
if: ${{ inputs.sendRollbackStartedSlackNotification != 'true' && inputs.sendRollbackCompletedSlackNotification != 'true'}}
id: release_version
shell: pwsh
run: |
$branchName = $env:BranchName
$shortHash = "$env:SHA".Substring(0, 7)
$dateTime = Get-Date -Format "yyyy.MM.dd"
$versionName = "$branchName.$dateTime.$env:Rev.$shortHash"
Write-Host "Version Name: $versionName"
"version=$versionName" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
env:
BranchName: ${{ github.ref_name }}
Date: ${{ github.event.head_commit.timestamp }}
Rev: ${{ github.run_number }}
SHA: ${{ github.sha }}
- name: Create GitHub Release
if: ${{ inputs.sendRollbackStartedSlackNotification != 'true' && inputs.sendRollbackCompletedSlackNotification != 'true'}}
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.release_version.outputs.version }}
name: ${{ inputs.projectName }} ${{ steps.release_version.outputs.version }}
commit: ${{ github.sha }}
draft: false
prerelease: false
generateReleaseNotes: true
skipIfReleaseExists: true
makeLatest: true
- name: Assume AWS management account role
if: ${{ inputs.sendSlackNotification == 'true' && github.actor != 'renovate[bot]' }}
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::865076270365:role/github-ci-slack-webhook-url-read-role
aws-region: eu-west-1
role-session-name: "GitHubActions-DeployLayerStack"
unset-current-credentials: true
- name: Read SlackWebhookUrl AWS Secret value
if: ${{ inputs.sendSlackNotification == 'true' && github.actor != 'renovate[bot]' }}
id: slack_webhook_url
run: |
SLACK_WEBHOOK_URL=$(aws secretsmanager get-secret-value --secret-id arn:aws:secretsmanager:eu-west-1:865076270365:secret:SlackGithubActionsWebhookURL-vZcXfi --query SecretString --output text)
echo "::add-mask::$SLACK_WEBHOOK_URL"
echo "webhook_url=$SLACK_WEBHOOK_URL" >> $GITHUB_OUTPUT
shell: bash
- name: Sending notification to Slack
if: ${{ inputs.sendSlackNotification == 'true' && github.actor != 'renovate[bot]' }}
shell: pwsh
run: |
function Remove-Diacritics
{
Param(
[String]$inputString
)
return($sb = [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($inputString)))
}
$RollbackStarted = $env:RollbackStarted
$RollbackCompleted = $env:RollbackCompleted
$versionRollbackTo = $env:versionRollbackTo
if (($RollbackStarted -eq "true") -or ($RollbackCompleted -eq "true")) {
if ($null -eq $versionRollbackTo) {
Write-Error "versionRollbackTo to is not provided. Exiting..."
exit 1
}
$ghReleasesTags = gh release list --limit 15 --exclude-drafts --exclude-pre-releases --json tagName,isLatest --repo $env:repository | ConvertFrom-Json
$latestReleaseTag = $ghReleasesTags | Where-Object { $_.isLatest -eq $true } | Select-Object -ExpandProperty tagName
$previousReleaseTag = $versionRollbackTo
Write-Host "Latest Release Tag: $latestReleaseTag"
Write-Host "Previous Release Tag: $previousReleaseTag"
} else {
$body = gh release view $env:Version --json body --repo $env:repository | ConvertFrom-Json
$releaseNotes = $body.body
$releaseNotes = Remove-Diacritics -inputString $releaseNotes
}
if ($RollbackStarted -eq "true") {
if ($latestReleaseTag -eq $previousReleaseTag) {
$titleText = "$env:projectName ($latestReleaseTag) - Release In Progress"
$messageText = "Releasing the same version: " + $previousReleaseTag
} else {
$titleText = "$env:projectName ($latestReleaseTag) - Rollback In Progress"
$messageText = "Previous version: " + $previousReleaseTag
}
} elseif ($RollbackCompleted -eq "true") {
if ($latestReleaseTag -eq $previousReleaseTag) {
$titleText = "$env:projectName ($latestReleaseTag) - Release In Progress"
$messageText = "No changelist attached."
} else {
$titleText = "$env:projectName ($latestReleaseTag) - Rollback Completed"
$messageText = "Previous version: " + $previousReleaseTag
gh release edit --latest "$previousReleaseTag" --repo $env:repository
}
} else {
$titleText = "$env:projectName ($env:Version) - Successfully Released"
$parts = $releaseNotes -split "##"
if ($parts.count -lt 3) {
$messageText = $releaseNotes -replace "## What's Changed", ""
} else {
$WhatsChanged = $parts[1]
$messageText = $WhatsChanged -replace "What's Changed", ""
$messageText+= "`n"
$FullChangelogURL = $parts[2] -split "\n" | Select-Object -Last 1
$messageText+= $FullChangelogURL
}
$messageText = $messageText.Replace('**', '')
$messageText = $messageText.Replace('*', '-')
}
$payload = @{
channel = $env:SlackChannel
attachments = @(
@{
color = "#008000"
title = $titleText
title_link = "https://github.com/$env:repository/actions/runs/$env:runId"
text = $messageText
fallback = "Slack Notification"
}
)
}
$jsonPayload = $payload | ConvertTo-Json
Invoke-RestMethod -Uri $env:SlackWebhookUrl -Method Post -Body $jsonPayload -ContentType "application/json"
env:
GH_TOKEN: ${{ github.token }}
SlackWebhookUrl: ${{ steps.slack_webhook_url.outputs.webhook_url }}
SlackChannel: ${{ inputs.slackChannel }}
projectName: ${{ inputs.projectName }}
runId: ${{ github.run_id }}
repository: ${{ github.repository }}
RollbackStarted: ${{ inputs.sendRollbackStartedSlackNotification }}
RollbackCompleted: ${{ inputs.sendRollbackCompletedSlackNotification }}
Version: ${{ steps.release_version.outputs.version }}
versionRollbackTo: ${{ inputs.versionRollbackTo }}