Skip to content

Commit

Permalink
Add Netlify prod deployment on develop
Browse files Browse the repository at this point in the history
  • Loading branch information
florentmaitre committed Sep 19, 2024
1 parent 402e593 commit 62b994b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: build-docs

on:
# Runs on pushes targeting the default branch
push:
branches: [ "main" ]
branches:
- main
- develop

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

pull_request:
Expand Down Expand Up @@ -91,6 +91,7 @@ jobs:
netlify:
needs: [ build, test ]
runs-on: ubuntu-latest
if: github.event_name != 'push' || github.ref != 'refs/heads/master'
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
79 changes: 43 additions & 36 deletions buildSrc/src/main/kotlin/netlify.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* Software description: Android library of reusable graphical components
*/

import com.orange.ouds.gradle.Environment
import com.orange.ouds.gradle.execute
import com.orange.ouds.gradle.gitHubApi
import com.orange.ouds.gradle.requireTypedProperty
Expand All @@ -26,48 +25,56 @@ tasks.register<DefaultTask>("publishDocumentationToNetlify") {
val netlifyToken = requireTypedProperty<String>("netlifyToken")
val commitSha = requireTypedProperty<String>("commitSha")

// GITHUB_HEAD_REF is equal to the branch name for a pull request and is empty otherwise
// GITHUB_REF_NAME is equal to X/merge for a pull request (where X is the pull request number) or to the branch name otherwise
// That's why we use GITHUB_HEAD_REF for a pull request and GITHUB_REF_NAME otherwise
val branchName = "develop"//Environment.getVariablesOrNull("GITHUB_HEAD_REF", "GITHUB_REF_NAME").firstOrNull { it?.isNotBlank() == true }

val args = mutableListOf("deploy", "--dir", documentationPath, "--site", netlifySiteId, "--auth", netlifyToken)
if (branchName == "develop") {
args += "--prod"
}

val output = try {
execute("netlify", "deploy", "--dir", documentationPath, "--site", netlifySiteId, "--auth", netlifyToken)
execute("netlify", *args.toTypedArray())
} catch (exception: ExecException) {
null
}

gitHubApi {
// GITHUB_HEAD_REF is equal to the branch name for a pull request and is empty otherwise
// GITHUB_REF_NAME is equal to X/merge for a pull request (where X is the pull request number) or to the branch name otherwise
// That's why we use GITHUB_HEAD_REF for a pull request and GITHUB_REF_NAME otherwise
val branchName = Environment.getVariablesOrNull("GITHUB_HEAD_REF", "GITHUB_REF_NAME").firstOrNull { it?.isNotBlank() == true }
// Find pull request for current branch
val pullRequests = getPullRequests()
val pullRequest = pullRequests.firstOrNull { it.branchName == branchName }
if (pullRequest != null) {
println("Found pull request #${pullRequest.number} for branch $branchName.")
val body = "$netlifyCommentPreamble\\n" + if (output != null) {
// The regex below removes colors from output
val outputLines = output.replace("\\x1B\\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]".toRegex(), "").split("\n")
val netlifyDeployPreviewUrl = outputLines.firstNotNullOfOrNull { "^Website draft URL:\\s+(.*)$".toRegex().find(it) }
?.groupValues
?.getOrNull(1)
val netlifyDeployLogUrl = outputLines.firstNotNullOfOrNull { "^Build logs:\\s+(.*)$".toRegex().find(it) }
?.groupValues
?.getOrNull(1)
"### 🟢 Netlify deploy preview for commit $commitSha succeeded\\n\\nDeploy preview: $netlifyDeployPreviewUrl\\nDeploy log: $netlifyDeployLogUrl"
} else {
"### 🔴 Netlify deploy preview for commit $commitSha failed"
}
// Although we use the "issues/{issue_number}/comments" GitHub API, this will comment the pull request because a pull request is an issue
// The "pulls/{pull_number}/comments" is used to add review comments on a pull request
val issueComments = getIssueComments(pullRequest.number)
val netlifyComment = issueComments.firstOrNull { it.body.startsWith(netlifyCommentPreamble) }
if (netlifyComment != null) {
println("Update comment with Netlify deploy info to '${pullRequest.title} (#${pullRequest.number})'.")
updateIssueComment(netlifyComment.id, body)
if (branchName != "develop") {
gitHubApi {
// Find pull request for current branch
val pullRequests = getPullRequests()
val pullRequest = pullRequests.firstOrNull { it.branchName == branchName }
if (pullRequest != null) {
println("Found pull request #${pullRequest.number} for branch $branchName.")
val body = "$netlifyCommentPreamble\\n" + if (output != null) {
// The regex below removes colors from output
val outputLines = output.replace("\\x1B\\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]".toRegex(), "").split("\n")
val netlifyDeployPreviewUrl = outputLines.firstNotNullOfOrNull { "^Website draft URL:\\s+(.*)$".toRegex().find(it) }
?.groupValues
?.getOrNull(1)
val netlifyDeployLogUrl = outputLines.firstNotNullOfOrNull { "^Build logs:\\s+(.*)$".toRegex().find(it) }
?.groupValues
?.getOrNull(1)
"### 🟢 Netlify deploy preview for commit $commitSha succeeded\\n\\nDeploy preview: $netlifyDeployPreviewUrl\\nDeploy log: $netlifyDeployLogUrl"
} else {
"### 🔴 Netlify deploy preview for commit $commitSha failed"
}
// Although we use the "issues/{issue_number}/comments" GitHub API, this will comment the pull request because a pull request is an issue
// The "pulls/{pull_number}/comments" is used to add review comments on a pull request
val issueComments = getIssueComments(pullRequest.number)
val netlifyComment = issueComments.firstOrNull { it.body.startsWith(netlifyCommentPreamble) }
if (netlifyComment != null) {
println("Update comment with Netlify deploy info to '${pullRequest.title} (#${pullRequest.number})'.")
updateIssueComment(netlifyComment.id, body)
} else {
println("Create comment with Netlify deploy info to '${pullRequest.title} (#${pullRequest.number})'.")
createIssueComment(pullRequest.number, body)
}
} else {
println("Create comment with Netlify deploy info to '${pullRequest.title} (#${pullRequest.number})'.")
createIssueComment(pullRequest.number, body)
throw GradleException("Could not find a pull request for branch $branchName.")
}
} else {
throw GradleException("Could not find a pull request for branch $branchName.")
}
}
}
Expand Down

0 comments on commit 62b994b

Please sign in to comment.