| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- package actions
- import (
- "context"
- "encoding/base64"
- "errors"
- "fmt"
- "net/http"
- "github.com/Masterminds/semver/v3"
- ghinstallation "github.com/bradleyfalzon/ghinstallation/v2"
- "github.com/google/go-github/v41/github"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/oauth"
- "github.com/porter-dev/porter/internal/repository"
- "golang.org/x/crypto/nacl/box"
- "golang.org/x/oauth2"
- "strings"
- "gopkg.in/yaml.v2"
- )
- var (
- ErrProtectedBranch = errors.New("protected branch")
- ErrCreatePRForProtectedBranch = errors.New("unable to create PR to merge workflow files into protected branch")
- )
- type GithubActions struct {
- ServerURL string
- InstanceName string
- GithubOAuthIntegration *models.GitRepo
- GitRepoName string
- GitRepoOwner string
- Repo repository.Repository
- GithubConf *oauth2.Config // one of these will let us authenticate
- GithubAppID int64
- GithubAppSecretPath string
- GithubInstallationID uint
- PorterToken string
- BuildEnv map[string]string
- ProjectID uint
- ClusterID uint
- ReleaseName string
- ReleaseNamespace string
- GitBranch string
- DockerFilePath string
- FolderPath string
- ImageRepoURL string
- defaultBranch string
- Version string
- DryRun bool
- ShouldCreateWorkflow bool
- }
- var (
- deleteWebhookAndEnvSecretsConstraint, _ = semver.NewConstraint(" < 0.1.0")
- )
- func (g *GithubActions) Setup() ([]byte, error) {
- client, err := g.getClient()
- if err != nil {
- return nil, err
- }
- // get the repository to find the default branch
- repo, _, err := client.Repositories.Get(
- context.TODO(),
- g.GitRepoOwner,
- g.GitRepoName,
- )
- if err != nil {
- return nil, err
- }
- g.defaultBranch = repo.GetDefaultBranch()
- if !g.DryRun {
- // create porter token secret
- if err := createGithubSecret(client, g.getPorterTokenSecretName(), g.PorterToken, g.GitRepoOwner, g.GitRepoName); err != nil {
- return nil, err
- }
- }
- workflowYAML, err := g.GetGithubActionYAML()
- if err != nil {
- return nil, err
- }
- if !g.DryRun && g.ShouldCreateWorkflow {
- branch := g.GitBranch
- if branch == "" {
- branch = g.defaultBranch
- }
- // check if the branch is protected
- githubBranch, _, err := client.Repositories.GetBranch(
- context.Background(),
- g.GitRepoOwner,
- g.GitRepoName,
- branch,
- true,
- )
- if err != nil {
- return nil, err
- }
- isOAuth := g.GithubOAuthIntegration != nil
- if githubBranch.GetProtected() {
- err = createNewBranch(client, g.GitRepoOwner, g.GitRepoName, branch, "porter-setup")
- if err != nil {
- return nil, fmt.Errorf(
- "Unable to create PR to merge workflow files into protected branch: %s.\n"+
- "To enable automatic deployments to Porter, please create a Github workflow "+
- "file in this branch with the following contents:\n"+
- "--------\n%s--------\nERROR: %w", branch, string(workflowYAML), ErrCreatePRForProtectedBranch,
- )
- }
- _, err = commitWorkflowFile(client, g.getPorterYMLFileName(), workflowYAML, g.GitRepoOwner,
- g.GitRepoName, "porter-setup", isOAuth)
- if err != nil {
- return nil, fmt.Errorf(
- "Unable to create PR to merge workflow files into protected branch: %s.\n"+
- "To enable automatic deployments to Porter, please create a Github workflow "+
- "file in this branch with the following contents:\n"+
- "--------\n%s--------\nERROR: %w", branch, string(workflowYAML), ErrCreatePRForProtectedBranch,
- )
- }
- pr, _, err := client.PullRequests.Create(
- context.Background(), g.GitRepoOwner, g.GitRepoName, &github.NewPullRequest{
- Title: github.String("Enable Porter automatic deployments"),
- Base: github.String(branch),
- Head: github.String("porter-setup"),
- },
- )
- if err != nil {
- return nil, err
- }
- return nil, fmt.Errorf("Please merge %s to enable automatic deployments on Porter.\nERROR: %w",
- pr.GetHTMLURL(), ErrProtectedBranch)
- }
- _, err = commitWorkflowFile(client, g.getPorterYMLFileName(), workflowYAML, g.GitRepoOwner,
- g.GitRepoName, branch, isOAuth)
- if err != nil {
- return workflowYAML, err
- }
- }
- return workflowYAML, err
- }
- func (g *GithubActions) Cleanup() error {
- client, err := g.getClient()
- if err != nil {
- return err
- }
- // get the repository to find the default branch
- repo, _, err := client.Repositories.Get(
- context.TODO(),
- g.GitRepoOwner,
- g.GitRepoName,
- )
- if err != nil {
- return err
- }
- g.defaultBranch = repo.GetDefaultBranch()
- actionVersion, err := semver.NewVersion(g.Version)
- if err != nil {
- return err
- }
- if deleteWebhookAndEnvSecretsConstraint.Check(actionVersion) {
- // delete the webhook token secret
- if err := g.deleteGithubSecret(client, g.getWebhookSecretName()); err != nil {
- return err
- }
- // delete the env secret
- if err := g.deleteGithubSecret(client, g.getBuildEnvSecretName()); err != nil {
- return err
- }
- }
- branch := g.GitBranch
- if branch == "" {
- branch = g.defaultBranch
- }
- isOAuth := g.GithubOAuthIntegration != nil
- return deleteGithubFile(client, g.getPorterYMLFileName(), g.GitRepoOwner, g.GitRepoName, branch, isOAuth)
- }
- type GithubActionYAMLStep struct {
- Name string `yaml:"name,omitempty"`
- ID string `yaml:"id,omitempty"`
- Timeout uint64 `yaml:"timeout-minutes,omitempty"`
- Uses string `yaml:"uses,omitempty"`
- Run string `yaml:"run,omitempty"`
- With map[string]string `yaml:"with,omitempty"`
- Env map[string]string `yaml:"env,omitempty"`
- }
- type GithubActionYAMLOnPushBranches struct {
- Branches []string `yaml:"branches,omitempty"`
- }
- type GithubActionYAMLOnPush struct {
- Push GithubActionYAMLOnPushBranches `yaml:"push,omitempty"`
- }
- type GithubActionYAMLJob struct {
- RunsOn string `yaml:"runs-on,omitempty"`
- Steps []GithubActionYAMLStep `yaml:"steps,omitempty"`
- Concurrency map[string]string `yaml:"concurrency,omitempty"`
- }
- type GithubActionYAML struct {
- On interface{} `yaml:"on,omitempty"`
- Name string `yaml:"name,omitempty"`
- Jobs map[string]GithubActionYAMLJob `yaml:"jobs,omitempty"`
- }
- func (g *GithubActions) GetGithubActionYAML() ([]byte, error) {
- gaSteps := []GithubActionYAMLStep{
- getCheckoutCodeStep(),
- getSetTagStep(),
- getUpdateAppStep(g.ServerURL, g.getPorterTokenSecretName(), g.ProjectID, g.ClusterID, g.ReleaseName, g.ReleaseNamespace, g.Version),
- }
- branch := g.GitBranch
- if branch == "" {
- branch = g.defaultBranch
- }
- actionYAML := GithubActionYAML{
- On: GithubActionYAMLOnPush{
- Push: GithubActionYAMLOnPushBranches{
- Branches: []string{
- branch,
- },
- },
- },
- Name: "Deploy to Porter",
- Jobs: map[string]GithubActionYAMLJob{
- "porter-deploy": {
- RunsOn: "ubuntu-latest",
- Steps: gaSteps,
- },
- },
- }
- return yaml.Marshal(actionYAML)
- }
- func (g *GithubActions) getClient() (*github.Client, error) {
- // in the case that this still uses the oauth integration
- if g.GithubOAuthIntegration != nil {
- // get the oauth integration
- oauthInt, err := g.Repo.OAuthIntegration().ReadOAuthIntegration(g.ProjectID, g.GithubOAuthIntegration.OAuthIntegrationID)
- if err != nil {
- return nil, err
- }
- _, _, err = oauth.GetAccessToken(oauthInt.SharedOAuthModel, g.GithubConf, oauth.MakeUpdateOAuthIntegrationTokenFunction(oauthInt, g.Repo))
- if err != nil {
- return nil, err
- }
- client := github.NewClient(g.GithubConf.Client(oauth2.NoContext, &oauth2.Token{
- AccessToken: string(oauthInt.AccessToken),
- RefreshToken: string(oauthInt.RefreshToken),
- Expiry: oauthInt.Expiry,
- TokenType: "Bearer",
- }))
- return client, nil
- }
- // authenticate as github app installation
- itr, err := ghinstallation.NewKeyFromFile(
- http.DefaultTransport,
- g.GithubAppID,
- int64(g.GithubInstallationID),
- g.GithubAppSecretPath)
- if err != nil {
- return nil, err
- }
- return github.NewClient(&http.Client{Transport: itr}), nil
- }
- func createGithubSecret(
- client *github.Client,
- secretName,
- secretValue,
- gitRepoOwner,
- gitRepoName string,
- ) error {
- // get the public key for the repo
- key, _, err := client.Actions.GetRepoPublicKey(context.TODO(), gitRepoOwner, gitRepoName)
- if err != nil {
- return err
- }
- // encrypt the secret with the public key
- keyBytes := [32]byte{}
- keyDecoded, err := base64.StdEncoding.DecodeString(*key.Key)
- if err != nil {
- return err
- }
- copy(keyBytes[:], keyDecoded[:])
- secretEncoded, err := box.SealAnonymous(nil, []byte(secretValue), &keyBytes, nil)
- if err != nil {
- return err
- }
- encrypted := base64.StdEncoding.EncodeToString(secretEncoded)
- encryptedSecret := &github.EncryptedSecret{
- Name: secretName,
- KeyID: *key.KeyID,
- EncryptedValue: encrypted,
- }
- // write the secret to the repo
- _, err = client.Actions.CreateOrUpdateRepoSecret(context.TODO(), gitRepoOwner, gitRepoName, encryptedSecret)
- return err
- }
- func (g *GithubActions) deleteGithubSecret(
- client *github.Client,
- secretName string,
- ) error {
- // delete the secret from the repo
- _, err := client.Actions.DeleteRepoSecret(
- context.TODO(),
- g.GitRepoOwner,
- g.GitRepoName,
- secretName,
- )
- return err
- }
- func (g *GithubActions) CreateEnvSecret() error {
- client, err := g.getClient()
- if err != nil {
- return err
- }
- return g.createEnvSecret(client)
- }
- func (g *GithubActions) createEnvSecret(client *github.Client) error {
- // convert the env object to a string
- lines := make([]string, 0)
- for key, val := range g.BuildEnv {
- lines = append(lines, fmt.Sprintf(`%s=%s`, key, val))
- }
- secretName := g.getBuildEnvSecretName()
- return createGithubSecret(client, secretName, strings.Join(lines, "\n"), g.GitRepoOwner, g.GitRepoName)
- }
- func (g *GithubActions) getWebhookSecretName() string {
- return fmt.Sprintf("WEBHOOK_%s", strings.Replace(
- strings.ToUpper(g.ReleaseName), "-", "_", -1),
- )
- }
- func (g *GithubActions) getBuildEnvSecretName() string {
- return fmt.Sprintf("ENV_%s", strings.Replace(
- strings.ToUpper(g.ReleaseName), "-", "_", -1),
- )
- }
- func (g *GithubActions) getPorterYMLFileName() string {
- if g.InstanceName != "" {
- return fmt.Sprintf("porter_%s_%s.yml", strings.Replace(
- strings.ToLower(g.ReleaseName), "-", "_", -1),
- strings.ToLower(g.InstanceName),
- )
- }
- return fmt.Sprintf("porter_%s.yml", strings.Replace(
- strings.ToLower(g.ReleaseName), "-", "_", -1),
- )
- }
- func (g *GithubActions) getPorterTokenSecretName() string {
- if g.InstanceName != "" {
- return fmt.Sprintf("PORTER_TOKEN_%s_%d", strings.ToUpper(g.InstanceName), g.ProjectID)
- }
- return fmt.Sprintf("PORTER_TOKEN_%d", g.ProjectID)
- }
- func getPorterTokenSecretName(projectID uint) string {
- return fmt.Sprintf("PORTER_TOKEN_%d", projectID)
- }
- func commitWorkflowFile(
- client *github.Client,
- filename string,
- contents []byte,
- gitRepoOwner, gitRepoName, branch string,
- isOAuth bool,
- ) (string, error) {
- filepath := ".github/workflows/" + filename
- sha := ""
- // get contents of a file if it exists
- fileData, _, _, _ := client.Repositories.GetContents(
- context.TODO(),
- gitRepoOwner,
- gitRepoName,
- filepath,
- &github.RepositoryContentGetOptions{
- Ref: branch,
- },
- )
- if fileData != nil {
- sha = *fileData.SHA
- }
- opts := &github.RepositoryContentFileOptions{
- Message: github.String(fmt.Sprintf("Create %s file", filename)),
- Content: contents,
- Branch: github.String(branch),
- SHA: &sha,
- }
- if isOAuth {
- opts.Committer = &github.CommitAuthor{
- Name: github.String("Porter Bot"),
- Email: github.String("contact@getporter.dev"),
- }
- }
- resp, _, err := client.Repositories.UpdateFile(
- context.TODO(),
- gitRepoOwner,
- gitRepoName,
- filepath,
- opts,
- )
- if err != nil {
- return "", err
- }
- return *resp.Commit.SHA, nil
- }
- func deleteGithubFile(
- client *github.Client,
- filename, gitRepoOwner, gitRepoName, branch string,
- isOAuth bool,
- ) error {
- filepath := ".github/workflows/" + filename
- // get contents of a file if it exists
- fileData, _, _, _ := client.Repositories.GetContents(
- context.TODO(),
- gitRepoOwner,
- gitRepoName,
- filepath,
- &github.RepositoryContentGetOptions{
- Ref: branch,
- },
- )
- sha := ""
- if fileData != nil {
- sha = *fileData.SHA
- }
- opts := &github.RepositoryContentFileOptions{
- Message: github.String(fmt.Sprintf("Delete %s file", filename)),
- Branch: &branch,
- SHA: &sha,
- }
- if isOAuth {
- opts.Committer = &github.CommitAuthor{
- Name: github.String("Porter Bot"),
- Email: github.String("contact@getporter.dev"),
- }
- }
- _, response, err := client.Repositories.DeleteFile(
- context.TODO(),
- gitRepoOwner,
- gitRepoName,
- filepath,
- opts,
- )
- if response != nil && response.StatusCode == 404 {
- return nil
- }
- if err != nil {
- return err
- }
- return nil
- }
|