|
| 1 | +name: Create and publish devcontainer Docker image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - "main" |
| 7 | + paths: |
| 8 | + - ".devcontainer/Dockerfile" |
| 9 | + - ".github/workflows/CreateDevcontainerImage.yml" |
| 10 | + - "rust-toolchain.toml" |
| 11 | + |
| 12 | +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. |
| 13 | +env: |
| 14 | + REGISTRY: ghcr.io |
| 15 | + IMAGE_NAME: ${{ github.repository }}-devcontainer |
| 16 | + USER: vscode |
| 17 | + GROUP: vscode |
| 18 | + LLVM_VERSION: 17 |
| 19 | + RUST_TOOLCHAIN_DEFAULT: 1.81.0 |
| 20 | + RUST_TOOLCHAIN_FILE: rust-toolchain.toml |
| 21 | + |
| 22 | +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. |
| 23 | +jobs: |
| 24 | + build-and-push-image: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. |
| 27 | + permissions: |
| 28 | + contents: read |
| 29 | + packages: write |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Checkout repository |
| 33 | + uses: actions/checkout@v3 |
| 34 | + |
| 35 | + - name: Read Rust toolchain version from ${{ env.RUST_TOOLCHAIN_FILE }} |
| 36 | + id: toolchain |
| 37 | + run: | |
| 38 | + version=$(cat ${{ env.RUST_TOOLCHAIN_FILE }} | sed -n '/\[toolchain\]/,/^\[/{/^\s*channel = /s/[^"]*"\([^"]*\)".*/\1/p}') |
| 39 | + cat ${{ env.RUST_TOOLCHAIN_FILE }} | grep $version &> /dev/null \ |
| 40 | + && echo "RUST_TOOLCHAIN=${version}" >> "$GITHUB_OUTPUT" \ |
| 41 | + || echo "RUST_TOOLCHAIN=${{ env.RUST_TOOLCHAIN_FILE }}" >> "$GITHUB_OUTPUT" |
| 42 | + |
| 43 | + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. |
| 44 | + - name: Log in to the Container registry |
| 45 | + uses: docker/login-action@v1 |
| 46 | + with: |
| 47 | + registry: ${{ env.REGISTRY }} |
| 48 | + username: ${{ github.actor }} |
| 49 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 50 | + |
| 51 | + - name: Extract metadata (tags, labels) for Docker |
| 52 | + id: meta |
| 53 | + uses: docker/metadata-action@v5 |
| 54 | + with: |
| 55 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 56 | + |
| 57 | + - name: Build and push Docker image |
| 58 | + id: push |
| 59 | + uses: docker/build-push-action@v6 |
| 60 | + with: |
| 61 | + context: ./.devcontainer |
| 62 | + push: true |
| 63 | + tags: | |
| 64 | + ${{ steps.meta.outputs.tags }} |
| 65 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest |
| 66 | + labels: ${{ steps.meta.outputs.labels }} |
| 67 | + build-args: | |
| 68 | + USER=${{ env.USER }} |
| 69 | + GROUP=${{ env.GROUP }} |
| 70 | + LLVM_VERSION=${{ env.LLVM_VERSION }} |
| 71 | + RUST_TOOLCHAIN=${{ steps.toolchain.outputs.RUST_TOOLCHAIN }} |
0 commit comments