Skip to content

Commit

Permalink
Merge pull request #13 from jnsgruk/promote-to-stable-parse-snap-name
Browse files Browse the repository at this point in the history
feat: add snapcraft-yaml-path options to other workflows
  • Loading branch information
jnsgruk authored Dec 5, 2023
2 parents 8f8a784 + 7e1ff68 commit 93d1199
Show file tree
Hide file tree
Showing 19 changed files with 332 additions and 109 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every week
interval: "monthly"
17 changes: 17 additions & 0 deletions .github/shellcheck-actions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -euo pipefail
info() { echo -e "\e[92m[+] $@\e[0m"; }
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

export SHELLCHECK_OPTS=(
"-s" "bash"
"-e" "2296"
"-e" "2157"
"-e" "2129"
"-e" "2154"
)

for f in "$DIR"/../**/*.yaml; do
info "Linting scripts in $f"
yq '.runs.steps[].run' "$f" | grep -v -P "^null$" | shellcheck "${SHELLCHECK_OPTS[@]}" -
done
37 changes: 37 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Lint

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
lint:
name: Lint
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4

- name: Run linters
run: |
make lint
- name: Run formatters
run: |
make format
- name: Check for modifications to generated files
run: |
if [[ -n "$(git status -s)" ]]; then
echo "Please run 'make format' then commit/push changes"
echo
git diff
exit 1
fi
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: help
help:
@echo "Usage:"
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'

.PHONY: lint
lint:
## lint: Lint the codebase with Prettier
npx prettier --print-width=99 --check .
bash ${CURDIR}/.github/shellcheck-actions.sh

.PHONY: format
format:
## format: Formats both Markdown documents and YAML documents to preferred repository style.
npx prettier --print-width=99 --write .
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,25 @@ You can see examples of these actions in use in the following repos:
- [signal-desktop](https://github.com/snapcrafters/signal-desktop/main/.github/workflows)
- [mattermost-desktop](https://github.com/snapcrafters/mattermost-desktop/main/.github/workflows)
- [discord](https://github.com/snapcrafters/discord/main/.github/workflows)

## Contributing

If you'd like to contribute to this repository, please feel free to fork and create a pull request.

There are a few style guidelines to keep in mind:

- Code should be linted using [Prettier](https://prettier.io/). You can achieve this with `make
lint` and `make format`. The only requirements are [`npx`](https://www.npmjs.com/package/npx) and
[`shellcheck`](https://github.com/koalaman/shellcheck).
- When defining inputs/outputs in `action.yaml`, or listing them in the tables within `README.md`,
they should be listed in alphabetical order for easy reading and updating.
- Github Action inputs/outputs should be named all lowercase, separated by `-` where needed. The
applies to inputs/outputs to actions themselves, and for individual steps within the actions. For
example: `snap-name` or `token`.
- Environment variables referring to repository level secrets and variables should be named all
uppercase, and separated by `_`. For example: `SNAPCRAFTERS_BOT_COMMIT`.
- Step/job level environment variables should be named all lowercase, and separated by `_`. For
example: `snap_name` or `yaml_path`.
- All `bash` variables should be quoted.
- Scripts of all kinds, including those within actions `run:|` directives should follow the [Google
styleguide](https://google.github.io/styleguide/shellguide.html)
15 changes: 8 additions & 7 deletions call-for-testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ jobs:

### Inputs

| Key | Description | Required | Default |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------: | :---------------- |
| `architectures` | The architectures that the snap supports. | Y | |
| `ci-repo` | The repo to fetch tools/templates from. Only for debugging. | N | `snapcrafters/ci` |
| `channel` | The channel to create the call for testing for. | N | `candidate` |
| `github-token` | A token with permissions to create issues on the repository. | Y | |
| `store-token` | A token with permissions to query the specified channel in the Snap Store. Only required if the revisions to test are not passed to the workflow by the `release-to-candidate` workflow | N | |
| Key | Description | Required | Default |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------: | :---------------- |
| `architectures` | The architectures that the snap supports. | Y | |
| `ci-repo` | The repo to fetch tools/templates from. Only for debugging. | N | `snapcrafters/ci` |
| `channel` | The channel to create the call for testing for. | N | `candidate` |
| `github-token` | A token with permissions to create issues on the repository. | Y | |
| `snapcraft-yaml-path` | The path to the `snapcraft.yaml` file. | N |
| `store-token` | A token with permissions to query the specified channel in the Snap Store. Only required if the revisions to test are not passed to the workflow by the `release-to-candidate` workflow | N | |

### Outputs

Expand Down
50 changes: 41 additions & 9 deletions call-for-testing/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
github-token:
description: "A token with permissions to create issues on the repository"
required: true
snapcraft-yaml-path:
description: "Custom path to snapcraft.yaml for when it is not in the default location."
required: false
store-token:
description: "A token with permissions to upload to the specified channel"
required: false
Expand All @@ -40,18 +43,46 @@ runs:
uses: actions/download-artifact@v3
with:
name: "manifests"

- name: Setup snapcraft
shell: bash
run: |
sudo snap install snapcraft --classic
- name: Find the snapcraft.yaml path
id: yaml-path
shell: bash
run: |
if [[ -n "${{ inputs.snapcraft-yaml-path }}" ]]; then
yaml_path="${{ inputs.snapcraft-yaml-path }}"
else
snapcraft_yaml_paths=(
"snap/snapcraft.yaml"
"snapcraft.yaml"
"build-aux/snap/snapcraft.yaml"
".snapcraft.yaml"
)
for file in "${snapcraft_yaml_paths[@]}"; do
if [[ -f "$file" ]]; then
yaml_path="$file"
fi
done
fi
if [[ -z "${yaml_path}" ]]; then
echo "No snapcraft.yaml found"
exit 1
fi
echo "yaml-path=${yaml_path}" >> "$GITHUB_OUTPUT"
echo "snap-name=$(yq -r '.name' "$yaml_path")" >> "$GITHUB_OUTPUT"
- name: Write the arch/rev table
shell: bash
id: build
env:
SNAPCRAFT_STORE_CREDENTIALS: ${{ inputs.store-token }}
SNAP_NAME: ${{ github.event.repository.name }}
snap_name: ${{ steps.yaml-path.outputs.snap-name }}
snapcraft_yaml: ${{ steps.yaml-path.outputs.yaml-path }}
run: |
revisions=()
Expand All @@ -63,10 +94,10 @@ runs:
echo "Found build manifests - populating template with revisions from the manifests"
# Iterate over the manifest files and write the table rows for each architecture
for file in $(ls manifest-*.yaml); do
for file in manifest-*.yaml; do
# Parse the arch and the revision
arch="$(cat "${file}" | yq -r '.architecture')"
rev="$(cat "${file}" | yq -r '.revision')"
arch="$(yq -r '.architecture' "${file}")"
rev="$(yq -r '.revision' "${file}")"
# Write the table row and add the revision to the list we're tracking
table="${table}<tr><td>${arch}</td><td>${rev}</td></tr>"
revisions+=("$rev")
Expand All @@ -75,8 +106,9 @@ runs:
echo "No build manifests found - populating template with information from the store"
# Otherwise, get the latest revision for each architecture in the release channel
# shellcheck disable=SC1083
for arch in ${{ inputs.architectures }}; do
rev="$(snapcraft list-revisions "${SNAP_NAME}" --arch "$arch" | grep "latest/${{ inputs.channel }}*" | head -n1 | cut -d' ' -f1)"
rev="$(snapcraft list-revisions "${snap_name}" --arch "$arch" | grep "latest/${{ inputs.channel }}*" | head -n1 | cut -d' ' -f1)"
revisions+=("$rev")
# Add a row to the HTML table
table="${table}<tr><td>${arch}</td><td>${rev}</td></tr>"
Expand All @@ -89,7 +121,7 @@ runs:
# Get a comma separated list of revisions
printf -v joined '%s,' "${revisions[@]}"
version="$(cat snap/snapcraft.yaml | yq -r '.version')"
version="$(yq -r '.version' "$snapcraft_yaml")"
echo "version=${version}" >> "$GITHUB_OUTPUT"
echo "revisions=${joined%,}" >> "$GITHUB_OUTPUT"
echo "table=${table}" >> "$GITHUB_OUTPUT"
Expand All @@ -98,13 +130,13 @@ runs:
shell: bash
run: |
wget -qO template.md "https://raw.githubusercontent.com/${{ inputs.ci-repo }}/main/call-for-testing/template.md"
- name: Create call for testing issue
uses: JasonEtco/create-an-issue@v2
id: issue
env:
GITHUB_TOKEN: ${{ inputs.github-token }}
snap_name: ${{ github.event.repository.name }}
snap_name: ${{ steps.yaml-path.outputs.snap-name }}
channel: ${{ inputs.channel }}
revisions: ${{ steps.build.outputs.revisions }}
table: ${{ steps.build.outputs.table }}
Expand Down
4 changes: 3 additions & 1 deletion get-architectures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ jobs:
### Inputs
None
| Key | Description | Required | Default |
| --------------------- | -------------------------------------- | :------: | :------ |
| `snapcraft-yaml-path` | The path to the `snapcraft.yaml` file. | N | |

### Outputs

Expand Down
44 changes: 39 additions & 5 deletions get-architectures/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ branding:
icon: code
color: orange

inputs:
snapcraft-yaml-path:
description: "Custom path to snapcraft.yaml for when it is not in the default location."
required: false

outputs:
architectures:
description: "A space-separated list of architectures supported by the snap"
Expand All @@ -19,20 +24,49 @@ runs:
- name: Checkout the source
uses: actions/checkout@v4

- name: Find the snapcraft.yaml path
id: yaml-path
shell: bash
run: |
if [[ -n "${{ inputs.snapcraft-yaml-path }}" ]]; then
yaml_path="${{ inputs.snapcraft-yaml-path }}"
else
snapcraft_yaml_paths=(
"snap/snapcraft.yaml"
"snapcraft.yaml"
"build-aux/snap/snapcraft.yaml"
".snapcraft.yaml"
)
for file in "${snapcraft_yaml_paths[@]}"; do
if [[ -f "$file" ]]; then
yaml_path="$file"
fi
done
fi
if [[ -z "${yaml_path}" ]]; then
echo "No snapcraft.yaml found"
exit 1
fi
echo "yaml-path=${yaml_path}" >> "$GITHUB_OUTPUT"
echo "snap-name=$(yq -r '.name' "$yaml_path")" >> "$GITHUB_OUTPUT"
- name: Compute architectures
id: architectures
shell: bash
env:
yaml_path: ${{ steps.yaml-path.outputs.yaml-path }}
run: |
# Get the list as a json array. E.g. ["amd64", "arm64"]
architectures_list="$(cat snap/snapcraft.yaml | yq -r -I=0 -o=json '[.architectures[]]')"
architectures_list="$(yq -r -I=0 -o=json '[.architectures[]]' | "$yaml_path")"
# Get the list as a space-separated string. E.g. "amd64" "arm64"
architectures="$(cat snap/snapcraft.yaml | yq -r -I=0 -o=csv '[.architectures[]]' | tr ',' ' ')"
architectures="$(yq -r -I=0 -o=csv '[.architectures[]]' "$yaml_path" | tr ',' ' ')"
# Handle the case where architectures is a list of objects
if echo "$architectures" | grep -q "build-on"; then
architectures_list="$(cat snap/snapcraft.yaml | yq -r -I=0 -o=json '[.architectures[]."build-on"]')"
architectures="$(cat snap/snapcraft.yaml | yq -r -I=0 -o=csv '[.architectures[]."build-on"]' | tr ',' ' ')"
architectures_list="$(yq -r -I=0 -o=json '[.architectures[]."build-on"]' "$yaml_path")"
architectures="$(yq -r -I=0 -o=csv '[.architectures[]."build-on"]' "$yaml_path" | tr ',' ' ')"
fi
echo "architectures_list=$architectures_list" >> "$GITHUB_OUTPUT"
Expand Down
17 changes: 9 additions & 8 deletions get-screenshots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ jobs:
### Inputs
| Key | Description | Required | Default |
| ------------------- | ------------------------------------------------------------------------------------------------------------------ | :------: | :---------------------------- |
| `issue-number` | The issue number to post the screenshots to. | Y | |
| `ci-repo` | The repo to fetch tools/templates from. Only for debugging. | N | `snapcrafters/ci` |
| `channel` | The channel to create the call for testing for. | N | `candidate` |
| `github-token` | A token with permissions to common on issues in the repository. | Y | |
| `screenshots-repo` | The repository where screenshots should be uploaded. | N | `snapcrafters/ci-screenshots` |
| `screenshots-token` | A token with permissions to commit screenshots to [ci-screenshots](https://github.com/snapcrafters/ci-screenshots) | Y | |
| Key | Description | Required | Default |
| --------------------- | ------------------------------------------------------------------------------------------------------------------ | :------: | :---------------------------- |
| `issue-number` | The issue number to post the screenshots to. | Y | |
| `ci-repo` | The repo to fetch tools/templates from. Only for debugging. | N | `snapcrafters/ci` |
| `channel` | The channel to create the call for testing for. | N | `candidate` |
| `github-token` | A token with permissions to common on issues in the repository. | Y | |
| `screenshots-repo` | The repository where screenshots should be uploaded. | N | `snapcrafters/ci-screenshots` |
| `screenshots-token` | A token with permissions to commit screenshots to [ci-screenshots](https://github.com/snapcrafters/ci-screenshots) | Y | |
| `snapcraft-yaml-path` | The path to the `snapcraft.yaml` file. | N |

### Outputs

Expand Down
Loading

0 comments on commit 93d1199

Please sign in to comment.