Skip to content

Commit

Permalink
feat: Refactor GitHub Actions workflow for build and release
Browse files Browse the repository at this point in the history
This commit includes changes to the GitHub Actions workflow that handles building and releasing. The changes include:

- Removal of redundant outputs
- Addition of steps to get artifact JSON, define build matrix, and refactor build process
- Changes in condition checks for artifact existence during various steps
- Use of a dynamic matrix strategy for running jobs on different OS platforms
- Improved handling of artifacts by checking if they already exist before proceeding with the build process
  • Loading branch information
rrrodzilla committed Jul 24, 2024
1 parent ea5a6c6 commit c9536ba
Showing 1 changed file with 68 additions and 62 deletions.
130 changes: 68 additions & 62 deletions .github/workflows/release_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
runs-on: ubuntu-latest
outputs:
cargo_version: ${{ steps.extract.outputs.cargo_version }}
workflow_run_id: ${{ steps.set-workflow-run-id.outputs.workflow_run_id }}
steps:
- uses: actions/checkout@v4
- name: Extract version from Cargo.toml
Expand All @@ -24,114 +23,121 @@ jobs:
echo "Error: Version is blank."
exit 1
fi
echo "cargo_version=$version" >> $GITHUB_ENV
build:
name: Build and Package
runs-on: ${{ matrix.os }}
echo "### Release Build: v$version" >> $GITHUB_STEP_SUMMARY
echo "cargo_version=$version" >> $GITHUB_OUTPUT
get-artifact-json:
name: Get Artifact JSON
runs-on: ubuntu-latest
outputs:
workflow_run_id: ${{ steps.set-workflow-run-id.outputs.workflow_run_id }}
needs: extract-version
env:
CARGO_VERSION: ${{ needs.extract-version.outputs.cargo_version }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]

artifact_json: ${{ steps.get-json.outputs.artifact_json }}
steps:
- name: Fail if version is blank
run: |
if [ -z "$CARGO_VERSION" ]; then
echo "Error: CARGO_VERSION is blank."
exit 1
fi
echo "Cargo version is $CARGO_VERSION"
shell: bash
if: runner.os != 'Windows'
- name: Fail if version is blank
- id: get-json
run: |
if (-not $env:CARGO_VERSION) {
Write-Error "Error: CARGO_VERSION is blank."
exit 1
}
Write-Host "Cargo version is $env:CARGO_VERSION"
shell: pwsh
if: runner.os == 'Windows'
response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/actions/artifacts")
echo "artifact_json=$response" >> $GITHUB_OUTPUT
define-build-matrix:
name: Define Build Matrix
runs-on: ubuntu-latest
needs: [extract-version, get-artifact-json]

outputs:
matrix: ${{ steps.set-matrix.outputs.build_matrix }}

- name: Determine artifact name
id: set-artifact-name
steps:
- name: Set matrix
id: set-matrix
run: |
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
echo "artifact_name=paseto_cli-linux-x64" >> $GITHUB_ENV
elif [[ "${{ matrix.os }}" == "macos-latest" ]]; then
echo "artifact_name=paseto_cli-darwin" >> $GITHUB_ENV
elif [[ "${{ matrix.os }}" == "windows-latest" ]]; then
echo "artifact_name=paseto_cli-win32-x64" >> $GITHUB_ENV
response = ${{ needs.get-artifact-json.outputs.artifact_json }}
artifact_name="paseto_cli-linux-x64"
artifact_info=$(echo "$response" | jq -r '.artifacts[] | select(.name=="'"$artifact_name"'")')
if [ -n "$artifact_info" ]; then
workflow_run_id=$(echo "$artifact_info" | jq -r '.workflow_run.id')
echo "Artifact $artifact_name already exists from workflow id: $workflow_run_id."
echo 'linux-x64={os: "ubuntu-latest",exists:true, run_id:"$workflow_run_id", artifact:"$artifact_name"}' >> $GITHUB_ENV
else
echo "Artifact does not exist."
echo 'linux-x64={os: "ubuntu-latest",exists:false, run_id:"$workflow_run_id", artifact:"$artifact_name"}' >> $GITHUB_ENV
fi

artifact_name="paseto_cli-darwin"
artifact_info=$(echo "$response" | jq -r '.artifacts[] | select(.name=="'"$artifact_name"'")')
if [ -n "$artifact_info" ]; then
workflow_run_id=$(echo "$artifact_info" | jq -r '.workflow_run.id')
echo "Artifact $artifact_name already exists from workflow id: $workflow_run_id."
echo 'darwin={os: "macos-latest",exists:true, run_id:"$workflow_run_id", artifact:"$artifact_name"}' >> $GITHUB_ENV
else
echo "Artifact does not exist."
echo 'darwin={os: "macos-latest",exists:false, run_id:"$workflow_run_id", artifact:"$artifact_name"}' >> $GITHUB_ENV
fi
shell: bash

- name: Check if artifact exists and get download URL
id: check-artifact
continue-on-error: true
run: |
artifact_name="${{ env.artifact_name }}"
response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/actions/artifacts")
artifact_name="paseto_cli-win32-x64"
artifact_info=$(echo "$response" | jq -r '.artifacts[] | select(.name=="'"$artifact_name"'")')
if [ -n "$artifact_info" ]; then
download_url=$(echo "$artifact_info" | jq -r '.archive_download_url')
workflow_run_id=$(echo "$artifact_info" | jq -r '.workflow_run.id')
echo "Artifact $artifact_name already exists from workflow id: $workflow_run_id."
echo "artifact-exists=true" >> $GITHUB_ENV
echo "workflow_run_id=$workflow_run_id" >> $GITHUB_ENV
echo 'win32-x64={os: "windows-latest",exists:true, run_id:"$workflow_run_id", artifact:"$artifact_name"}' >> $GITHUB_ENV
else
echo "Artifact does not exist."
echo "artifact-exists=false" >> $GITHUB_ENV
echo "workflow_run_id=${{ github.run_id }}" >> $GITHUB_ENV
echo 'win32-x64={os: "windows-latest",exists:false, run_id:"$workflow_run_id", artifact:"$artifact_name"}' >> $GITHUB_ENV
fi

shell: bash
echo "build_matrix.${{ matrix.arch }}=" >> $GITHUB_ENV
echo "build_matrix='version:${{ needs.extract-version.outputs.cargo_version }},target:[${{ env.linux-x64 }},${{ env.darwin }},${{ env.win32-x64 }}]'" >> $GITHUB_OUTPUT

build:
name: Build and Package
runs-on: ${{ matrix.target.os }}
needs: define-build-matrix
strategy:
matrix:
target: ${{ fromJSON(needs.define-build-matrix.outputs.build_matrix) }}

steps:
- name: Checkout code
if: env.artifact-exists == 'false'
if: matrix.target.exists == 'false'
uses: actions/checkout@v4
- name: Install Rust
if: env.artifact-exists == 'false'
if: matrix.target.exists == 'false'
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.os == 'ubuntu-latest' && 'x86_64-unknown-linux-gnu' || matrix.os == 'macos-latest' && 'x86_64-apple-darwin' || 'x86_64-pc-windows-msvc' }}
target: ${{ matrix.target.os == 'ubuntu-latest' && 'x86_64-unknown-linux-gnu' || matrix.target.os == 'macos-latest' && 'x86_64-apple-darwin' || 'x86_64-pc-windows-msvc' }}
override: true

- name: Build
if: env.artifact-exists == 'false'
if: matrix.target.exists == 'false'
uses: actions-rs/cargo@v1
with:
command: build
args: --release --target ${{ matrix.os == 'ubuntu-latest' && 'x86_64-unknown-linux-gnu' || matrix.os == 'macos-latest' && 'x86_64-apple-darwin' || 'x86_64-pc-windows-msvc' }}
args: --release --target ${{ matrix.target.os == 'ubuntu-latest' && 'x86_64-unknown-linux-gnu' || matrix.target.os == 'macos-latest' && 'x86_64-apple-darwin' || 'x86_64-pc-windows-msvc' }}

- name: Package Binary
if: env.artifact-exists == 'false'
if: matrix.target.exists == 'false'
run: |
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
if [[ "${{ matrix.target.os }}" == "ubuntu-latest" ]]; then
cd target/x86_64-unknown-linux-gnu/release
tar -czf paseto_cli-linux-x64.tar.gz paseto_cli
mv paseto_cli-linux-x64.tar.gz ../../../
elif [[ "${{ matrix.os }}" == "macos-latest" ]]; then
elif [[ "${{ matrix.target.os }}" == "macos-latest" ]]; then
cd target/x86_64-apple-darwin/release
tar -czf paseto_cli-darwin.tar.gz paseto_cli
mv paseto_cli-darwin.tar.gz ../../../
elif [[ "${{ matrix.os }}" == "windows-latest" ]]; then
elif [[ "${{ matrix.target.os }}" == "windows-latest" ]]; then
cd target/x86_64-pc-windows-msvc/release
7z a -r paseto_cli-win32-x64.zip paseto_cli.exe
powershell -command "Move-Item -Path paseto_cli-win32-x64.zip -Destination ../../../"
fi
shell: bash

- name: Upload Binary
if: env.artifact-exists == 'false'
if: matrix.target.exists == 'false'
uses: actions/upload-artifact@v4
with:
name: ${{ env.artifact_name }}
path: ${{ matrix.os == 'ubuntu-latest' && 'paseto_cli-linux-x64.tar.gz' || matrix.os == 'macos-latest' && 'paseto_cli-darwin.tar.gz' || 'paseto_cli-win32-x64.zip' }}
name: ${{ matrix.target.artifact_name }}
path: ${{ matrix.target.os == 'ubuntu-latest' && 'paseto_cli-linux-x64.tar.gz' || matrix.target.os == 'macos-latest' && 'paseto_cli-darwin.tar.gz' || 'paseto_cli-win32-x64.zip' }}
compression-level: 9
create-release:
needs: [ build, extract-version ]
Expand Down

0 comments on commit c9536ba

Please sign in to comment.