|
| 1 | +# This workflow builds and publishes the ReefGuideAPI base Docker image to GitHub Container Registry (ghcr.io) |
| 2 | +# It is triggered when a push is made to the main branch. |
| 3 | + |
| 4 | +# Additional notes: |
| 5 | +# - The workflow uses the github.repository context to name the image, ensuring it's tied to your repository |
| 6 | +# - The GITHUB_TOKEN is automatically provided by GitHub Actions, no need to set it up manually |
| 7 | +# - The Docker metadata action automatically generates appropriate tags based on the release version |
| 8 | +# - The Julia version can be easily updated by changing the JULIA_VERSION environment variable at the top of the workflow |
| 9 | + |
| 10 | +name: Build and Publish ReefGuideAPI.jl Docker Image |
| 11 | + |
| 12 | +on: |
| 13 | + push: |
| 14 | + branches: |
| 15 | + - main |
| 16 | + # TODO Remove before merge |
| 17 | + - dockerisation |
| 18 | + release: |
| 19 | + types: [published] |
| 20 | + |
| 21 | +env: |
| 22 | + REGISTRY: ghcr.io |
| 23 | + IMAGE_NAME: ${{ github.repository }}/reefguide-src |
| 24 | + JULIA_VERSION: 1.10.5 |
| 25 | + # Set to true to use a fixed ReefGuideAPI version for debugging, false to use the release version |
| 26 | + USE_FIXED_REEFGUIDEAPI_VERSION: true |
| 27 | + # TODO this doesn't make sense until the releasing is sorted out |
| 28 | + # The fixed ReefGuideAPI version to use when USE_FIXED_REEFGUIDEAPI_VERSION is true |
| 29 | + FIXED_REEFGUIDEAPI_VERSION: "0.1.0" |
| 30 | + |
| 31 | +jobs: |
| 32 | + build-and-push-image: |
| 33 | + runs-on: ubuntu-latest |
| 34 | + permissions: |
| 35 | + contents: read |
| 36 | + packages: write |
| 37 | + |
| 38 | + steps: |
| 39 | + # Step 1: Checkout the repository code |
| 40 | + - name: Checkout repository |
| 41 | + uses: actions/checkout@v3 |
| 42 | + |
| 43 | + # Step 2: Extract the version number from the release tag |
| 44 | + # This removes the 'v' prefix from the tag (e.g., 'v1.2.3' becomes '1.2.3') |
| 45 | + - name: Extract version from tag |
| 46 | + id: get_version |
| 47 | + run: | |
| 48 | + if ${{ env.USE_FIXED_REEFGUIDEAPI_VERSION == 'true' }}; then |
| 49 | + echo "Using fixed ReefGuideAPI version: ${{ env.FIXED_REEFGUIDEAPI_VERSION }}" |
| 50 | + echo "VERSION=${{ env.FIXED_REEFGUIDEAPI_VERSION }}" >> $GITHUB_OUTPUT |
| 51 | + elif [ "${{ github.event_name }}" = "release" ]; then |
| 52 | + RELEASE_VERSION=${GITHUB_REF#refs/tags/v} |
| 53 | + echo "Using release version: $RELEASE_VERSION" |
| 54 | + echo "VERSION=$RELEASE_VERSION" >> $GITHUB_OUTPUT |
| 55 | + else |
| 56 | + echo "No version specified and not a release event. Using default version." |
| 57 | + echo "VERSION=${{ env.FIXED_REEFGUIDEAPI_VERSION }}" >> $GITHUB_OUTPUT |
| 58 | + fi |
| 59 | +
|
| 60 | + # Step 3: Log in to the GitHub Container Registry |
| 61 | + # This uses the provided GitHub token for authentication |
| 62 | + - name: Log in to the Container registry |
| 63 | + uses: docker/login-action@v2 |
| 64 | + with: |
| 65 | + registry: ${{ env.REGISTRY }} |
| 66 | + username: ${{ github.actor }} |
| 67 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + |
| 69 | + # Step 4: Extract metadata for Docker |
| 70 | + # This step generates tags and labels for the Docker image |
| 71 | + - name: Extract metadata (tags, labels) for Docker |
| 72 | + id: meta |
| 73 | + uses: docker/metadata-action@v4 |
| 74 | + with: |
| 75 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 76 | + tags: | |
| 77 | + type=semver,pattern={{version}} |
| 78 | + type=semver,pattern={{major}}.{{minor}} |
| 79 | + type=semver,pattern={{major}}.{{minor}}.{{patch}} |
| 80 | + type=semver,pattern={{major}} |
| 81 | + type=ref,event=branch |
| 82 | + type=sha,format=long |
| 83 | + type=sha,format=short |
| 84 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} |
| 85 | +
|
| 86 | + # Step 5: Build and push the Docker image |
| 87 | + # This step builds the reefguide-src image and pushes it to the registry |
| 88 | + - name: Build and push Docker image |
| 89 | + uses: docker/build-push-action@v4 |
| 90 | + with: |
| 91 | + context: . |
| 92 | + target: reefguide-src # Specifies which stage of the Dockerfile to build |
| 93 | + push: true # Pushes the image to the registry |
| 94 | + tags: ${{ steps.meta.outputs.tags }} # Uses the tags generated in the metadata step |
| 95 | + labels: ${{ steps.meta.outputs.labels }} # Uses the labels generated in the metadata step |
| 96 | + # Passes the Julia and ReefGuideAPI versions to the Dockerfile |
| 97 | + # TODO bring back this build arg when releasing is ready |
| 98 | + # REEFGUIDE_VERSION=${{ steps.get_version.outputs.VERSION }} |
| 99 | + build-args: | |
| 100 | + JULIA_VERSION=${{ env.JULIA_VERSION }} |
0 commit comments