diff --git a/.github/workflows/commits.yml b/.github/workflows/commits.yml index 0a136b4..0ce3b32 100644 --- a/.github/workflows/commits.yml +++ b/.github/workflows/commits.yml @@ -5,7 +5,8 @@ name: Build simple commit on: push: - branches: ['*'] + # the staging branch is built by staging.yml + branches-ignore: [staging] tags-ignore: [ 'v*.*.*' ] jobs: diff --git a/.github/workflows/reuse-build.yml b/.github/workflows/reuse-build.yml index 526ffc0..f248e38 100644 --- a/.github/workflows/reuse-build.yml +++ b/.github/workflows/reuse-build.yml @@ -2,6 +2,19 @@ name: Build on: workflow_call: + inputs: + ref: + description: Git ref to build (default the ref that triggered the workflow) + type: string + default: '' + tag: + description: Single image tag replacing the tags derived from the ref (e.g. staging) + type: string + default: '' + pull-requests: + description: JSON object merged into BUILD.json (baseCommit, pullRequests, excludedPullRequests of a staging build) + type: string + default: '' jobs: build: @@ -9,6 +22,8 @@ jobs: steps: - name: Checkout git repository uses: actions/checkout@v7 + with: + ref: ${{ inputs.ref }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4 @@ -20,9 +35,16 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + # BUILD.json is added in the last layer of the Dockerfile: a build whose code is unchanged + # (a staging branch with no pull request) only rebuilds that layer - name: Prepare BUILD.json + env: + VERSION: ${{ inputs.tag || github.ref_name }} + REPOSITORY: ${{ github.server_url }}/${{ github.repository }} + EXTRA: ${{ inputs.pull-requests || '{}' }} run: | - echo '{"version": "${{github.ref_name}}", "repository": "${{github.server_url}}/${{github.repository}}", "commit": "${{github.sha}}", "date": "'$(date -Is)'"}' > BUILD.json + jq -n --arg version "$VERSION" --arg repository "$REPOSITORY" --arg commit "$(git rev-parse HEAD)" --arg date "$(date -Is)" --argjson extra "$EXTRA" \ + '{version: $version, repository: $repository, commit: $commit, date: $date} + $extra' > BUILD.json cat BUILD.json - name: Prepare docker tags for main image @@ -31,11 +53,12 @@ jobs: with: images: ghcr.io/${{ github.repository }} tags: | - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - type=semver,pattern={{major}} + type=raw,value=${{ inputs.tag }},enable=${{ inputs.tag != '' }} + type=ref,event=branch,enable=${{ inputs.tag == '' }} + type=ref,event=pr,enable=${{ inputs.tag == '' }} + type=semver,pattern={{version}},enable=${{ inputs.tag == '' }} + type=semver,pattern={{major}}.{{minor}},enable=${{ inputs.tag == '' }} + type=semver,pattern={{major}},enable=${{ inputs.tag == '' }} - name: Build and push main image id: docker_build_main @@ -56,11 +79,12 @@ jobs: with: images: ghcr.io/${{ github.repository }}/worker tags: | - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - type=semver,pattern={{major}} + type=raw,value=${{ inputs.tag }},enable=${{ inputs.tag != '' }} + type=ref,event=branch,enable=${{ inputs.tag == '' }} + type=ref,event=pr,enable=${{ inputs.tag == '' }} + type=semver,pattern={{version}},enable=${{ inputs.tag == '' }} + type=semver,pattern={{major}}.{{minor}},enable=${{ inputs.tag == '' }} + type=semver,pattern={{major}},enable=${{ inputs.tag == '' }} - name: Build and push worker image id: docker_build_worker diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml new file mode 100644 index 0000000..34c2c51 --- /dev/null +++ b/.github/workflows/staging.yml @@ -0,0 +1,46 @@ +# the staging branch is rebuilt from scratch at every change: the default branch plus the open pull +# requests of this repository labeled "staging", then built as the image tag "staging" and deployed +# on the staging environment, which always follows that tag + +name: Staging + +on: + push: + branches: [master, main] + pull_request: + types: [labeled, unlabeled, synchronize, reopened, closed] + workflow_dispatch: + +jobs: + # shared by the data-fair services, maintained in data-fair + rebuild: + uses: data-fair/data-fair/.github/workflows/staging-rebuild.yml@master + permissions: + contents: write + pull-requests: write + + # a push made with GITHUB_TOKEN triggers no workflow: the staging branch is built and deployed here + build: + needs: rebuild + if: needs.rebuild.outputs.commit != '' + concurrency: + group: staging-build + cancel-in-progress: true + permissions: + contents: read + packages: write + uses: ./.github/workflows/reuse-build.yml + with: + # the commit of this rebuild, not the branch, which a newer rebuild may have moved already + ref: ${{ needs.rebuild.outputs.commit }} + tag: staging + pull-requests: ${{ needs.rebuild.outputs.pull-requests }} + + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Send Keel notifications to staging-koumoul.com + run: | + curl -s --fail --show-error -X POST https://keel.admin.staging-koumoul.com/v1/webhooks/native -u ${{ secrets.KEEL_STAGING_USER }}:${{ secrets.KEEL_STAGING_PASSWORD }} -d '{"name": "ghcr.io/${{ github.repository }}", "tag": "staging"}' + curl -s --fail --show-error -X POST https://keel.admin.staging-koumoul.com/v1/webhooks/native -u ${{ secrets.KEEL_STAGING_USER }}:${{ secrets.KEEL_STAGING_PASSWORD }} -d '{"name": "ghcr.io/${{ github.repository }}/worker", "tag": "staging"}'