Skip to content

Commit

Permalink
Add GitHub token to checkout step
Browse files Browse the repository at this point in the history
This commit adds `token` param to the checkout step.
The reason for that is to support pushing to GitHub protected branches within CI flow.

Updated checkout action to use 'v3'
  • Loading branch information
Matan Keidar committed May 9, 2022
1 parent 228964c commit 13d2272
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 22 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ jobs:
run: git config --global core.autocrlf false

- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down Expand Up @@ -95,9 +96,10 @@ jobs:
run: git config --global core.autocrlf false

- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/sbtghactions/GenerativeKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ trait GenerativeKeys {

lazy val githubWorkflowEnv = settingKey[Map[String, String]](s"A map of static environment variable assignments global to the workflow (default: { GITHUB_TOKEN: $${{ secrets.GITHUB_TOKEN }} })")
lazy val githubWorkflowAddedJobs = settingKey[Seq[WorkflowJob]]("A list of additional jobs to add to the CI workflow (default: [])")

lazy val githubWorkflowToken = settingKey[String]("A token for checkout step (default: $${{ secrets.GITHUB_TOKEN }} )")
}

object GenerativeKeys extends GenerativeKeys
7 changes: 5 additions & 2 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,10 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
githubWorkflowTargetPaths := Paths.None,

githubWorkflowEnv := Map("GITHUB_TOKEN" -> s"$${{ secrets.GITHUB_TOKEN }}"),
githubWorkflowAddedJobs := Seq())
githubWorkflowAddedJobs := Seq(),

githubWorkflowToken := s"$${{ secrets.GITHUB_TOKEN }}"
)

private lazy val internalTargetAggregation = settingKey[Seq[File]]("Aggregates target directories from all subprojects")

Expand Down Expand Up @@ -630,7 +633,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
}

autoCrlfOpt :::
List(WorkflowStep.CheckoutFull) :::
List(WorkflowStep.CheckoutFull(githubWorkflowToken.value)) :::
WorkflowStep.SetupJava(githubWorkflowJavaVersions.value.toList) :::
githubWorkflowGeneratedCacheSteps.value.toList
},
Expand Down
16 changes: 11 additions & 5 deletions src/main/scala/sbtghactions/WorkflowStep.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ sealed trait WorkflowStep extends Product with Serializable {

object WorkflowStep {

val CheckoutFull: WorkflowStep = Use(
UseRef.Public("actions", "checkout", "v2"),
name = Some("Checkout current branch (full)"),
params = Map("fetch-depth" -> "0"))
def CheckoutFull(token: String): WorkflowStep = {
Use(
UseRef.Public("actions", "checkout", "v3"),
name = Some("Checkout current branch (full)"),
params = Map(
"fetch-depth" -> "0",
"token" -> token
)
)
}

val Checkout: WorkflowStep = Use(UseRef.Public("actions", "checkout", "v2"), name = Some("Checkout current branch (fast)"))
val Checkout: WorkflowStep = Use(UseRef.Public("actions", "checkout", "v3"), name = Some("Checkout current branch (fast)"))

def SetupJava(versions: List[JavaSpec]): List[WorkflowStep] =
versions map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down Expand Up @@ -94,9 +95,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down
6 changes: 4 additions & 2 deletions src/sbt-test/sbtghactions/no-clean/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down Expand Up @@ -77,9 +78,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down Expand Up @@ -76,9 +77,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down Expand Up @@ -93,9 +94,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down Expand Up @@ -78,9 +79,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Java (temurin@11)
if: matrix.java == 'temurin@11'
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/sbtghactions/GenerativePluginSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class GenerativePluginSpec extends Specification {
- run: echo hello

- name: Checkout current branch (fast)
uses: actions/checkout@v2"""
uses: actions/checkout@v3"""
}

"compile a job with one step and three oses" in {
Expand Down Expand Up @@ -639,7 +639,7 @@ class GenerativePluginSpec extends Specification {
- run: echo $${{ matrix.test }}

- name: Checkout current branch (fast)
uses: actions/checkout@v2"""
uses: actions/checkout@v3"""
}

"compile a job with extra runs-on labels" in {
Expand Down Expand Up @@ -838,7 +838,7 @@ class GenerativePluginSpec extends Specification {
- run: echo hello

- name: Checkout current branch (fast)
uses: actions/checkout@v2"""
uses: actions/checkout@v3"""
}
}

Expand Down

0 comments on commit 13d2272

Please sign in to comment.