ARTIFACTORY: How to build and deploy a Package to a swift Local Repository
This article provides a step-by-step guide for building and deploying a package to a Swift local Artifactory repository. Prerequisites: Swift client installed on your machine. Artifactory Swift Local repository set up. Steps to Build and Deploy a Swift Package: 1. First, create a new Swift project called "AshrithaProjectFolder" using the Swift client:
# Create a new Swift project called AshrithaProjectFolder $ mkdir AshrithaProjectFolder $ cd AshrithaProjectFolder
2. Please create a directory with the following structureAshrithaProjectFolder/
├── Package.swift
├── Sources/
│ └── AshrithaTargetName/
│ ├── AshrithaSourceFile.swift
│ └── main.swift
└── Tests/
└── AshrithaTargetNameTests/
└── AshrithaTestFile.swift3. In the project directory, create a Package.swift file to define the Swift package:
// swift-tools-version:5.6
import PackageDescription
let package = Package(
name: "AshrithaProject",
platforms: [
.macOS(.v11) // You can modify the version depending on your macOS version.
],
products: [
.executable(
name: "AshrithaProject",
targets: ["AshrithaTargetName"]
),
],
dependencies: [
// Add any dependencies here if needed, e.g.:
// .package(url: "https://github.com/SomeLibrary/SomeLibrary.git", from: "1.0.0")
],
targets: [
.target(
name: "AshrithaTargetName",
dependencies: [],
path: "Sources/AshrithaTargetName"
),
.testTarget(
name: "AshrithaTargetNameTests",
dependencies: ["AshrithaTargetName"],
path: "Tests/AshrithaTargetNameTests"
),
]
)
4. Add the following in your source files. AshrithaSourceFile.swift:
// Sources/AshrithaTargetName/AshrithaSourceFile.swift
import Foundation
func greet(name: String) -> String {
return "Hello, \(name)!"
}
main.swift:
// Sources/AshrithaTargetName/main.swift import Foundation let greeting = greet(name: "Ashritha") print(greeting) // Output: Hello, Ashritha!
5. Add below in AshrithaTestFile.swift // Tests/AshrithaTargetNameTests/AshrithaTestFile.swift
import XCTest
@testable import AshrithaTargetName
final class AshrithaTestFile: XCTestCase {
func testGreet() {
// Test the greet function
let result = greet(name: "Ashritha")
XCTAssertEqual(result, "Hello, Ashritha!")
}
}
6. Run the following command to build your Swift package and fetch any dependencies from the specified URLs in Package.swift:$ swift build This will build your project successfully, fetching the necessary dependencies from the provided URLs.
7. Configure the Swift package registry to use your Artifactory repository:$ swift package-registry set "https://myartifactory.jfrog.io/artifactory/api/swift/swift-local" Ensure that you are in the project directory containing the Package.swift file when you run this command.
8. Create a package archive by running the command:# swift package archive-source This command zips all the files under the project folder into a single archived zip file. If the project folder is not initialized as a git repository, follow the commands below to initialize it: # git init // Initializes a git repository within the folder. # git add . // Adds the changes to the next commit in the repository. # git commit -m "Test Commit" // Commits the changes to the git repository.
9. After executing the above commands, ran `swift package archive-source` in the project folder to create the zip file. The directory listing will display the newly created zip file.# ls -latr total 197848 drwxr-xr-x 8 user staff 256 Jan 31 08:25 .. -rw-r--r-- 1 user staff 456 Jan 31 08:28 Package.swift drwxr-xr-x 4 user staff 128 Jan 31 08:32 Sources drwxr-xr-x 3 user staff 96 Jan 31 08:35 main drwxr-xr-x 10 user staff 320 Jan 31 08:37 .build drwxr-xr-x 3 user staff 96 Jan 31 08:38 .swiftpm -rw-r--r-- 1 user staff 99252613 Jan 31 08:38 YourPackage.zip drwxr-xr-x 8 user staff 256 Jan 31 08:38 .
10. Deployed the zip file, along with the scope and version, to Artifactory using the following cURL command:#curl -X PUT –uadmin:password -H "Accept: application/vnd.swift.registry.v1+json" -F source-archive="@YourPackage.zip" https://myartifactory.jfrog.io/artifactory/api/swift/<repo-name>/test/YourPackage/1.0.0
After executing the command, you will notice that the package has been deployed to Artifactory with the corresponding scope and version.Conclusion Following these steps, you can successfully build and deploy a Swift package to a local Artifactory repository. Ensure all prerequisites are met and commands are executed correctly to avoid errors during deployment.