Skip to content

Commit

Permalink
Add CI workflow (#1)
Browse files Browse the repository at this point in the history
* Add CI test using `dry-run` input

Tests a sample `stack.yaml` that resolves to the default LTS Haskell snapshot. This will mitigate the likelihood of regressions when updating this action to use a different snapshot repository.

The action is split up to avoid building with an updated stack.yaml, since this would require a more elaborate test case. However, committing the result is useful to test and doesn't require any additional scaffolding.

* Restyled by prettier-yaml (#2)

Co-authored-by: Restyled.io <[email protected]>

* Create valid test package

* Point build to stack-yaml input

* Consolidate steps and remove dry-run input

* Validate no build errors

* Remove deprecated set-output command

* Validate commit message

* Fix test variable

* Use delimiter for multiline output

A randomized delimiter is used for security reasons.

See: https://github.com/orgs/community/discussions/26288#discussioncomment-3876281

* Fix `message` output

* Test other action outputs

---------

Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored Jul 13, 2023
1 parent 01c1d27 commit 72e75a5
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 24 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI

on:
pull_request:
push:
branches: main

jobs:
test-lts:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run action on stack.yaml resolving to LTS Haskell snapshot
id: test
uses: ./
with:
working-directory: test/lts-example
- name: Validate action outputs
run: |
current=${{ steps.test.outputs.current }}
latest=${{ steps.test.outputs.latest }}
newer_available=${{ steps.test.outputs.newer-available }}
build_errors=${{ steps.test.outputs.build-errors }}
commit_message="${{ steps.test.outputs.commit-message }}"
if [[ "$current" != "lts-20.20" ]]; then
echo "Found unexpected current LTS version:"
echo "$current"
exit 1
fi
if [[ -z "$latest" ]]; then
echo "Action failed to find latest LTS version"
exit 1
fi
if [[ "$newer_available" != "true" ]]; then
echo "Failed to find newer resolver"
exit 1
fi
if [[ "$build_errors" == "true" ]]; then
echo "Failed to build with updated resolver"
exit 1
fi
if ! echo "$commit_message" | grep -q "Bump Stackage Resolver to" ; then
echo "Malformed commit message:"
echo "$commit_message"
exit 1
fi
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.stack-work
53 changes: 29 additions & 24 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ inputs:
actor:
description: "Login to use instead of github.actor"
required: true
default: ''
default: ""

outputs:
path:
Expand All @@ -32,7 +32,7 @@ outputs:
value: ${{ steps.setup.outputs.latest }}
newer-available:
description: "True if we found a newer resolver"
value: ${{ steps.commit.outputs.newer-available }}
value: ${{ steps.update.outputs.newer-available }}
build-errors:
description: "True if stack was not able to build"
value: ${{ steps.commit.outputs.build-errors }}
Expand All @@ -53,36 +53,50 @@ runs:
else
path=${{ inputs.stack-yaml }}
fi
echo "::set-output name=path::$path"
echo "path=$path" >>"$GITHUB_OUTPUT"
current=$(sed '/^resolver: \(.*\)$/!d; s//\1/' "$path")
echo "::set-output name=current::$current"
echo "current=$current" >>"$GITHUB_OUTPUT"
latest=$(
curl --silent -D - https://www.stackage.org/lts |
sed '/^location: \/\(.*\)/!d; s//\1/;' |
tr '\r\n' '\n'
)
echo "::set-output name=latest::$latest"
echo "latest=$latest" >>"$GITHUB_OUTPUT"
- name: Update resolver and commit
id: commit
- name: Update resolver
id: update
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
current=${{ steps.setup.outputs.current }}
latest=${{ steps.setup.outputs.latest }}
stack_yaml=${{ inputs.stack-yaml }}
stack_yaml_lock=${{ inputs.stack-yaml }}.lock
path=${{ steps.setup.outputs.path }}
if [[ "$current" == "$latest" ]]; then
echo "Up to date ($current)"
echo "::set-output name=newer-available::false"
echo "newer-available=false" >>"$GITHUB_OUTPUT"
exit 0
fi
echo "::set-output name=newer-available::true"
echo "newer-available=true" >>"$GITHUB_OUTPUT"
sed -i 's/^resolver: .*$/resolver: '"$latest"'/' "$path"
- if: ${{ inputs.dry-run != 'true' && steps.update.outputs.newer-available == 'true' }}
name: Build and commit
id: commit
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
current=${{ steps.setup.outputs.current }}
latest=${{ steps.setup.outputs.latest }}
stack_yaml=${{ inputs.stack-yaml }}
stack_yaml_lock=${{ inputs.stack-yaml }}.lock
path=${{ steps.setup.outputs.path }}
if ! stack build --dry-run --test --bench --stack-yaml "$stack_yaml" ; then
echo "build-errors=true" >>"$GITHUB_OUTPUT"
fi
commit_message() {
cat <<EOM
Expand All @@ -95,22 +109,13 @@ runs:
EOM
}
sed -i 's/^resolver: .*$/resolver: '"$latest"'/' "$path"
if ! stack build --dry-run --test --bench; then
echo "::set-output name=build-errors::true"
fi
echo "::set-output name=build-errors::false"
actor=${{ inputs.actor }}
actor=${actor:-${{ github.actor }}}
git config --global user.email "[email protected]"
git config --global user.name "$actor"
git commit "$stack_yaml" "$stack_yaml_lock" "$path" --file <(commit_message)
# https://github.community/t/set-output-truncates-multiline-strings/16852/5
output_message=$(commit_message)
output_message="${output_message//'%'/'%25'}"
output_message="${output_message//$'\n'/'%0A'}"
output_message="${output_message//$'\r'/'%0D'}"
echo "::set-output name=message::$output_message"
delimiter="$(openssl rand -hex 8)"
echo "message<<${delimiter}" >> $GITHUB_OUTPUT
commit_message >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
9 changes: 9 additions & 0 deletions test/lts-example/lts-example.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.1.
--
-- see: https://github.com/sol/hpack

name: lts-example
version: 0.1.0.0
build-type: Simple
2 changes: 2 additions & 0 deletions test/lts-example/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: lts-example
version: 0.1.0.0
2 changes: 2 additions & 0 deletions test/lts-example/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Resolver to an earlier LTS Haskell snapshot
resolver: lts-20.20
12 changes: 12 additions & 0 deletions test/lts-example/stack.yaml.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files

packages: []
snapshots:
- completed:
sha256: 126fa33ceb11f5e85ceb4e86d434756bd9a8439e2e5776d306a15fbc63b01e89
size: 650041
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/20.yaml
original: lts-20.20

0 comments on commit 72e75a5

Please sign in to comment.