Marco Eidinger
Swifty Tech by Marco Eidinger

Swifty Tech by Marco Eidinger

Develop GitHub Actions with Swift

Develop GitHub Actions with Swift

Marco Eidinger's photo
Marco Eidinger
·Feb 26, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

I am an avid fan and user of GitHub Actions. Actions are individual tasks that you can combine to create jobs and customize your CI/CD workflow.

You can write your own actions to use in your workflow and share those actions with the GitHub community.

You cannot literally write GitHub actions exclusively in Swift as actions are natively Docker container actions or JavaScript actions.

TypeOperating system
Docker containerLinux
JavaScriptLinux, macOS, Windows

But you can use Swift to build large portions of your GitHub action. Here is a proposal on how to do it:

Untitled Diagram-3.png

A good example is the swift-create-xcframework GitHub Action.

In its action.yml file you see that the action is built with JavaScript.

runs:
  using: node12
  main: action.js

But the javascript code only parses the options and the heavy lifting is delegated to a particular Swift package which resides in the same repository.

In order to install a Swift package first use Homebrew to install Mint.

// install mint if its not installed
await installUsingBrewIfRequired("mint")

async function installUsingBrewIfRequired (package) {
    if (await isInstalled(package)) {
        core.info(package + " is already installed.")

    } else {
        core.info("Installing " + package)
        await exec.exec('brew', [ 'install', package ])
    }
}

Mint is able to install executable Swift packages and therefore is used to install the Swift package which is co-located with the action code.

// install ourselves if not installed
await installUsingMintIfRequired('swift-create-xcframework', 'unsignedapps/swift-create-xcframework')

async function installUsingMintIfRequired (command, package) {
    if (await isInstalled(command)) {
        core.info(command + " is already installed")

    } else {
        core.info("Installing " + package)
        await exec.exec('mint', [ 'install', 'unsignedapps/swift-create-xcframework@' + scxVersion ])
    }
}

Finally the executable Swift package gets called.

await exec.exec('swift-create-xcframework', options)

I hope this recipe is helpful for people who wondered how to leverage their Swift expertise when it comes to GitHub action development.

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this