Skip to content
Snippets Groups Projects
Commit 8b87b15b authored by Afshin Arani's avatar Afshin Arani Committed by Andres G. Aragoneses
Browse files

scripts: move snap release from GitHubCI to GitLabCI

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: gitlab-org/gitlab#30632



Co-authored-by: default avatarAndres G. Aragoneses <knocte@gmail.com>
parent 0aeb1ad0
No related branches found
No related tags found
No related merge requests found
Pipeline #415969090 passed
...@@ -79,10 +79,12 @@ jobs: ...@@ -79,10 +79,12 @@ jobs:
sudo apt update sudo apt update
./scripts/install_snapcraft.sh ./scripts/install_snapcraft.sh
./scripts/snap_build.sh ./scripts/snap_build.sh
- name: Upload snap package to Snap Store
env: # this step below is commented because we now upload the snap in GitLab:
SNAPCRAFT_LOGIN: ${{ secrets.SNAPCRAFT_LOGIN }} # - name: Upload snap package to Snap Store
run: | # env:
sudo apt update # SNAPCRAFT_LOGIN: ${{ secrets.SNAPCRAFT_LOGIN }}
sudo apt install --yes fsharp # run: |
./scripts/snap_release.sh # sudo apt update
# sudo apt install --yes fsharp
# ./scripts/snap_release.sh
...@@ -90,7 +90,7 @@ newmono_test_integration: ...@@ -90,7 +90,7 @@ newmono_test_integration:
make && make &&
make update-servers make update-servers
stockmono_snap: stockmono_snap_build:
image: ubuntu:20.04 image: ubuntu:20.04
stage: package stage: package
...@@ -110,3 +110,14 @@ stockmono_snap: ...@@ -110,3 +110,14 @@ stockmono_snap:
- "*.snap" - "*.snap"
expire_in: 50days 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
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
...@@ -24,6 +24,16 @@ open Process ...@@ -24,6 +24,16 @@ open Process
#load "fsxHelper.fs" #load "fsxHelper.fs"
open GWallet.Scripting 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") let snapFiles = FsxHelper.RootDir.EnumerateFiles().Where(fun file -> file.Name.EndsWith ".snap")
if not (snapFiles.Any()) then if not (snapFiles.Any()) then
Console.Error.WriteLine "No snap package found." Console.Error.WriteLine "No snap package found."
...@@ -34,37 +44,74 @@ if null = snapFile then ...@@ -34,37 +44,74 @@ if null = snapFile then
Console.Error.WriteLine "Too many snap packages found, please discard invalid/old ones first." Console.Error.WriteLine "Too many snap packages found, please discard invalid/old ones first."
Environment.Exit 2 Environment.Exit 2
Console.WriteLine "Checking if this is a tag commit..."
let githubRef = Environment.GetEnvironmentVariable "GITHUB_REF" let githubRef = Environment.GetEnvironmentVariable "GITHUB_REF"
if String.IsNullOrEmpty githubRef then let gitlabRef = Environment.GetEnvironmentVariable "CI_COMMIT_REF_SLUG"
failwith "GITHUB_REF var not found. Beware: manual logging for release has been disabled, only automated CI jobs can upload now" 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 =
let tagsPrefix = "refs/tags/" if not (String.IsNullOrEmpty githubRef) then
if not (githubRef.StartsWith tagsPrefix) then GitHub
Console.WriteLine (sprintf "No tag being set (GITHUB_REF=%s), skipping release." githubRef) elif not (String.IsNullOrEmpty gitlabRef) then
Environment.Exit 0 GitLab
else
let gitTag = githubRef.Substring tagsPrefix.Length failwith onlyCiMsg
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 snapcraftLoginFileName = Path.Combine(FsxHelper.RootDir.FullName, "snapcraft.login") let snapcraftLoginFileName = Path.Combine(FsxHelper.RootDir.FullName, "snapcraft.login")
if File.Exists snapcraftLoginFileName then if File.Exists snapcraftLoginFileName then
Console.WriteLine "snapcraft.login file found, skipping log-in" Console.WriteLine "snapcraft.login file found, skipping log-in"
else else
let snapcraftLogin = Environment.GetEnvironmentVariable "SNAPCRAFT_LOGIN" let snapcraftLoginEnvVar =
if String.IsNullOrEmpty snapcraftLogin then match gitProvider with
failwith "Manual logging for release has been disabled, only automated CI jobs can upload now" | 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 else
Console.WriteLine "Automatic login about to begin..." 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 // if this fails, use `snapcraft export-login` to generate a new token
Process.SafeExecute ({ Command = "snapcraft"; Arguments = "login --with snapcraft.login" }, Echo.All) Process.SafeExecute ({ Command = "snapcraft"; Arguments = "login --with snapcraft.login" }, Echo.All)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment