# Automatisieren mithilfe von Projects-Aktionen

Sie können GitHub Actions verwenden, um Ihre Projekte zu automatisieren.

## GitHub Actions Arbeitsabläufe

In diesem Artikel wird gezeigt, wie die GraphQL-API und GitHub Actions verwendet werden, um einem Organisationsprojekt einen Pull Request hinzuzufügen. Wenn der Pull Request in den Beispiel-Workflows als „Bereit zur Überprüfung“ gekennzeichnet ist, wird dem Projekt ein neuer Auftrag hinzugefügt. Bei diesem Auftrag wird das Feld „Status“ auf „To-Do“ festgelegt, und das aktuelle Datum wird einem benutzerdefinierten „Datum der Veröffentlichung“-Feld hinzugefügt.

Du kannst einen der nachstehenden Workflows kopieren und wie in der folgenden Tabelle beschrieben so ändern, dass er deinen Anforderungen entspricht.

Ein Projekt kann mehrere Repositorys umfassen, ein Workflow ist jedoch für ein Repository spezifisch. Füge den Workflow jedem Repository hinzu, das dein Projekt nachverfolgen soll. Weitere Informationen zum Erstellen von Workflowdateien findest du unter [Schnellstart für GitHub Actions](/de/enterprise-cloud@latest/actions/get-started/quickstart).

In diesem Artikel wird davon ausgegangen, dass Sie ein grundlegendes Verständnis von GitHub Actions. Weitere Informationen zu GitHub Actions findest du unter [GitHub Actions Dokumentation](/de/enterprise-cloud@latest/actions).

Weitere Informationen zu sonstigen Änderungen, die du an deinem Projekt über die API vornehmen kannst, findest du unter [Die API zum Verwalten von Projects verwenden](/de/enterprise-cloud@latest/issues/planning-and-tracking-with-projects/automating-your-project/using-the-api-to-manage-projects).

Möglicherweise möchten Sie auch den Workflow **actions/add-to-project** verwenden, der von GitHub verwaltet wird und das aktuelle Issue oder den aktuellen Pull Request dem angegebenen Projekt hinzufügt. Weitere Informationen findest du im [actions/add-to-project](https://github.com/actions/add-to-project)-Repository und der zugehörigen README.

Bei Projektmanagementaufgaben, die das Verständnis des Repositorykontexts erfordern , z. B. das Triagieren neuer Probleme, das Zusammenfassen von Backlogs, das Klassifizieren von Problemen nach Typ oder Priorität und die Empfehlung von Feldwerten,agentic workflows eignen sich besser als herkömmliche GitHub Actions Workflows. Im Gegensatz zu festen Workflowschritten können Sie beschreiben, agentic workflows was Sie in natürlicher Sprache wünschen, und ein KI-Codierungs-Agent verarbeitet die Gründe und Entscheidungsfindung.  Weitere Informationen finden Sie unter [Erstellen von agentenbasierten GitHub-Workflows](/de/enterprise-cloud@latest/copilot/how-tos/github-agentic-workflows/creating-github-agentic-workflows).

> [!NOTE]
> `GITHUB_TOKEN` ist auf die Repository-Ebene beschränkt und kann nicht auf Projekte zugreifen. Für den Zugriff Projekte können Sie entweder ein GitHub App (empfohlen für Organisationsprojekte) oder ein personal access token (empfohlen für Benutzerprojekte) erstellen. Unten werden Workflowbeispiele für beide Ansätze gezeigt.

## Beispiel für eine Workflowauthentifizierung mit einem GitHub App

Weitere Informationen zur Authentifizierung in einem Workflow mit einem GitHub ActionsGitHub Appfinden Sie unter [Erstellen authentifizierter API-Anforderungen mit einer GitHub App in einem GitHub Actions-Workflow](/de/enterprise-cloud@latest/apps/creating-github-apps/authenticating-with-a-github-app/making-authenticated-api-requests-with-a-github-app-in-a-github-actions-workflow).

1. Erstellen Sie ein GitHub App oder wählen Sie ein vorhandenes GitHub App, das sich im Besitz Ihrer Organisation befindet. Weitere Informationen finden Sie unter [Registrieren einer GitHub-App](/de/enterprise-cloud@latest/apps/creating-github-apps/registering-a-github-app/registering-a-github-app).

2. Erteilen Sie Ihrem GitHub App Lese- und Schreibberechtigungen für Projekte der Organisation. Für dieses spezifische Beispiel benötigt GitHub App auch Leseberechtigungen für Repository-Pullanforderungen und Repositoryprobleme. Weitere Informationen finden Sie unter [Ändern einer GitHub App-Registrierung](/de/enterprise-cloud@latest/apps/maintaining-github-apps/modifying-a-github-app-registration).

   > [!NOTE]
   > Du kannst die Berechtigungen deiner App für Organisationsprojekte und für Repositoryprojekte steuern. Du musst die Berechtigung zum Lesen und Schreiben von Organisationsprojekten erteilen. Die Lese- und Schreibberechtigung für Repositoryprojekte ist nicht ausreichend.

3. Installieren Sie die GitHub App Datei in Ihrer Organisation. Installiere sie für alle Repositorys, auf die dein Projekt zugreifen muss. Weitere Informationen finden Sie unter [Installieren Ihrer eigenen GitHub App](/de/enterprise-cloud@latest/apps/using-github-apps/installing-your-own-github-app).

4. Speichern Sie ihre GitHub AppClient-ID als Konfigurationsvariable in Ihrem Repository oder Ihrer Organisation. Ersetzen Sie `APP_CLIENT_ID` im folgenden Workflow durch den Namen der Konfigurationsvariablen. Sie finden Ihre Client-ID auf der Einstellungsseite für Ihre App oder über die App-API. Weitere Informationen finden Sie unter [REST-API-Endpunkte für Apps](/de/enterprise-cloud@latest/rest/apps#get-an-app). Weitere Informationen zu Konfigurationsvariablen findest du unter [Speichern von Informationen in Variablen](/de/enterprise-cloud@latest/actions/how-tos/write-workflows/choose-what-workflows-do/use-variables#defining-configuration-variables-for-multiple-workflows).

5. Generiere einen privaten Schlüssel für deine App. Speichere den Inhalt der resultierenden Datei als Geheimnis in deinem Repository oder deiner Organisation. (Speichere den gesamten Inhalt der Datei, einschließlich `-----BEGIN RSA PRIVATE KEY-----` und `-----END RSA PRIVATE KEY-----`.) Ersetze `APP_PRIVATE_KEY` im folgenden Workflow durch den Namen des Geheimnisses. Weitere Informationen finden Sie unter [Verwalten privater Schlüssel für GitHub Apps](/de/enterprise-cloud@latest/apps/creating-github-apps/authenticating-with-a-github-app/managing-private-keys-for-github-apps). Weitere Informationen zum Speichern von Geheimnissen findest du unter [Verwenden von Geheimnissen in GitHub-Aktionen](/de/enterprise-cloud@latest/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets).

6. Ersetze `YOUR_ORGANIZATION` im folgenden Workflow durch den Namen deiner Organisation. Beispiel: `octo-org`. Ersetze `YOUR_PROJECT_NUMBER` durch deine Projektnummer. Sieh dir die URL des Projekts an, um die Projektnummer zu finden.
   `https://github.com/orgs/octo-org/projects/5` hat beispielsweise die Projektnummer 5. Damit dieses spezifische Beispiel funktioniert, muss dein Projekt auch über ein Datumsfeld „Veröffentlichungsdatum“ verfügen.

```yaml annotate copy
#
name: Add PR to project
# This workflow runs whenever a pull request in the repository is marked as "ready for review".
on:
  pull_request:
    types:
      - ready_for_review
jobs:
  track_pr:
    runs-on: ubuntu-latest
    steps:
    # Uses the [actions/create-github-app-token](https://github.com/marketplace/actions/create-github-app-token) action to generate an installation access token for your app from the client ID and private key. The installation access token is accessed later in the workflow as `${{ steps.generate-token.outputs.token }}`.
    #
    # Replace `APP_CLIENT_ID` with the name of the configuration variable that contains your client ID.
    #
    # Replace `APP_PRIVATE_KEY` with the name of the secret that contains your app private key.
      - name: Generate token
        id: generate-token
        uses: actions/create-github-app-token@v3
        with:
          client-id: ${{ vars.APP_CLIENT_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}
      # Sets environment variables for this step.
      #
      # Replace `YOUR_ORGANIZATION` with the name of your organization. For example, `octo-org`.
      #
      # Replace `YOUR_PROJECT_NUMBER` with your project number. To find the project number, look at the project URL. For example, `https://github.com/orgs/octo-org/projects/5` has a project number of 5.
      - name: Get project data
        env:
          GH_TOKEN: ${{ steps.generate-token.outputs.token }}
          ORGANIZATION: YOUR_ORGANIZATION
          PROJECT_NUMBER: YOUR_PROJECT_NUMBER
        # Uses [GitHub CLI manual](https://cli.github.com/manual/) to query the API for the ID of the project and return the name and ID of the first 20 fields in the project. `fields` returns a union and the query uses inline fragments (`... on`) to return information about any `ProjectV2Field` and `ProjectV2SingleSelectField` fields. The response is stored in a file called `project_data.json`.
        run: |
          gh api graphql -f query='
            query($org: String!, $number: Int!) {
              organization(login: $org){
                projectV2(number: $number) {
                  id
                  fields(first:20) {
                    nodes {
                      ... on ProjectV2Field {
                        id
                        name
                      }
                      ... on ProjectV2SingleSelectField {
                        id
                        name
                        options {
                          id
                          name
                        }
                      }
                    }
                  }
                }
              }
            }' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json

          # Parses the response from the API query and stores the relevant IDs as environment variables. Modify this to get the ID for different fields or options. For example:
          #
          # - To get the ID of a field called `Team`, add `echo 'TEAM_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") | .id' project_data.json) >> $GITHUB_ENV`.
          # - To get the ID of an option called `Octoteam` for the `Team` single select field, add `echo 'OCTOTEAM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") |.options[] | select(.name=="Octoteam") |.id' project_data.json) >> $GITHUB_ENV`.
          #
          # **Note:** This workflow assumes that you have a project with a single select field called "Status" that includes an option called "Todo" and a date field called "Date posted". You must modify this section to match the fields that are present in your table.

          echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV
          echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV
          echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
          echo 'TODO_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV

# Sets environment variables for this step. `GH_TOKEN` is the token generated in the first step. `PR_ID` is the ID of the pull request that triggered this workflow.
      - name: Add PR to project
        env:
          GH_TOKEN: ${{ steps.generate-token.outputs.token }}
          PR_ID: ${{ github.event.pull_request.node_id }}
        # Uses [GitHub CLI manual](https://cli.github.com/manual/) and the API to add the pull request that triggered this workflow to the project. The `jq` flag parses the response to get the ID of the created item.
        run: |
          item_id="$( gh api graphql -f query='
            mutation($project:ID!, $pr:ID!) {
              addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) {
                item {
                  id
                }
              }
            }' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')"

          # Stores the ID of the created item as an environment variable.
          echo 'ITEM_ID='$item_id >> $GITHUB_ENV

# Saves the current date as an environment variable in `yyyy-mm-dd` format.
      - name: Get date
        run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV

# Sets environment variables for this step. `GH_TOKEN` is the token generated in the first step.
      - name: Set fields
        env:
          GH_TOKEN: ${{ steps.generate-token.outputs.token }}
        # Sets the value of the `Status` field to `Todo`. Sets the value of the `Date posted` field.
        run: |
          gh api graphql -f query='
            mutation (
              $project: ID!
              $item: ID!
              $status_field: ID!
              $status_value: String!
              $date_field: ID!
              $date_value: Date!
            ) {
              set_status: updateProjectV2ItemFieldValue(input: {
                projectId: $project
                itemId: $item
                fieldId: $status_field
                value: {
                  singleSelectOptionId: $status_value
                  }
              }) {
                projectV2Item {
                  id
                  }
              }
              set_date_posted: updateProjectV2ItemFieldValue(input: {
                projectId: $project
                itemId: $item
                fieldId: $date_field
                value: {
                  date: $date_value
                }
              }) {
                projectV2Item {
                  id
                }
              }
            }' -f project=$PROJECT_ID -f item=$ITEM_ID -f status_field=$STATUS_FIELD_ID -f status_value=${{ env.TODO_OPTION_ID }} -f date_field=$DATE_FIELD_ID -f date_value=$DATE --silent
```

## Beispiel für eine Workflowauthentifizierung mit einem personal access token

1. Erstellen Sie ein personal access token (classic) mit den Gültigkeitsbereichen `project` und `repo`. Weitere Informationen finden Sie unter [Verwalten deiner persönlichen Zugriffstoken](/de/enterprise-cloud@latest/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens).
2. Speichern Sie den personal access token geheimen Schlüssel in Ihrem Repository oder Ihrer Organisation.
3. Ersetze `YOUR_TOKEN` im folgenden Workflow durch den Namen des Geheimnisses. Ersetze `YOUR_ORGANIZATION` durch den Namen deiner Organisation. Beispiel: `octo-org`. Ersetze `YOUR_PROJECT_NUMBER` durch deine Projektnummer. Sieh dir die URL des Projekts an, um die Projektnummer zu finden.
   `https://github.com/orgs/octo-org/projects/5` hat beispielsweise die Projektnummer 5.

```yaml annotate copy
# This workflow runs whenever a pull request in the repository is marked as "ready for review".
name: Add PR to project
on:
  pull_request:
    types:
      - ready_for_review
jobs:
  track_pr:
    runs-on: ubuntu-latest
    steps:
    # Sets environment variables for this step.
    #
    # If you are using a personal access token, replace `YOUR_TOKEN` with the name of the secret that contains your personal access token.
    #
    # Replace `YOUR_ORGANIZATION` with the name of your organization. For example, `octo-org`.
    #
    # Replace `YOUR_PROJECT_NUMBER` with your project number. To find the project number, look at the project URL. For example, `https://github.com/orgs/octo-org/projects/5` has a project number of 5.
      - name: Get project data
        env:
          GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
          ORGANIZATION: YOUR_ORGANIZATION
          PROJECT_NUMBER: YOUR_PROJECT_NUMBER
        # Uses [GitHub CLI manual](https://cli.github.com/manual/) to query the API for the ID of the project and return the name and ID of the first 20 fields in the project. `fields` returns a union and the query uses inline fragments (`... on`) to return information about any `ProjectV2Field` and `ProjectV2SingleSelectField` fields. The response is stored in a file called `project_data.json`.
        run: |
          gh api graphql -f query='
            query($org: String!, $number: Int!) {
              organization(login: $org){
                projectV2(number: $number) {
                  id
                  fields(first:20) {
                    nodes {
                      ... on ProjectV2Field {
                        id
                        name
                      }
                      ... on ProjectV2SingleSelectField {
                        id
                        name
                        options {
                          id
                          name
                        }
                      }
                    }
                  }
                }
              }
            }' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json

          # Parses the response from the API query and stores the relevant IDs as environment variables. Modify this to get the ID for different fields or options. For example:
          #
          # - To get the ID of a field called `Team`, add `echo 'TEAM_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") | .id' project_data.json) >> $GITHUB_ENV`.
          # - To get the ID of an option called `Octoteam` for the `Team` single select field, add `echo 'OCTOTEAM_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Team") |.options[] | select(.name=="Octoteam") |.id' project_data.json) >> $GITHUB_ENV`.
          #
          # **Note:** This workflow assumes that you have a project with a single select field called "Status" that includes an option called "Todo" and a date field called "Date posted". You must modify this section to match the fields that are present in your table.

          echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV
          echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Date posted") | .id' project_data.json) >> $GITHUB_ENV
          echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
          echo 'TODO_OPTION_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "Status") | .options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV

# Sets environment variables for this step. Replace `YOUR_TOKEN` with the name of the secret that contains your personal access token.
      - name: Add PR to project
        env:
          GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
          PR_ID: ${{ github.event.pull_request.node_id }}
        # Uses [GitHub CLI manual](https://cli.github.com/manual/) and the API to add the pull request that triggered this workflow to the project. The `jq` flag parses the response to get the ID of the created item.
        run: |
          item_id="$( gh api graphql -f query='
            mutation($project:ID!, $pr:ID!) {
              addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) {
                item {
                  id
                }
              }
            }' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')"

          # Stores the ID of the created item as an environment variable.
          echo 'ITEM_ID='$item_id >> $GITHUB_ENV

# Saves the current date as an environment variable in `yyyy-mm-dd` format.
      - name: Get date
        run: echo "DATE=$(date +"%Y-%m-%d")" >> $GITHUB_ENV

# Sets environment variables for this step. Replace `YOUR_TOKEN` with the name of the secret that contains your personal access token.
      - name: Set fields
        env:
          GH_TOKEN: ${{ secrets.YOUR_TOKEN }}
        # Sets the value of the `Status` field to `Todo`. Sets the value of the `Date posted` field.
        run: |
          gh api graphql -f query='
            mutation (
              $project: ID!
              $item: ID!
              $status_field: ID!
              $status_value: String!
              $date_field: ID!
              $date_value: Date!
            ) {
              set_status: updateProjectV2ItemFieldValue(input: {
                projectId: $project
                itemId: $item
                fieldId: $status_field
                value: {
                  singleSelectOptionId: $status_value
                  }
              }) {
                projectV2Item {
                  id
                  }
              }
              set_date_posted: updateProjectV2ItemFieldValue(input: {
                projectId: $project
                itemId: $item
                fieldId: $date_field
                value: {
                  date: $date_value
                }
              }) {
                projectV2Item {
                  id
                }
              }
            }' -f project=$PROJECT_ID -f item=$ITEM_ID -f status_field=$STATUS_FIELD_ID -f status_value=${{ env.TODO_OPTION_ID }} -f date_field=$DATE_FIELD_ID -f date_value=$DATE --silent

```