|
| 1 | +name: Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + schedule: |
| 6 | + - cron: '0 0 * * *' |
| 7 | + |
| 8 | +jobs: |
| 9 | + buildAndRelease: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + permissions: |
| 13 | + # needed for creating a release |
| 14 | + contents: write |
| 15 | + # needed for pushing docker image |
| 16 | + attestations: write |
| 17 | + id-token: write |
| 18 | + packages: write |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Set up jq |
| 25 | + run: | |
| 26 | + sudo apt-get update && sudo apt-get install jq -y |
| 27 | +
|
| 28 | + - name: Set up Name and Version |
| 29 | + id: releases |
| 30 | + run: | |
| 31 | + NAME=${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/} |
| 32 | + THIS_REPO=https://api.github.com/repos/$GITHUB_REPOSITORY/releases/latest |
| 33 | + TARGET_REPO=https://api.github.com/repos/coder/code-server/releases/latest |
| 34 | + CURRENT_VERSION=$(curl -s $THIS_REPO | jq -r '.tag_name // empty') |
| 35 | + TARGET_VERSION=$(curl -s $TARGET_REPO | jq -r '.tag_name // empty') |
| 36 | + RELEASE_URL=$(curl -s $TARGET_REPO | jq -r '.html_url') |
| 37 | + echo "VERSION=${TARGET_VERSION}" >> $GITHUB_ENV |
| 38 | + echo "NAME=${NAME}" >> $GITHUB_ENV |
| 39 | + echo "url=${RELEASE_URL}" >> $GITHUB_OUTPUT |
| 40 | + echo "current=${CURRENT_VERSION}" >> $GITHUB_OUTPUT |
| 41 | + echo "target=${TARGET_VERSION}" >> $GITHUB_OUTPUT |
| 42 | + echo Using NAME=$NAME, TARGET_VERSION=$TARGET_VERSION, CURRENT_VERSION=$CURRENT_VERSION, RELEASE_URL=$RELEASE_URL |
| 43 | +
|
| 44 | + - name: Set up Docker |
| 45 | + uses: docker/setup-buildx-action@v2 |
| 46 | + |
| 47 | + - name: Build Docker image |
| 48 | + env: |
| 49 | + VERSION: ${{ steps.releases.outputs.target }} |
| 50 | + run: | |
| 51 | + docker build --build-arg VERSION=$VERSION --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` -t ghcr.io/${{ github.repository_owner }}/$NAME:$VERSION . |
| 52 | + docker tag ghcr.io/${{ github.repository_owner }}/$NAME:$VERSION ghcr.io/${{ github.repository_owner }}/$NAME:latest |
| 53 | +
|
| 54 | + - name: Create GitHub Release |
| 55 | + uses: softprops/action-gh-release@v2 |
| 56 | + if: steps.releases.outputs.current != steps.releases.outputs.target |
| 57 | + env: |
| 58 | + VERSION: ${{ steps.releases.outputs.target }} |
| 59 | + with: |
| 60 | + name: Update to ${{ steps.releases.outputs.target }} |
| 61 | + tag_name: ${{ steps.releases.outputs.target }} |
| 62 | + body: See release notes [here](${{ steps.releases.outputs.url }}) |
| 63 | + |
| 64 | + - name: Log in to GitHub Container Registry |
| 65 | + uses: docker/login-action@v3 |
| 66 | + if: steps.releases.outputs.current != steps.releases.outputs.target |
| 67 | + with: |
| 68 | + registry: ghcr.io |
| 69 | + username: ${{ github.actor }} |
| 70 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 71 | + |
| 72 | + - name: Push Docker image |
| 73 | + if: steps.releases.outputs.current != steps.releases.outputs.target |
| 74 | + env: |
| 75 | + VERSION: ${{ steps.releases.outputs.target }} |
| 76 | + run: | |
| 77 | + docker push ghcr.io/${{ github.repository_owner }}/$NAME:$VERSION |
| 78 | + docker push ghcr.io/${{ github.repository_owner }}/$NAME:latest |
0 commit comments