steps.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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(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. ) GithubActionYAMLStep {
  71. var path string
  72. if porterYamlPath != "" {
  73. path = porterYamlPath
  74. } else {
  75. path = "porter.yaml"
  76. }
  77. return GithubActionYAMLStep{
  78. Name: "Deploy stack",
  79. Uses: fmt.Sprintf("%s@%s", cliActionName, actionVersion),
  80. With: map[string]string{
  81. "command": fmt.Sprintf("apply -f %s", path),
  82. },
  83. Env: map[string]string{
  84. "PORTER_CLUSTER": fmt.Sprintf("%d", clusterID),
  85. "PORTER_HOST": serverURL,
  86. "PORTER_PROJECT": fmt.Sprintf("%d", projectID),
  87. "PORTER_TOKEN": fmt.Sprintf("${{ secrets.%s }}", porterTokenSecretName),
  88. "PORTER_TAG": "${{ steps.vars.outputs.sha_short }}",
  89. "PORTER_STACK_NAME": stackName,
  90. },
  91. Timeout: 30,
  92. }
  93. }