close
close

Publish packages to AWS CodeArtifact using Amazon CodeCatalyst actions

Publish packages to AWS CodeArtifact using Amazon CodeCatalyst actions

Amazon CodeCatalyst is a unified software development service for development teams to quickly build, deploy, and scale applications on AWS while adhering to organizational best practices. Developers can automate development tasks and innovate faster with generative AI capabilities, and spend less time setting up project tools, managing CI/CD pipelines, deploying and configuring different development environments, or coordinating with team members.

It can integrate with services such as AWS CodeArtifact, a managed artifact repository service that allows you to securely store, publish, and share software packages. In this blog post, we’ll show you how to use the Publish to AWS CodeArtifact action in a CodeCatalyst workflow to publish packages to AWS Code Artifact.

In Amazon CodeCatalyst, an action is the main building block of a workflow and defines a logical unit of work to be performed during the execution of a workflow. Typically, a workflow includes multiple actions that are executed sequentially or in parallel, depending on the configuration. Amazon CodeCatalyst provides a library of pre-built actions that you can use in your workflows, such as building, testing, and deploying applications. You can also create custom actions for specific tasks that are not covered by the pre-built options.

The following are instructions for using the Publish to AWS CodeArtifact action in the Amazon CodeCatalyst workflow.

Requirements

To follow this walkthrough, you will need:

{
  "Version": "2012-10-17",
  "Statement": (
     {
        "Effect": "Allow",
        "Action": (
           "codeartifact:GetAuthorizationToken",
           "codeartifact:GetRepositoryEndpoint",
           "codeartifact:PublishPackageVersion",
           "codeartifact:PutPackageMetadata",
           "sts:GetServiceBearerToken"
        ),
        "Resource": "*"   
     }
  )
}

And the following custom trust policy.

{
  "Version": "2012-10-17",
  "Statement": (
     { 
        "Effect": "Allow",
        "Principal": {
          "Service":  (
            "codecatalyst-runner.amazonaws.com",
            "codecatalyst.amazonaws.com"     
           )  
        },
        "Action": "sts:AssumeRole"   
     }
  )
}

In the trust policy, we specified two AWS services in the Principal element. Service principals are defined by the service. The following service principals are defined for CodeCatalyst:

  • amazonaws.com – This service principal is used for a role that grants CodeCatalyst access to AWS.
  • codecatalyst-runner.amazonaws.com – This service principal is used for a role that grants CodeCatalyst access to AWS resources in CodeCatalyst workflow deployments.

Solution

In this example, we publish an npm package to a CodeArtifact repository named “myapp-frontend” in the “myapp-artifacts” domain. Amazon CodeCatalyst is currently available in two regions, namely Europe (Ireland) and US West (Oregon). We use “us-west-2” for all resources in this walkthrough.

Here are the steps to create your workflow.

  1. In the navigation pane, select CI/CDand then select Workflows.
  2. Choose Create workflow.

The workflow definition file appears in the YAML editor of the CodeCatalyst console.

How to configure your workflow

You can manage your workflow in Visually Publisher or the YAML Let’s start with the YAML editor and then move on to the visual editor.

  1. Choose + Actions to view a list of workflow actions you can add to your workflow.
  2. In the Build Select + to add the action’s YAML file to your workflow definition file. Your workflow will now look something like this. You can follow the code below by editing it in the YAML editor.

This image shows the Build action from the Action dropdown list in the Amazon CodeCatalyst workflow.

The following code shows the newly created workflow.

Name: CodeArtifactWorkflow
SchemaVersion: "1.0"

# Optional - Set automatic triggers.
Triggers:
  - Type: Push
    Branches:
      - main

# Required - Define action configurations.
Actions:
  Build:
    # Identifies the action. Do not modify this value.
    Identifier: aws/[email protected]
    # Specifies the source and/or artifacts to pass to the action as input.
    Inputs:
      # Optional
      Sources:
        - WorkflowSource # This specifies that the action requires this Workflow as a source
    Outputs:
      Artifacts:
        - Name: ARTIFACT
          Files:
            - "**/*"
    # Defines the action's properties.
    Configuration:
      # Required - Steps are sequential instructions that run shell commands
      Steps:
        - Run: cd integration/npm/npm-package-example-main
        - Run: npm pack
        - Run: ls
    Compute:
      Type: EC2
    Environment:
      Connections:
        - Role: CodeCatalystWorkflowDevelopmentRole-action-workshop
          Name: codecatalystconnection
      Name: action-builder

In this build action, we will use the “npm pack” command to create a compressed tarball file (.tgz) with our package’s source code and configuration files. We will create an output artifact named “ARTIFACT” and our files will be located in this directory integration/npm/npm-package-example-master.

Now we select the Publish-to-Code Artifact action from the action drop-down list.

This image shows the Publish to AWS CodeArtifact action from the Actions dropdown list in the Amazon CodeCatalyst workflow.

The following code shows the newly added action in the workflow file.

Publish-to-code-artifact:
    Identifier: .
    
    Environment:
      Connections:
        - Role: CodeCatalystWorkflowDevelopmentRole-action-workshop
          Name: mushhz
      Name: action-builder
      
    Inputs:
      Sources:
        - WorkflowSource
      Artifacts:
        - ARTIFACT
        
    Compute:
      Type: EC2
      
    Configuration:
      PackagePath: /artifacts/Validatepublish-to-code-artifact/ARTIFACT/integration/npm/npm-package-example-main/ktsn-npm-package-example-1.0.1.tgz
      PackageFormat: npm
      RepositoryName: action-builder
      AWSRegion: us-west-2
      DomainName: action-builder

In the above code, you can see that we have specified PackageFormat, RepositoryName, DomainName and AWSRegion. These are all required fields.

The package path is the output path of the build artifact + the folder path.

The full workflow file can be found in this GitHub repository.

If you choose the Visual option to view the workflow definition file in the visual editor, it will look like the image below. The fields in the visual editor allow you to configure the YAML properties displayed in the YAML editor.

If you choose the Visual option to view the workflow definition file in the visual editor, it will look like the image below.

Here’s how the Publish to AWS CodeArtifact action works:

The Publish to AWS CodeArtifact action works as follows at runtime:

  • Verifies that package format, package path, repository name, domain name, and AWS Region are specified, validates the configuration, and configures AWS credentials based on the specified environment, connection, and role.
  • Looks for package files to publish in the path configured in the PackagePath field in the WorkflowSource. If no source is configured in Sources but an artifact is configured in Arifacts, the action looks for the files in the configured artifact folder.
  • Publishes the package to AWS CodeArtifact.

Cleanup

Once you have followed this workflow, you should delete the provisioned resources to avoid further charges.

  • Delete the published package in AWS CodeArtifact by following these instructions.
  • Delete the repository in AWS CodeArtifact by following these instructions.
  • Delete the domain in AWS CodeArtifact by following these instructions.
  • If you created a new Amazon CodeCatalyst project for this tutorial, delete it. For instructions, see Deleting a Project. Deleting the project also deletes the source repository and workflow.

Diploma

In this post, we showed how to use an Amazon CodeCatalyst workflow to publish packages to AWS CodeArtifact using the Publish to AWS CodeArtifact action. By following the steps outlined in this blog post, you can ensure that your packages are immediately available to your projects while maintaining version control and security.

For more information, see “Working with Actions” in the CodeCatalyst documentation.


About the authors

Muhammad Shahzad

Muhammad Shahzad is a Solutions Architect at AWS. He is passionate about helping customers succeed in their journey to the cloud. He enjoys designing solutions and helping them implement DevSecOps by explaining principles, creating automated solutions, and incorporating best practices into their journey to the cloud. Outside of work, Muhammad regularly plays badminton, participates in various other sports, and is a passionate hiker on scenic trails.

Alexander Schueren

Alexander Schueren is a Senior Specialist Solutions Architect at AWS, dedicated to modernizing legacy applications and building event-driven serverless solutions. With a focus on simplifying complex tasks and bringing clarity to technical challenges, Alexander’s mission is to equip developers with the tools they need to succeed. As a maintainer of the open source project “Powertools for AWS Lambda (TypeScript)”, he is committed to driving innovation in serverless technologies. In his free time, Alexander draws his creativity from street photography, capturing crucial moments in the urban landscape.

Leave a Reply

Your email address will not be published. Required fields are marked *