Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP : Cbp 7139 adding tarflag #28

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cloudbees/testing/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ inputs:
gitlab-server-url:
description: The base URL for the GitLab instance that you are trying to clone from, will use environment defaults (i.e. the GITLAB_SERVER_URL environment variable) to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://gitlab.com or https://my-gl-server.example.com
required: false
create-tarfile:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be kept in sync with the root action.yaml. Why am I not seeing the matching change there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have to add the flag in both places

description: 'Whether to create a tarfile of the repository'
default: "false"
required: false
runs:
using: composite
steps:
Expand Down Expand Up @@ -113,3 +117,4 @@ runs:
"--github-server-url=${{ inputs.github-server-url }}" \
"--bitbucket-server-url=${{ inputs.bitbucket-server-url }}" \
"--gitlab-server-url=${{ inputs.gitlab-server-url }}" \
"--create-tarfile=${{ inputs.create-tarfile }}"
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func init() {
cmd.Flags().StringVar(&cfg.GithubServerURL, "github-server-url", "", "The base URL for the GitHub instance that you are trying to clone from")
cmd.Flags().StringVar(&cfg.BitbucketServerURL, "bitbucket-server-url", "", "The base URL for the Bitbucket instance that you are trying to clone from")
cmd.Flags().StringVar(&cfg.GitlabServerURL, "gitlab-server-url", "", "The base URL for the GitLab instance that you are trying to clone from")
cmd.Flags().BoolVar(&cfg.CreateTarfile, "create-tarfile", false, "Whether to create tar file of the checked out repository at workspace directory")

cmd.AddCommand(helperCmd)
}
Expand Down
67 changes: 67 additions & 0 deletions internal/checkout/source_provider.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package checkout

import (
"archive/tar"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -43,6 +45,7 @@ type Config struct {
GitlabServerURL string
Commit string
githubWorkflowOrganizationId string
CreateTarfile bool
}

const (
Expand Down Expand Up @@ -586,6 +589,17 @@ func (cfg *Config) Run(ctx context.Context) (retErr error) {
return err
}

// Tar the checked-out repository if flag enabled
if cfg.CreateTarfile {
core.StartGroup("Creating Tarfile of the repository")
tarFilePath := filepath.Join(workspacePath, "repository.tar.gz")
if err := generateTarfile(repositoryPath, tarFilePath); err != nil {
return fmt.Errorf("failed to create tarball: %w", err)
}
core.Debug("Repository tarball created at %s", tarFilePath)
core.EndGroup("Tarfile created")
}

// remove auth - already handled by defer functions

if os.Getenv("DEBUG_SHELL") != "" {
Expand Down Expand Up @@ -1090,3 +1104,56 @@ func prepareExistingDirectory(cli *git.GitCLI, repositoryPath string, repository
}
return nil
}

func generateTarfile(srcDir, filename string) error {

if err := core.DirExists(srcDir, true); err != nil {
return err
}

file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()

tw := tar.NewWriter(file)
defer tw.Close()

err = filepath.Walk(srcDir, func(file string, fi os.FileInfo, err error) error {
if err != nil {
return err
}

header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}

// Create a relative path for the tarball
header.Name, err = filepath.Rel(srcDir, file)
if err != nil {
return err
}

if err := tw.WriteHeader(header); err != nil {
return err
}

if fi.Mode().IsRegular() {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()

if _, err := io.Copy(tw, f); err != nil {
return err
}
}

return nil
})

return err
}