Publishing crates using GitHub Actions

Publishing crates using CLI is easy. But why do it?

Wouldn’t it be much better if we could automate this entire process? GitHub Actions help us do just that.

The key part of the action is going to be

For those well versed with GitHub Actions, you probably already stopped reading.

However, for those of us new to GitHub Actions, we are going to go through with the entire process.

You can view the full file here

Getting a API token

You can login with GitHub on to create an account or login to your existing one.

Upon reaching, to open your Settings page, scroll down to New Token and generate one.

The name here is not very important you can choose any name for it but do remember to save the token.

Save a secret on GitHub Actions

Open your repository, Click on Settings and Secrets

Settings -> Actions -> This page. You might not see a CRATES_TOKEN variable I do as I had already previously created one for my repo

As is clear from here, you need to press the New repository secret button

Add your token here.

Remember here the name matters. Personally I would recommend using CRATES_TOKEN here but if you change the name, remember to change it the next time as well

Writing our very own GitHub Action

Create the file

The first thing to do is create a file by the name of .github/workflows/publish.yml
You can change publish to something else without issues but publish is descriptive enough in this case

Specify how to trigger action

So we are triggering: -

  1. On push with tag (Recommended)
  2. Manually. (Makes it easy for me to explain it to you. But you can remove it)

Writing the job

Now that GitHub knows how to trigger, let us tell it how to get the job done

Pretty basic. We set the name and specify the OS

Checkout repo

Clone the repository to publish. Pretty basic

Install Rust

We are going to take the help of a very well known action for this.

Publish crate

The moment we have all been waiting for isn’t it?

If you have changed the name of the CRATES_TOKEN secret, you have to change secrets.CRATES_TOKEN to your tokens name

Full code

Why didn’t you use a fancy action for this?

In this case, because we don’t need to honestly.

Why didn’t we run tests?

For brevity.

You can run them very easily. Same command you use offline in fact.

Why do you like GitHub Actions?

  1. Free for an open source project
  2. Reduces the deployment effort because creating tags is easier
  3. No need to worry about saving or remembering the tokens

Creating tags for version updates? How!

First install cargo-bump

Then run

cargo bump minor — git-tag

Or it can be major or minor or patch. Semver style! Just like NPM

Then you can simply push to GitHub via

git push — follow-tags

Contact me

You can comment here or contact me via LinkedIn