steps.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package actions
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. const (
  7. updateAppActionName = "porter-dev/porter-update-action"
  8. createPreviewActionName = "porter-dev/porter-preview-action"
  9. cliActionName = "porter-dev/porter-cli-action"
  10. )
  11. func getCheckoutCodeStep() GithubActionYAMLStep {
  12. return GithubActionYAMLStep{
  13. Name: "Checkout code",
  14. Uses: "actions/checkout@v3",
  15. }
  16. }
  17. func getSetTagStep() GithubActionYAMLStep {
  18. return GithubActionYAMLStep{
  19. Name: "Set Github tag",
  20. ID: "vars",
  21. Run: `echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT`,
  22. }
  23. }
  24. func getSetupPorterStep() GithubActionYAMLStep {
  25. return GithubActionYAMLStep{
  26. Name: "Setup porter",
  27. Uses: "porter-dev/setup-porter@v0.1.0",
  28. }
  29. }
  30. func getUpdateAppStep(serverURL, porterTokenSecretName string, projectID uint, clusterID uint, appName string, appNamespace, actionVersion string) GithubActionYAMLStep {
  31. return GithubActionYAMLStep{
  32. Name: "Update Porter App",
  33. Uses: fmt.Sprintf("%s@%s", updateAppActionName, actionVersion),
  34. With: map[string]string{
  35. "app": appName,
  36. "cluster": fmt.Sprintf("%d", clusterID),
  37. "host": serverURL,
  38. "project": fmt.Sprintf("%d", projectID),
  39. "token": fmt.Sprintf("${{ secrets.%s }}", porterTokenSecretName),
  40. "tag": "${{ steps.vars.outputs.sha_short }}",
  41. "namespace": appNamespace,
  42. },
  43. Timeout: 20,
  44. }
  45. }
  46. func getCreatePreviewEnvStep(
  47. serverURL, porterTokenSecretName string,
  48. projectID, clusterID, gitInstallationID uint,
  49. repoOwner, repoName, actionVersion string,
  50. ) GithubActionYAMLStep {
  51. return GithubActionYAMLStep{
  52. Name: "Create Porter preview env",
  53. Uses: fmt.Sprintf("%s@%s", createPreviewActionName, actionVersion),
  54. With: map[string]string{
  55. "cluster": fmt.Sprintf("%d", clusterID),
  56. "host": serverURL,
  57. "project": fmt.Sprintf("%d", projectID),
  58. "token": fmt.Sprintf("${{ secrets.%s }}", porterTokenSecretName),
  59. "namespace": fmt.Sprintf("pr-${{ github.event.inputs.pr_number }}-%s",
  60. strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(repoName, "_", "-"), ".", "-"))),
  61. "pr_id": "${{ github.event.inputs.pr_number }}",
  62. "pr_name": "${{ github.event.inputs.pr_title }}",
  63. "installation_id": fmt.Sprintf("%d", gitInstallationID),
  64. "pr_branch_from": "${{ github.event.inputs.pr_branch_from }}",
  65. "pr_branch_into": "${{ github.event.inputs.pr_branch_into }}",
  66. "action_id": "${{ github.run_id }}",
  67. "repo_owner": repoOwner,
  68. "repo_name": repoName,
  69. },
  70. Timeout: 30,
  71. }
  72. }
  73. func getDeployStackStep(
  74. serverURL, porterTokenSecretName, stackName, actionVersion, porterYamlPath string,
  75. projectID, clusterID uint,
  76. preview bool,
  77. ) GithubActionYAMLStep {
  78. var path string
  79. if porterYamlPath != "" {
  80. path = porterYamlPath
  81. } else {
  82. path = "porter.yaml"
  83. }
  84. command := fmt.Sprintf("porter apply -f %s", path)
  85. if preview {
  86. command = fmt.Sprintf("%s --preview", command)
  87. }
  88. name := "Deploy stack"
  89. if preview {
  90. name = "Build and deploy preview environment"
  91. }
  92. return GithubActionYAMLStep{
  93. Name: name,
  94. Run: command,
  95. Env: map[string]string{
  96. "PORTER_CLUSTER": fmt.Sprintf("%d", clusterID),
  97. "PORTER_HOST": serverURL,
  98. "PORTER_PROJECT": fmt.Sprintf("%d", projectID),
  99. "PORTER_TOKEN": fmt.Sprintf("${{ secrets.%s }}", porterTokenSecretName),
  100. "PORTER_TAG": "${{ steps.vars.outputs.sha_short }}",
  101. "PORTER_STACK_NAME": stackName,
  102. "PORTER_PR_NUMBER": "${{ github.event.number }}",
  103. },
  104. Timeout: 30,
  105. }
  106. }