steps.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 getUpdateAppStep(serverURL, porterTokenSecretName string, projectID uint, clusterID uint, appName string, appNamespace, actionVersion string) GithubActionYAMLStep {
  25. return GithubActionYAMLStep{
  26. Name: "Update Porter App",
  27. Uses: fmt.Sprintf("%s@%s", updateAppActionName, actionVersion),
  28. With: map[string]string{
  29. "app": appName,
  30. "cluster": fmt.Sprintf("%d", clusterID),
  31. "host": serverURL,
  32. "project": fmt.Sprintf("%d", projectID),
  33. "token": fmt.Sprintf("${{ secrets.%s }}", porterTokenSecretName),
  34. "tag": "${{ steps.vars.outputs.sha_short }}",
  35. "namespace": appNamespace,
  36. },
  37. Timeout: 20,
  38. }
  39. }
  40. func getCreatePreviewEnvStep(
  41. serverURL, porterTokenSecretName string,
  42. projectID, clusterID, gitInstallationID uint,
  43. repoOwner, repoName, actionVersion string,
  44. ) GithubActionYAMLStep {
  45. return GithubActionYAMLStep{
  46. Name: "Create Porter preview env",
  47. Uses: fmt.Sprintf("%s@%s", createPreviewActionName, actionVersion),
  48. With: map[string]string{
  49. "cluster": fmt.Sprintf("%d", clusterID),
  50. "host": serverURL,
  51. "project": fmt.Sprintf("%d", projectID),
  52. "token": fmt.Sprintf("${{ secrets.%s }}", porterTokenSecretName),
  53. "namespace": fmt.Sprintf("pr-${{ github.event.inputs.pr_number }}-%s",
  54. strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(repoName, "_", "-"), ".", "-"))),
  55. "pr_id": "${{ github.event.inputs.pr_number }}",
  56. "pr_name": "${{ github.event.inputs.pr_title }}",
  57. "installation_id": fmt.Sprintf("%d", gitInstallationID),
  58. "pr_branch_from": "${{ github.event.inputs.pr_branch_from }}",
  59. "pr_branch_into": "${{ github.event.inputs.pr_branch_into }}",
  60. "action_id": "${{ github.run_id }}",
  61. "repo_owner": repoOwner,
  62. "repo_name": repoName,
  63. },
  64. Timeout: 30,
  65. }
  66. }
  67. func getDeployStackStep(
  68. serverURL, porterTokenSecretName, stackName, actionVersion, porterYamlPath string,
  69. projectID, clusterID uint,
  70. preview bool,
  71. ) GithubActionYAMLStep {
  72. var path string
  73. if porterYamlPath != "" {
  74. path = porterYamlPath
  75. } else {
  76. path = "porter.yaml"
  77. }
  78. command := fmt.Sprintf("apply -f %s", path)
  79. if preview {
  80. command = fmt.Sprintf("%s --preview", command)
  81. }
  82. name := "Deploy stack"
  83. if preview {
  84. name = "Build and deploy preview environment"
  85. }
  86. return GithubActionYAMLStep{
  87. Name: name,
  88. Uses: fmt.Sprintf("%s@%s", cliActionName, actionVersion),
  89. With: map[string]string{
  90. "command": command,
  91. },
  92. Env: map[string]string{
  93. "PORTER_CLUSTER": fmt.Sprintf("%d", clusterID),
  94. "PORTER_HOST": serverURL,
  95. "PORTER_PROJECT": fmt.Sprintf("%d", projectID),
  96. "PORTER_TOKEN": fmt.Sprintf("${{ secrets.%s }}", porterTokenSecretName),
  97. "PORTER_TAG": "${{ steps.vars.outputs.sha_short }}",
  98. "PORTER_STACK_NAME": stackName,
  99. },
  100. Timeout: 30,
  101. }
  102. }