Skip to content

Commit

Permalink
scripts: move snap release from GitHubCI to GitLabCI
Browse files Browse the repository at this point in the history
The reason to use SNAPCRAFT_LOGIN_FILE instead
of SNAPCRAFT_LOGIN (like in Github) is in the docs:

From https://docs.gitlab.com/ee/ci/variables/#cicd-variable-types :

...
File type variables:

Consist of a key, value and file.
Are made available in jobs as environment variables, with
The CI/CD variable key as the environment variable name.
The CI/CD variable value saved to a temporary file.
The path to the temporary file as the environment variable value.
...

And the reason for using "needs:" instead of "dependencies:"
is because the job depends on artifacts of a job in the same
stage. More info: https://gitlab.com/gitlab-org/gitlab/-/issues/30632

Co-authored-by: Andres G. Aragoneses <[email protected]>
  • Loading branch information
aarani and knocte committed Nov 25, 2021
1 parent 0aeb1ad commit 8b87b15
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 31 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ jobs:
sudo apt update
./scripts/install_snapcraft.sh
./scripts/snap_build.sh
- name: Upload snap package to Snap Store
env:
SNAPCRAFT_LOGIN: ${{ secrets.SNAPCRAFT_LOGIN }}
run: |
sudo apt update
sudo apt install --yes fsharp
./scripts/snap_release.sh
# this step below is commented because we now upload the snap in GitLab:
# - name: Upload snap package to Snap Store
# env:
# SNAPCRAFT_LOGIN: ${{ secrets.SNAPCRAFT_LOGIN }}
# run: |
# sudo apt update
# sudo apt install --yes fsharp
# ./scripts/snap_release.sh
13 changes: 12 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ newmono_test_integration:
make &&
make update-servers

stockmono_snap:
stockmono_snap_build:
image: ubuntu:20.04
stage: package

Expand All @@ -110,3 +110,14 @@ stockmono_snap:
- "*.snap"
expire_in: 50days

stockmono_snap_upload:
image: ubuntu:20.04
stage: package

needs:
- stockmono_snap_build
script:
- apt update && apt install --yes git make curl fsharp
- ./configure.sh # in case we need fsx instead of fsharpi
- ./scripts/snap_release.sh

79 changes: 79 additions & 0 deletions scripts/githubActions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace GWallet.Github

open System
open System.IO
open System.Linq
open System.Threading
open System.Text
open System.Configuration
open System.Net.Http
open System.Net.Http.Headers
open System.Web.Script.Serialization
open System.Collections
open System.Collections.Generic

module GithubActions =
let private SendRequest (url: string) =
async {
use client = new HttpClient()
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse "application/vnd.github.v3+json")
client.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue("CIChecker", "1.0.0"))
return! client.GetStringAsync url |> Async.AwaitTask
}

let private QueryRunCount (status: string) (lastCommit: string) currentBranch =
async {
let url =
sprintf
"https://api.github.com/repos/nblockchain/geewallet/actions/runs?status=%s&branch=%s"
status
currentBranch

Console.WriteLine (sprintf "Querying github API: %s" url)

let! response = SendRequest url

let responseObj =
JavaScriptSerializer().Deserialize<Dictionary<string, obj>> response

match responseObj.TryGetValue "workflow_runs" with
| true, workflowRuns ->
match workflowRuns with
| :? ArrayList as runsArray ->
return
runsArray.OfType<Dictionary<string, obj>>()
|> Seq.filter
(fun run ->
match run.TryGetValue "head_sha" with
| false, _ ->
failwithf "Couldn't find 'head_sha' in sub-JSON: %s" response
| true, headSha ->
match headSha with
| :? string as headShaString ->
lastCommit.StartsWith headShaString || headShaString.StartsWith lastCommit
| _ ->
failwithf "Couldn't cast 'head_sha' to string: %s" response
)
|> Seq.length
| _ -> return failwithf "Couldn't cast 'workflow_runs' to ArrayList: %s" response
| false, _ ->
return failwithf "Couldn't find 'workflow_runs' in JSON: %s" response

}

let private CheckAllRuns lastCommit currentBranch =
async {
let! successfulCount = QueryRunCount "success" lastCommit currentBranch
let! failedCount = QueryRunCount "failure" lastCommit currentBranch

if failedCount > 0 || successfulCount < 1 then
return false
else
return true
}

let MakeSureGithubCIPassed (lastCommit: string) (currentBranch: string) =
if CheckAllRuns lastCommit currentBranch |> Async.RunSynchronously then
Console.WriteLine (sprintf "GitHubCI is green for branch %s (commit %s)" currentBranch lastCommit)
else
failwithf "Failed job in GitHub: https://github.com/nblockchain/geewallet/commit/%s" lastCommit
93 changes: 70 additions & 23 deletions scripts/snap_release.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ open Process
#load "fsxHelper.fs"
open GWallet.Scripting

#r "System.Net.Http.dll"
#r "System.Web.Extensions.dll"
open System.Web.Script.Serialization
#load "githubActions.fs"
open GWallet.Github

type GitProvider =
| GitHub
| GitLab

let snapFiles = FsxHelper.RootDir.EnumerateFiles().Where(fun file -> file.Name.EndsWith ".snap")
if not (snapFiles.Any()) then
Console.Error.WriteLine "No snap package found."
Expand All @@ -34,37 +44,74 @@ if null = snapFile then
Console.Error.WriteLine "Too many snap packages found, please discard invalid/old ones first."
Environment.Exit 2

Console.WriteLine "Checking if this is a tag commit..."
let githubRef = Environment.GetEnvironmentVariable "GITHUB_REF"
if String.IsNullOrEmpty githubRef then
failwith "GITHUB_REF var not found. Beware: manual logging for release has been disabled, only automated CI jobs can upload now"

let tagsPrefix = "refs/tags/"
if not (githubRef.StartsWith tagsPrefix) then
Console.WriteLine (sprintf "No tag being set (GITHUB_REF=%s), skipping release." githubRef)
Environment.Exit 0

let gitTag = githubRef.Substring tagsPrefix.Length
if not (snapFile.FullName.Contains gitTag) then
Console.Error.WriteLine (
sprintf "Git tag (%s) doesn't match version in snap package file name (%s)"
gitTag
snapFile.FullName
)
Environment.Exit 3

Console.WriteLine (sprintf "About to start upload of release %s" gitTag)
let gitlabRef = Environment.GetEnvironmentVariable "CI_COMMIT_REF_SLUG"
let onlyCiMsg = "No github or gitlab variable found, beware: manual logging for release has been disabled, only automated CI jobs can upload now"
let gitProvider =
if not (String.IsNullOrEmpty githubRef) then
GitHub
elif not (String.IsNullOrEmpty gitlabRef) then
GitLab
else
failwith onlyCiMsg

let snapcraftLoginFileName = Path.Combine(FsxHelper.RootDir.FullName, "snapcraft.login")
if File.Exists snapcraftLoginFileName then
Console.WriteLine "snapcraft.login file found, skipping log-in"
else
let snapcraftLogin = Environment.GetEnvironmentVariable "SNAPCRAFT_LOGIN"
if String.IsNullOrEmpty snapcraftLogin then
failwith "Manual logging for release has been disabled, only automated CI jobs can upload now"
let snapcraftLoginEnvVar =
match gitProvider with
| GitHub ->
"SNAPCRAFT_LOGIN"
| GitLab ->
"SNAPCRAFT_LOGIN_FILE"

let snapcraftLoginEnvVarValue = Environment.GetEnvironmentVariable snapcraftLoginEnvVar
if String.IsNullOrEmpty snapcraftLoginEnvVarValue then
failwith "SNAPCRAFT_LOGIN-prefixed env var not found; note: manual logging for release has been disabled, only automated CI jobs can upload now"
else
Console.WriteLine "Automatic login about to begin..."
File.WriteAllText(snapcraftLoginFileName, snapcraftLogin)
match gitProvider with
| GitHub ->
File.WriteAllText(snapcraftLoginFileName, snapcraftLoginEnvVarValue)
| GitLab ->
if not (File.Exists snapcraftLoginEnvVarValue) then
failwithf "File '%s' secret doesn't exist, can't upload release." snapcraftLoginEnvVarValue

File.Copy(snapcraftLoginEnvVarValue, snapcraftLoginFileName)

Console.WriteLine "Checking if this is a tag commit..."

let gitTag =
match gitProvider with
| GitHub ->
let tagsPrefix = "refs/tags/"
if not (githubRef.StartsWith tagsPrefix) then
Console.WriteLine (sprintf "No tag being set (GITHUB_REF=%s), skipping release." githubRef)
Environment.Exit 0
githubRef.Substring tagsPrefix.Length

| GitLab ->
let commitHash = Git.GetLastCommit()
let currentBranch = Environment.GetEnvironmentVariable "CI_COMMIT_REF_NAME"
if String.IsNullOrEmpty currentBranch then
failwith "CI_COMMIT_REF_NAME should be available when GitLab: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html"

GithubActions.MakeSureGithubCIPassed commitHash currentBranch

let ciTag = Environment.GetEnvironmentVariable "CI_COMMIT_TAG"

if String.IsNullOrEmpty ciTag then
Console.WriteLine (sprintf "No tag being set (CI_COMMIT_TAG=%s), skipping release." ciTag)
Environment.Exit 0
ciTag

if not (snapFile.FullName.Contains gitTag) then
failwithf "Git tag (%s) doesn't match version in snap package file name (%s)"
gitTag
snapFile.FullName

Console.WriteLine (sprintf "About to start upload of release %s" gitTag)

// if this fails, use `snapcraft export-login` to generate a new token
Process.SafeExecute ({ Command = "snapcraft"; Arguments = "login --with snapcraft.login" }, Echo.All)
Expand Down

0 comments on commit 8b87b15

Please sign in to comment.