I wrote a workflow to cross compile commands and release to GitHub Release in GitHub Actions.
https://github.com/jiro4989/repo-template-nim/blob/master/.github/workflows/main.yml
name: build
on:
push:
pull_request:
release:
types: [published]
env:
APP_NAME: 'APPNAME' # TODO: need change
jobs:
skip:
runs-on: ubuntu-latest
steps:
- run: echo "Skip job"
before:
runs-on: ubuntu-latest
if: "! contains(github.event.head_commit.message, '[skip ci]')"
steps:
- run: echo "not contains '[skip ci]'"
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
- macOS-latest
needs: before
env:
NIM_VERSION: 1.2.0
steps:
- uses: actions/checkout@v1
- name: Cache choosenim
id: cache-choosenim
uses: actions/cache@v1
with:
path: ~/.choosenim
key: ${{ runner.os }}-choosenim-${{ env.NIM_VERSION }}
- name: Cache nimble
id: cache-nimble
uses: actions/cache@v1
with:
path: ~/.nimble
key: ${{ runner.os }}-nimble-${{ hashFiles('*.nimble') }}
- uses: jiro4989/[email protected]
- run: nimble build -Y
- run: nimble test -Y
- name: Archive files
run: nimble archive
- name: Release
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v1
with:
files: 'dist/${{ env.APP_NAME }}_*.*'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
create-tag-draft:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
needs: build
steps:
- uses: release-drafter/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
This workflow creates a tag-draft when master was pushed.
And when you published the tag-draft, this workflow do cross-compiling your commands and release to GitHub Release.
https://github.com/jiro4989/repo-template-nim/releases
run: nimble archive is a original nimble task.
This task creates a release file.
It creates .zip file when OS is windows.
It creates .tar.gz file when OS is MacOS or Linux.
https://github.com/jiro4989/repo-template-nim/blob/master/APPNAME.nimble
import os, strformat
task archive, "Create archived assets":
let app = "APPNAME" # TODO: need change
let assets = &"{app}_{buildOS}"
let dir = "dist"/assets
mkDir dir
cpDir "bin", dir/"bin"
cpFile "LICENSE", dir/"LICENSE"
cpFile "README.rst", dir/"README.rst"
withDir "dist":
when buildOS == "windows":
exec &"7z a {assets}.zip {assets}"
else:
exec &"tar czf {assets}.tar.gz {assets}"
It's on my list to learn GitHub Actions. I just had a comment about the NimScript task archive. You can put that into a global config.nims of yours which you pull in before building all of your projects.
I have started using this minimal config.nims in my new projects. The project's .travis.yml then pulls in my global config.nims which enables running my generic tasks using nim docs, nim musl .. and nim test.
I updated release workflow.
It is a new workflow that uploads assets when tag was pushed.
name: release
on:
push:
tags:
- 'v*.*.*'
env:
APP_NAME: 'APPNAME'
NIM_VERSION: 'stable'
MAINTAINER: 'MAINTAINER'
jobs:
build-artifact:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
- macOS-latest
steps:
- uses: actions/checkout@v1
- uses: jiro4989/setup-nim-action@v1
with:
nim-version: ${{ env.NIM_VERSION }}
- run: nimble build -Y -d:release
- name: Create artifact
run: |
os="${{ runner.os }}"
assets="${{ env.APP_NAME }}_$(echo "${{ runner.os }}" | tr '[:upper:]' '[:lower:]')"
echo "$assets"
mkdir -p "dist/$assets"
cp -r bin LICENSE README.* "dist/$assets/"
(
cd dist
if [[ "${{ runner.os }}" == Windows ]]; then
7z a "$assets.zip" "$assets"
else
tar czf "$assets.tar.gz" "$assets"
fi
ls -lah *.*
)
shell: bash
- uses: actions/upload-artifact@v2
with:
name: artifact-${{ matrix.os }}
path: |
dist/*.tar.gz
dist/*.zip
create-release:
runs-on: ubuntu-latest
needs:
- build-artifact
steps:
- uses: actions/checkout@v1
- name: Create Release
id: create-release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
body: Release
draft: false
prerelease: false
- name: Write upload_url to file
run: echo '${{ steps.create-release.outputs.upload_url }}' > upload_url.txt
- uses: actions/upload-artifact@v2
with:
name: create-release
path: upload_url.txt
upload-release:
runs-on: ubuntu-latest
needs: create-release
strategy:
matrix:
include:
- os: ubuntu-latest
asset_name_suffix: linux.tar.gz
asset_content_type: application/gzip
- os: windows-latest
asset_name_suffix: windows.zip
asset_content_type: application/zip
- os: macOS-latest
asset_name_suffix: macos.tar.gz
asset_content_type: application/gzip
steps:
- uses: actions/download-artifact@v2
with:
name: artifact-${{ matrix.os }}
- uses: actions/download-artifact@v2
with:
name: create-release
- id: vars
run: |
echo "::set-output name=upload_url::$(cat upload_url.txt)"
- name: Upload Release Asset
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.vars.outputs.upload_url }}
asset_path: ${{ env.APP_NAME }}_${{ matrix.asset_name_suffix }}
asset_name: ${{ env.APP_NAME }}_${{ matrix.asset_name_suffix }}
asset_content_type: ${{ matrix.asset_content_type }}
CI runs when you create a new tags.
$ git tag v0.1.0
$ git push origin v0.1.0
This workflow uploads only archive files. See release.yml if you want to upload deb packages or rpm packages.
Thank you for sharing this! I've been looking for something like this for a while.
When I try to build it I get an error: https://github.com/sergiotapia/torrentinim/runs/912766993?check_suite_focus=true
trying to figure it out here, i'll post back if i solve it.
https://raw.githubusercontent.com/sergiotapia/torrentinim/master/.github/workflows/release_asset.yml
I had to modify the cp commands, but other than that, the action works flawlessly! Thanks again for sharing, huge time saver!
name: Check and update tag
on:
push:
branches:
- master
jobs:
tag:
if: github.actor == github.repository_owner
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: butlerlogic/[email protected]
with:
strategy: regex
root: "${{ github.event.repository.name }}.nimble"
regex_pattern: "version\\s*=\\s*\"([0-9\\.]+)\""
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
It will check if the version declared in the *.nimble file matches a tag and create it for you if it doesn't. Please be careful using this as I just started playing with actions and I'm not sure how it will behave eg if you get a pull request that bumps the version.
Thanks all.
I added cross-compilation example to Nim Wiki.
https://github.com/nim-lang/Nim/wiki/BuildServices#8-cross-compilation-and-release