|
| 1 | +--- |
| 2 | +name: Features |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + paths: |
| 9 | + - features/** |
| 10 | + push: |
| 11 | + branches: |
| 12 | + - main |
| 13 | + paths: |
| 14 | + - features/** |
| 15 | + |
| 16 | +permissions: {} |
| 17 | + |
| 18 | +jobs: |
| 19 | + detect-changes: |
| 20 | + name: Detect Changes |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + outputs: |
| 25 | + features: ${{ steps.detect_changes.outputs.changes }} |
| 26 | + steps: |
| 27 | + - name: Checkout |
| 28 | + id: checkout |
| 29 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 30 | + |
| 31 | + - name: Build path-filters file |
| 32 | + id: build_path_filters |
| 33 | + run: bash scripts/path-filter/configuration-generator.sh features |
| 34 | + |
| 35 | + - name: Detect changes |
| 36 | + id: detect_changes |
| 37 | + uses: dorny/paths-filter@0bc4621a3135347011ad047f9ecf449bf72ce2bd # v3.0.0 |
| 38 | + with: |
| 39 | + filters: .github/path-filters/features.yml |
| 40 | + |
| 41 | + preflight-checks: |
| 42 | + needs: [detect-changes] |
| 43 | + if: ${{ needs.detect-changes.outputs.features != '[]' && github.ref != 'refs/heads/main' }} |
| 44 | + name: Preflight Checks |
| 45 | + runs-on: ubuntu-latest |
| 46 | + permissions: |
| 47 | + contents: read |
| 48 | + id-token: write |
| 49 | + packages: read |
| 50 | + strategy: |
| 51 | + fail-fast: false |
| 52 | + matrix: |
| 53 | + feature: ${{ fromJson(needs.detect-changes.outputs.features) }} |
| 54 | + steps: |
| 55 | + - name: Checkout |
| 56 | + id: checkout |
| 57 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 58 | + |
| 59 | + - name: Check Version |
| 60 | + id: check_version |
| 61 | + env: |
| 62 | + GH_TOKEN: ${{ github.token }} |
| 63 | + run: | |
| 64 | + echo "${{ env.GH_TOKEN }}" | skopeo login ghcr.io --username ${{ github.actor }} --password-stdin |
| 65 | +
|
| 66 | + version=$(jq -r '.version' features/src/${{ matrix.feature }}/devcontainer-feature.json) |
| 67 | + export version |
| 68 | +
|
| 69 | + if skopeo list-tags docker://ghcr.io/ministryofjustice/devcontainer-feature/${{ matrix.feature }}; then |
| 70 | + feature_exists=true |
| 71 | + else |
| 72 | + echo "feature_exists=false" >>"${GITHUB_ENV}" |
| 73 | + echo "tag_exists=false" >>"${GITHUB_ENV}" |
| 74 | + fi |
| 75 | +
|
| 76 | + if [[ "${feature_exists}" == "true" ]]; then |
| 77 | + checkTag=$(skopeo list-tags docker://ghcr.io/ministryofjustice/devcontainer-feature/${{ matrix.feature }} | jq -r --arg version "${version}" '.Tags | index($version)') |
| 78 | + export checkTag |
| 79 | +
|
| 80 | + if [[ -z "${imageTag}" ]]; then |
| 81 | + echo "tag_exists=false" >>"${GITHUB_ENV}" |
| 82 | + else |
| 83 | + echo "tag_exists=true" >>"${GITHUB_ENV}" |
| 84 | + fi |
| 85 | + fi |
| 86 | +
|
| 87 | + - name: Check CHANGELOG Updates |
| 88 | + id: check_changelog_updates |
| 89 | + env: |
| 90 | + GH_TOKEN: ${{ github.token }} |
| 91 | + run: | |
| 92 | + mainSha=$(gh api --method GET /repos/"${GITHUB_REPOSITORY}"/contents/features/src/${{ matrix.feature }}/CHANGELOG.md --field ref="main" | jq -r '.sha') |
| 93 | + branchSha=$(gh api --method GET /repos/"${GITHUB_REPOSITORY}"/contents/features/src/${{ matrix.feature }}/CHANGELOG.md --field ref="${GITHUB_HEAD_REF}" | jq -r '.sha') |
| 94 | +
|
| 95 | + if [[ -z "${mainSha}" ]]; then |
| 96 | + SHA not found for main branch, assuming CHANGELOG.md does not exist |
| 97 | + elif [[ -z "${branchSha}" ]]; then |
| 98 | + SHA not found for "${GITHUB_HEAD_REF}" branch, assuming CHANGELOG.md does not exist |
| 99 | + "changelog_updated=false" >>"${GITHUB_ENV}" |
| 100 | + elif [[ "${mainSha}" == "${branchSha}" ]]; then |
| 101 | + echo "CHANGELOG.md matches main branch, needs to be updated" |
| 102 | + echo "changelog_updated=false" >>"${GITHUB_ENV}" |
| 103 | + elif [[ "${mainSha}" != "${branchSha}" ]]; then |
| 104 | + echo "CHANGELOG.md does not match main branch, does not need to be updated" |
| 105 | + echo "changelog_updated=true" >>"${GITHUB_ENV}" |
| 106 | + fi |
| 107 | +
|
| 108 | + - name: Evaluate Checks |
| 109 | + id: evaluate_checks |
| 110 | + run: | |
| 111 | + if [[ "${{ env.tag_exists }}" == "true" ]]; then |
| 112 | + echo "::error::FAIL: Feature tag already exists" |
| 113 | + export failBuild="true" |
| 114 | + else |
| 115 | + echo "::notice::OK: Feature tag does not exist" |
| 116 | + export failBuild="false" |
| 117 | + fi |
| 118 | +
|
| 119 | + if [[ "${{ env.changelog_updated }}" == "true" ]]; then |
| 120 | + echo "::notice::OK: CHANGELOG.md has been updated" |
| 121 | + export failBuild="false" |
| 122 | + elif [[ "${{ env.changelog_updated }}" == "false" ]]; then |
| 123 | + echo "::error::FAIL: CHANGELOG.md needs to be updated" |
| 124 | + export failBuild="true" |
| 125 | + fi |
| 126 | +
|
| 127 | + if [[ "${failBuild}" == "true" ]]; then |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | +
|
| 131 | + test: |
| 132 | + needs: [detect-changes] |
| 133 | + if: ${{ needs.detect-changes.outputs.features != '[]' && github.ref != 'refs/heads/main' }} |
| 134 | + name: Test |
| 135 | + runs-on: ubuntu-latest |
| 136 | + permissions: |
| 137 | + contents: read |
| 138 | + strategy: |
| 139 | + fail-fast: false |
| 140 | + matrix: |
| 141 | + feature: ${{ fromJson(needs.detect-changes.outputs.features) }} |
| 142 | + steps: |
| 143 | + - name: Checkout |
| 144 | + id: checkout |
| 145 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 146 | + |
| 147 | + - name: Install @devcontainers/cli |
| 148 | + id: install_devcontainers_cli |
| 149 | + run: npm install --global @devcontainers/cli@latest |
| 150 | + |
| 151 | + - name: Build Image |
| 152 | + id: build_image |
| 153 | + uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 # v5.1.0 |
| 154 | + with: |
| 155 | + file: images/base/Dockerfile |
| 156 | + context: images/base |
| 157 | + load: true |
| 158 | + tags: ghcr.io/ministryofjustice/devcontainer-base:local-feature-test-${{ github.sha }} |
| 159 | + |
| 160 | + - name: Testing ${{ matrix.feature }} |
| 161 | + id: test_feature |
| 162 | + run: | |
| 163 | + devcontainer features test \ |
| 164 | + --skip-duplicated \ |
| 165 | + --skip-scenarios \ |
| 166 | + --project-folder features \ |
| 167 | + --features ${{ matrix.feature }} \ |
| 168 | + --base-image ghcr.io/ministryofjustice/devcontainer-base:local-feature-test-${{ github.sha }} |
| 169 | +
|
| 170 | + - name: Testing ${{ matrix.feature }} scenarios |
| 171 | + id: test_feature_scenarios |
| 172 | + run: | |
| 173 | + devcontainer features test \ |
| 174 | + --project-folder features \ |
| 175 | + --features ${{ matrix.feature }} \ |
| 176 | + --skip-autogenerated \ |
| 177 | + --skip-duplicated \ |
| 178 | + --base-image ghcr.io/ministryofjustice/devcontainer-base:local-feature-test-${{ github.sha }} |
| 179 | +
|
| 180 | + validate: |
| 181 | + needs: [detect-changes] |
| 182 | + if: ${{ needs.detect-changes.outputs.features != '[]' && github.ref != 'refs/heads/main' }} |
| 183 | + name: Validate |
| 184 | + runs-on: ubuntu-latest |
| 185 | + permissions: |
| 186 | + contents: read |
| 187 | + steps: |
| 188 | + - name: Checkout |
| 189 | + id: checkout |
| 190 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 191 | + |
| 192 | + - name: Validate |
| 193 | + uses: devcontainers/action@2858bb873d82d653b033bfe0977fb8a16708b0c3 # v1.4.1 |
| 194 | + with: |
| 195 | + validate-only: true |
| 196 | + base-path-to-features: features/src |
| 197 | + |
| 198 | + publish: |
| 199 | + needs: [detect-changes] |
| 200 | + if: ${{ needs.detect-changes.outputs.features != '[]' && github.ref == 'refs/heads/main' }} |
| 201 | + name: Publish |
| 202 | + runs-on: ubuntu-latest |
| 203 | + permissions: |
| 204 | + contents: read |
| 205 | + id-token: write |
| 206 | + packages: write |
| 207 | + strategy: |
| 208 | + fail-fast: false |
| 209 | + matrix: |
| 210 | + feature: ${{ fromJson(needs.detect-changes.outputs.features) }} |
| 211 | + steps: |
| 212 | + - name: Checkout |
| 213 | + id: checkout |
| 214 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 215 | + |
| 216 | + - name: Install @devcontainers/cli |
| 217 | + id: install_devcontainers_cli |
| 218 | + run: npm install --global @devcontainers/cli@latest |
| 219 | + |
| 220 | + - name: Publish |
| 221 | + id: publish |
| 222 | + run: | |
| 223 | + devcontainer features publish \ |
| 224 | + --registry ghcr.io \ |
| 225 | + --namespace ministryofjustice/devcontainer-feature \ |
| 226 | + ./features/src/${{ matrix.feature }} |
0 commit comments