diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8aeda38..c051217 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,8 @@ on: paths: - config/** - spack.yaml +env: + SPACK_YAML_MODEL_YQ: .spack.specs[0] jobs: validate-json: name: Validate JSON @@ -14,50 +16,135 @@ jobs: with: src: "config" - check-json: - name: Check JSON Fields + check-config: + name: Check Config Fields needs: - validate-json runs-on: ubuntu-latest outputs: - spack-packages-version: ${{ steps.versions.outputs.packages }} - spack-config-version: ${{ steps.versions.outputs.config }} + spack-packages-version: ${{ steps.spack-packages.outputs.version }} + spack-config-version: ${{ steps.spack-config.outputs.version }} + steps: + - name: Validate spack-packages version + id: spack-packages + uses: access-nri/build-cd/.github/actions/validate-repo-version@main + with: + repo-to-check: spack-packages + + - name: Validate spack-config version + id: spack-config + uses: access-nri/build-cd/.github/actions/validate-repo-version@main + with: + repo-to-check: spack-config + + check-spack-yaml: + name: Check spack.yaml + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - # The next steps checkout the spack-{packages,config} repos to confirm that the versions in - # versions.json exist in the repositories. - - name: Setup - id: versions + - name: Check Model Version Modified + id: version run: | - echo "packages=$(jq --compact-output --raw-output '."spack-packages"' ./config/versions.json)" >> $GITHUB_OUTPUT - echo "config=$(jq --compact-output --raw-output '."spack-config"' ./config/versions.json)" >> $GITHUB_OUTPUT + git checkout ${{ github.base_ref }} + base_version=$(yq e '${{ env.SPACK_YAML_MODEL_YQ }}' spack.yaml) - - name: Spack Packages - id: spack-packages - continue-on-error: true - uses: actions/checkout@v4 + git checkout ${{ github.head_ref }} + current_version=$(yq e '${{ env.SPACK_YAML_MODEL_YQ }}' spack.yaml) + echo "current=${current_version}" >> $GITHUB_OUTPUT + + if [[ "${base_version}" == "${current_version}" ]]; then + echo "::warning::The version string hasn't been modified in this PR, but needs to be before merging." + exit 1 + fi + + - name: Same Model Version Failure Notifier + if: failure() && steps.version.outcome == 'failure' + uses: access-nri/actions/.github/actions/pr-comment@main with: - repository: access-nri/spack-packages - ref: ${{ steps.versions.outputs.packages }} - path: packages + comment: | + The model version in the `spack.yaml` has not been updated. + Either update it manually, or comment the following to have it updated and committed automatically: + * `!bump major` for feature releases + * `!bump minor` for bugfixes - - name: Spack Config - id: spack-config - continue-on-error: true - uses: actions/checkout@v4 + - name: More Checks + # FIXME: More spack.yaml checks specific to access-om3 + run: echo "::warning::Stub for more access-om3 specific checks" + + version-tag: + name: Get Version and Tag Prerelease + needs: + - check-spack-yaml + runs-on: ubuntu-latest + permissions: + contents: write + outputs: + release: ${{ steps.version.outputs.release }} + prerelease: ${{ steps.version.outputs.prerelease }} + steps: + - uses: actions/checkout@v4 with: - repository: access-nri/spack-config - ref: ${{ steps.versions.outputs.config }} - path: config + ref: ${{ github.head_ref }} + fetch-depth: 0 - - name: Failure Notifier - if: contains(steps.*.outcome, 'failure') + - name: Generate Versions + id: version + # This step generates the release and prerelease version numbers. + # The release is a general version number from the spack.yaml, looking the + # same as a regular release build. Ex. 'access-om2@git.2024.01.1' -> '2024.01.1' + # The prerelease looks like: `pr-`. + # Ex. Pull Request #12 with 2 commits on branch -> `pr12-2`. run: | - if [[ "${{ steps.spack-packages.outcome }}" == "failure" ]]; then - echo "::error::spack-packages at the specified ref (${{ steps.versions.outputs.packages }}) doesn't exist." - fi - if [[ "${{ steps.spack-config.outcome }}" == "failure" ]]; then - echo "::error::spack-config at the specified ref (${{ steps.versions.outputs.config }}) doesn't exist." - fi - exit 1 \ No newline at end of file + echo "release=$(yq '${{ env.SPACK_YAML_MODEL_YQ }} | split("@git.") | .[1]' spack.yaml)" >> $GITHUB_OUTPUT + + number_of_commits=$(git rev-list --count ${{ github.event.pull_request.base.sha }}..HEAD) + echo "prerelease=pr${{ github.event.pull_request.number }}-${number_of_commits}" >> $GITHUB_OUTPUT + + - name: Shift Prerelease Tag ${{ steps.version.outputs.release }} + # We shift the 'Release' tag along the PR as the spack.yaml will not work without the correct tag in this repostiory. + # NOTE: Regarding the config user.name/user.email, see https://github.com/actions/checkout/pull/1184 + run: | + git config user.name ${{ vars.GH_ACTIONS_BOT_GIT_USER_NAME }} + git config user.email ${{ vars.GH_ACTIONS_BOT_GIT_USER_EMAIL }} + git tag ${{ steps.version.outputs.release }} --force + git push --tags --force + + # ----------------------------- + # | PRERELEASE DEPLOYMENT JOB | + # ----------------------------- + prerelease-deploy: + name: Deploy to Prerelease + # This will create a `spack` environment with the name `access-om3-pr-`. + # For example, `access-om3-pr13-3` for the deployment based on the third commit on the PR#13. + needs: + - version-tag # implies all the spack.yaml-related checks have passed, has appropriate version for the prerelease build + - check-config # implies all the json-related checks have passed + uses: access-nri/build-cd/.github/workflows/deploy-1-setup.yml@main + with: + type: prerelease + ref: ${{ github.head_ref }} + version: ${{ needs.version-tag.outputs.prerelease }} + secrets: inherit + + notifier: + name: Notifier + needs: + - version-tag # implies all the spack.yaml-related checks have passed, has appropriate version for the prerelease build + - check-config # implies all the json-related checks have passed + permissions: + pull-requests: write + uses: access-nri/actions/.github/actions/pr-comment@main + with: + comment: | + This `${{ github.repository }}` model will be deployed with the following versions: + * `${{ needs.version-tag.outputs.release }}` as a Release (when merged). + * `${{ needs.version-tag.outputs.prerelease }}` as a Prerelease (during this PR). This can be accessed on `Gadi` via `spack` at `/g/data/vk83/prerelease/apps/spack/0.20/spack` once deployed. + + It will be deployed using: + * `access-nri/spack-packages` version [`${{ needs.check-config.outputs.spack-packages-version }}`](https://github.com/ACCESS-NRI/spack-packages/releases/tag/${{ needs.check-config.outputs.spack-packages-version }}) + * `access-nri/spack-config` version [`${{ needs.check-config.outputs.spack-config-version }}`](https://github.com/ACCESS-NRI/spack-config/releases/tag/${{ needs.check-config.outputs.spack-config-version }}) + + If this is not what was expected, commit changes to `config/versions.json`.